Programmers often mistook copy constructors provided by various collection classes, as a means to clone Collection like List, Set, ArrayList, HashSet, or any other implementation. What is worth remembering is that the copy constructor of Collection in Java only provides a shallow copy and not a deep copy, which means objects stored in both original Lists and cloned List will be the same and point to the same memory location in the Java heap. One thing, which adds to this misconception is a shallow copy of Collections with Immutable Objects. Since Immutable objects can't be changed, It's Ok even if two collections are pointing to the same object. This is exactly the case of String contained in the pool, update on one will not affect the other.
Monday, September 27, 2021
How to use Stream with List and Collection in Java 8? filter + map Example Tutorial
Finally, Java 8 is here, after more than 2 years of JDK 7, we have a much expected Java 8 with lots of interesting features. Though Lambda expression is the most talked-about item of the coming Java 8 release, it wouldn't have been this much popular, if Collections were not improved and Stream API was not introduced. Java 8 is bringing on new Streams API java.util.stream package, which allows you to process elements of Java Collections in parallel. Java is inheritably sequential and there are no direct means to introduce parallel processing at the library level, stream API is going to fill that gap. By using Stream API in Java, you can filter elements of collection on a given criterion. For example, if you have a list of orders, you can filter buy orders with sell orders, filter orders based upon their quantity and price, and so on.
Labels:
core java
,
Java 8
,
programming
,
Stream API examples
How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver [Solution]
This error comes when you try to connect to the Microsoft SQL Server database from the Java program but the required JDBC driver is not available in Classpath or driver is available in CLASSPATH but the class loader is not able to find it due to classpath intricacies. Depending upon your situation, a solution could be as simple as downloading any of sqljdbc.jar, sqljdbc4.jar, or sqljdbc41.jar, based upon the Java version you are using and adding them into CLASSPATH as set CLASSPATH = %CLASSPATH%; (path to Microsoft JDBC driver) in Windows. BTW, in most of the cases "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver" comes because of classpath intricacies.
Labels:
error and exception
,
JDBC
,
Microsoft SQL Server
Thursday, September 23, 2021
Overriding equals() and hashCode() method in Java and Hibernate
Override equals and hashCode in Java
Equals and hashCode in Java are two fundamental methods that are declared in Object class and part of the core Java library. equals() method is used to compare Objects for equality while hashCode is used to generate an integer code corresponding to that object. equals and hashCode have been used extensively in Java core library like they are used while inserting and retrieving Object in HashMap, see how to get method of HashMap works in Java for the full story, equals method is also used to avoid duplicates on HashSet and other Set implementation and every other place where you need to compare Objects.
Labels:
core java
,
core java interview question
,
equals and hashcode
Wednesday, September 22, 2021
Difference between map() and flatMap() in Java 8 Stream - Example
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.
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
Subscribe to:
Posts
(
Atom
)