The map() and flatmap() are two important operations in the new functional Java 8. Both represent functional operation and they are also methods in java.util.stream.Stream class but the map is used for transformation and flatmap is used for both transformation and flattening, that's why it's called the flatmap. The key difference between map() and flatmap() function is that when you use a map(), it applies a function on each element of the stream and stores the value returned by the function into a new Stream. This way one stream is transformed into another like a Stream of String is transformed into a Stream of Integer where each element is the length of the corresponding Stream.
Wednesday, September 22, 2021
Tuesday, September 21, 2021
10 Example of Lambda Expressions and Streams in Java 8
Java 8 release is just a couple of weeks away, scheduled on 18th March 2014, and there is a lot of buzz and excitement about this path-breaking release in the Java community. One feature, which is synonymous with this release is lambda expressions, which will provide the ability to pass behaviors to methods. Prior to Java 8, if you want to pass behavior to a method, then your only option was the Anonymous class, which will take 6 lines of code, and the most important line, which defines the behavior is lost in between. Lambda expression replaces anonymous classes and removes all boilerplate, enabling you to write code in a functional style, which is sometimes more readable and expression.
Labels:
core java
,
Java 8
,
Lambda expression
,
programming
Monday, September 20, 2021
5 Difference between Constructor and Static Factory method Pattern in Java
The common way to create objects in Java is by using public constructors. A class provides a public constructor like java.lang.String so anyone can create an instance of String class to use in their application, but, there is another technique that can be used to create objects in Java and every experienced Java programmer should know about it. A class can provide a public static factory method that can return an instance of the class e.g. HashMap.newInstance(). The factory method is a smart way to create objects in Java and provides several advantages over the traditional approach of creating objects using constructors in Java. It can also improve the quality of code by making the code more readable, less coupled, and improves performance by caching.
Labels:
best practices
,
core java
,
design patterns
How to set Java Path and Classpath in Windows 7, 8 and Windows 10 - Tutorial
So, you just bought a new PC or Laptop with Windows 8 operating system, and wondering how to set PATH and Classpath on Windows 8; Or, you might have just upgraded your Windows 7 laptop to the professional edition of Windows 8 and looking to set JDK Path to compile Java programs. Not to worry, this is the second step for anyone who wants to learn Java programming. Of course, the first step is to install JDK. In this Java tutorial, we will see step by step guide to set Java PATH and CLASSPATH in the Windows 8 operating system. By the way, if you are learning Java in Windows 10 operating system, You should still be able to set the Java path and classpath in Windows 10 by following the steps given here, because navigation steps for modifying environment variables on Windows 10 and Windows 8 are almost same.
Labels:
core java
,
Java Programming Tutorials
,
windows 8
Difference between @WebMvcTest and @DataJpaTest in Spring Boot? Example
Hello guys, testing the Spring Boot application is very important but not many
developers pay attention to that. If you are learning how to test the Spring
Boot application and wondering what is
@WebMvcTest and
@DataJpaTest and how to use them then
you have come to the right place. Earlier, I have shared
20+ Spring Boot Testing interview questions
and in this article, I will teach you how to use
@DataJpaTest and
@WebMvcTest annotation with
real-world examples. Spring Boot is an evolution of the Spring framework which helps to create both stand-alone and spring-based applications with minimal cost.
Labels:
spring boot
,
spring interview questions
,
Testing
Saturday, September 18, 2021
How to Read/Write from RandomAccessFile in Java - Example Tutorial
Random access file is a special kind of file in Java that allows non-sequential or random access to any location in the file. This means you don't need to start from 1st line if you want to read line number 10, you can directly go to line 10 and read. It's similar to the array data structure, Just like you can access any element in the array by index you can read any content from the file by using a file pointer. A random-access file actually behaves like a large array of bytes stored in the file system and that's why it's very useful for low latency applications which need some kind of persistence e.g. in front office trading application and FIX Engine, you can use random access file to store FIX sequence numbers or all open orders.
Labels:
core java
,
java IO tutorial
Friday, September 17, 2021
Difference between JAX-RS, Restlet, Jersey, RESTEasy, and Apache CXF Frameworks
The JAX-RS is a Java specification request (JSR 311 & JSR 339) that standardizes the development and deployment of RESTful web services using Java and JEE technologies. It provides API in Java Programming language to create web services according to the REST (Representational State Transfer) architectural pattern. Both Restlet and Jersey are two of the most popular implementation of JAX-RS used for developing RESTful web services in Java ecosystem but there is a couple of other implementation also exist like Apache Wink, Apache CXF, and JBoss RESTEasy, along with omnipresent and the most popular option of creating REST web services with Spring Framework, Spring Boot, and Spring MVC.
Labels:
J2EE
,
java interview questions
,
REST
Thursday, September 16, 2021
How to compare objects on natural order in Java? Comparable + compareTo Example
Java allows you to sort your object in natural order by implementing a Comparable interface. It's one of the fundamental interfaces of Java API and is defined in java.lang package, which means you don't need to implement this unlike its counterpart Comparator, which is defined in java.util package. Comparable is used to provide natural order of sorting to objects e.g. numeric order is a natural order for numbers, alphabetic order is a natural order for String and chronological order is natural for dates. Similarly when you define your own objects like Person, Book, or Employee, sorting them by name sounds natural. Similarly, for teams, ranking seems their natural order. It all depends on how the object is looked into in its domain.
Labels:
core java
,
java collection tutorial
,
perogramming
Tuesday, September 14, 2021
5 ways to check if String is empty in Java - examples
String in Java is considered empty if it's not null and its length is
zero. By the way, before checking length you should verify that String is not
null because calling length() method on null String
will result in java.lang.NullPointerException.
Empty String is represented by String literal “”. The definition of empty String may be extended to those String as well which only
contains white space but it's a specific requirement and in general String with white space is not considered as empty String in Java. Since String is one of the most
frequently used classes and commonly used in method arguments, we often need to
check if String is empty or not. Thankfully there are multiple ways to find if String is empty in Java or not.
Labels:
coding
,
core java
,
programming
,
spring
Monday, September 13, 2021
Difference between for loop and Enhanced for loop in Java? Example Tutorial
Though you can use both for loop and enhanced for loop to iterate over arrays and collections like a list, set, or map. There are some key differences between them. In general, enhanced for loop is much easier to use and less error-prone than for loop, where you need to manage the steps manually. At the same time, for loop is much more powerful because you get the opportunity to control the looping process. All the differences, you will learn in this article, stems from the very fact that traditional for loop gives more control than enhanced for loop but on the other hand enhanced or advanced for loop gives more convenience.
Labels:
core java
,
java collection tutorial
What is difference between Maven, ANT, Jenkins and Hudson?
In short, though Maven and ANT are build tools the main difference is that maven also provides dependency management, standard project layout, and project management. On the difference between Maven, ANT, and Jenkins, later is a continuous integration tool which is much more than a build tool. You can set up your CI environment using Jenkins or Hudson and automatically build, test, and deploy your Java project. Now last, the main difference between Jenkins and Hudson, both originate from the same source code but one is a closed source while the other is open source. You can read the details in this article.
Labels:
ant tutorials
,
Maven
,
programming
Sunday, September 12, 2021
Difference between OCPJP 8 Upgrade Exams 1Z0-813 and 1Z0-810? Which one to take?
Many Java developers who hold a Java certification either from Sun or Oracle and who are looking to upgrade to the latest Java version i.e. Java SE 8 certification have a common doubt, should they go for 1Z0-810 or 1Z0-813 upgrade exams? What are the differences between these two exams? Well, even though they are both upgrades to Java SE 8 exams, they are separate exams and have a different exam code, the difference is also obvious when you look at their title. The 1Z0-810 exam is known as "Upgrade Java SE 7 to Java SE 8 OCP Programme" while the 1Z0-813 exam is known as "Upgrade to Java SE 8 OCP ( Java SE 6 and all prior versions)".
Labels:
Java Certification OCPJP SCJP
5 Free OCEJWCD 6 Mock Exam 1Z0-899 Practice Test
The OCEJWCD is Oracle's version of the SCWCD (Sun Certified Web Component Developer) exam, which tests the candidate's knowledge of Servlet, JSP, and other web technology. The OCEJWCD stands for Oracle Certified Enterprise Java Web Component Developer. Like other Java certifications like the OCAJP or OCPJP, the key to success is selecting a good book, practicing code daily, and then solving as many mock questions as possible.
Labels:
Java Certification OCPJP SCJP
,
OCEJWCD
How to create LocalDateTime in Java 8 - Example Tutorial
The LocalDateTime is a new class introduced in Java 8 new Date and Time API. This class is in java.time package and it represents both date and time information without timezone. In this example, you will learn different ways to create an instance of LocalDateTime class in Java 8 like by using the static factory method, or by combining LocalDate and LocalTime instances together, which are subsequently used to denote date without time and time without the date in Java 8. As their name suggests they are local, so they don't contain timezone information.
Labels:
date and time tutorial
,
Java 8
Saturday, September 11, 2021
3 ways to loop over Set or HashSet in Java? Examples
Since Set interface or HashSet class doesn't provide a get() method to retrieve elements, the only way to take out elements from a Set is to iterate over it by using the Iterator, or loop over Set using advanced for loop of Java 5. You can get the iterator by calling the iterator() method of the Set interface. This method returns an iterator over the elements in the sets but they are returned in no particular order, as Set doesn't guarantee any order. Though individual Set implementations e.g. LinkedHashSet or TreeSet can impose ordering and in such iterator will return elements on that order.
Labels:
core java
,
HashSet
,
java collection tutorial
Subscribe to:
Posts
(
Atom
)