Sunday, May 8, 2022

Java 8 forEach() Loop Example

Java 8 has introduced a new way to loop over a List or Collection, by using the forEach() method of the new Stream class. You can iterate over any Collection like List, Set, or Map by converting them into a java.util.sttream.Stream instance and then calling the forEach() method. This method performs given operation on every element of Stream, which can be either simply printing it or doing something else. Since stream can be sequential or parallel, the behavior of if this method is not deterministic if used with a parallel stream

Difference between Transient, Persistent, and Detached Objects in Hibernate

In the Hibernate framework, an entity can be in three states, transient, persistent, and detached. When an object is in a transient state, it is commonly referred to as a transient object, similarly, if it is in persistence and detached state, it is known as a persistent and detached object. When an entity is first created using the new operator like new User() and not associated with Hibernate session like you haven't called session.save(user) method then it is known as a transient object. At this stage, Hibernate doesn't know anything about this object and the object doesn't have any representation in the database like a corresponding row in the User table.

Wednesday, April 27, 2022

How to use filter + map + collect + Stream in Java? Example Tutorial

Hello guys, many of my readers emailed me to write a post about the map and filter function of Java 8 because they found it difficult to understand and use. Even though I have previously blogged about both map() and filter(), I am writing this post again to explain the concept in more layman's language for a better understanding of my readers and fellow Java developers. The map() function is a method in Stream class that represents a functional programming concept. In simple words, the map() is used to transform one object into another by applying a function. That's the reason the Stream.map(Function mapper) takes a function as an argument. For example, by using map() function, you can convert a list of String into List of Integer by applying Integer.valueOf() method to each String on the input list.

Wednesday, April 20, 2022

Top 50 SQL and Database Phone Interview Questions Answers

Database and SQL is a very important skill, not just for DBA or Database admins but also for any application developer like Java.NET, or Web developers. This is why you would often see questions from SQL and Database in Programming interviews. For DBAs, SQL is more important than a programmer, because it being their primary skill, they are also expected to know more than a common Java or .NET developer. Since no Java interview is just about Java questions, many times I receive a request from my reader about SQL questions like how to solve a particular query or some tricky questions based upon database indexes.

Monday, April 18, 2022

How to use Environment Variables in Spring Boot's application.properties file? Example Tutorial

How to use environment variable in Spring boot's application.properties is a major problem when your spring boot application running on different places like local, Jenkins, and OpenShift. So we need to make the data source file dynamic in the application.properties file. So let's have look at how to do this at how to use the env variable in Spring Boot's application.properties. So in this tutorial, We assume that you have knowledge of Spring boot and not going to talk about small details. 

Thursday, April 7, 2022

How to Create a thread-safe ConcurrentHashSet in Java 8? [Example]

Until JDK 8, there was no way to create a large, thread-safe, ConcurrentHashSet in Java. The java.util.concurrent package doesn't even have a class called ConcurrentHashSet, but from JDK 8 onwards, you can use the newly added keySet(default value) and newKeySet() methods to create a ConcurrentHashSet backed by ConcurrentHashMap in Java. This is better than old tactical solutions like using a concurrent hash map with dummy value or using the set view of the map, where you cannot add new elements. The Set returned by keySet(defaultValue) and newKeySet() methods of JDK 8 is a proper set, where you can also add new elements along with performing other set operations like contains(), remove() etc.

Wednesday, March 30, 2022

How to Read JSON String in Java using json-simple library? Example Tutorial

Hello guys, if you are wondering how to read JSON in Java using the json-simple library and looking for an example then you have come to the right place. Earlier, I have shown you how to parse JSON using Jackson and how to read JSON using the Gson library in Java, and today, I am going to share how to read JSON strings using json-simple, another popular JSON library in Java. If you don't know, JSON is a text format that is widely used as a data-interchange language because its parsing and its generation are easy for programs. It is slowly replacing XML as the most powerful data interchange format, as it is lightweight, consumes less bandwidth, and is also platform-independent.  

Saturday, March 26, 2022

How to send JSON via POST request using RestTemplate in Spring | PostForObject, PostForEntity, and PostLocation Example in Java

Hello guys, if you are wondering how to send a Post request with Json in Java application using Spring framework then you have come to the right place. Earlier, I have shown how to consume JSON from a RESTful API and In this tutorial, we are going to discuss how to send POST requests via RestTemplate in JSON. We will have an example project which is having POST API and then test it by sending request body along with request headers using postEntry(). POST requests can be made using the RestTemplate class's postForObject(), postForEntity(), and postForLocation() template methods. The first two techniques are fairly similar to what I covered in the GET request lesson for RestTemplate. Instead of returning the whole resource, the last method returns the location of the newly formed resource.

Wednesday, March 16, 2022

Spring Boot + Redis Example in Java

Hello guys, if you are wondering how to use Redis with Spring Boot in Java then you have come to the right place. Earlier, I have shared the Spring Boot + React example, Spring Boot + Angular example, and Spring Boot + MyBatis examples In this tutorial, we will review the basics of how to use Redis with Spring Boot. We will build an application that demonstrates how to perform CRUD operations Redis through a web interface. So before going into in detailed project example, let's move with the what is spring Redis. As Java developers, we need to talk about the data layer, as we can't develop an insightful application without the use of CRUD operations. 

Saturday, February 12, 2022

How to delete a key value pair from a HashMap during Iteration in Java - Example tutorial

Suppose you have a Map or Dictionaries like HashMap or Hashtable, which contains key-value pairs like books and their prices, and you want to delete all books whose prices are greater than 40 USD, how do you that in Java? This is one of the most common scenarios while developing Java applications and many Java programmers, will say that they will iterate over Map and check each entry and then use the remove(Object key) or remove(Object key, Object value) methods from java.util.Map to delete any mapping where the value is greater than 40 USD. Though the approach is right, the answer is wrong.

Thursday, February 10, 2022

Top 30 Java Phone Interview Questions Answers for Freshers, 1 to 2 Years Experienced

Hello guys and ladies, in this article, I am sharing 30 core Java technical questions, from screening and phone rounds of interviews. In telephonic interviews, questions are short, fact-based and Interviewer expects a to-the-point reply and some keywords in answers. Accordingly, I have given very short answers to all these questions, only the main points; just to make this a revision post and not the main source for preparation. For thorough preparation, my master list of 140+ Java questions is a better one to start with. I am also expecting every Java programmer to know answers to all these Java technical questions if he has more than 4 to 5 years of experience. It's only freshers and junior developers who need to do a bit of research to understand topics well.

Tuesday, February 8, 2022

Difference between SOAP and RESTful Web Service in Java

Though both SOAP and RESTful web services allow a client to query the server for some information, the way they are implemented and used is quite different. The main difference between SOAP and REST is that the former provides a standard of communication between client, server, and other parties and has restricted a set of rules and format, while REST leverages the ubiquity of HTTP protocol, in both client and servers, to allow them to communicate with each other regardless of their implementation. In short, getting data from a RESTful web service requires less headache than getting data from a SOAP web service.

3 Ways to Read File line by line in Java 8? Examples

Java 8 has added a new method called lines() in the Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read the first 100 lines then the rest of the lines will not be loaded into memory, which results in better performance. This is slightly different from the Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream like forEach(), count(), etc. By using the count() method you can actually count a number of lines in files or the number of empty lines by filtering empty lines.

Sunday, February 6, 2022

2 Examples to read Zip Files in Java, ZipFile vs ZipInputStream - Tutorial

ZIP format is one of the most popular compression mechanisms in the computer world. A Zip file may contain multiple files or folders in compressed format.  Java API provides extensive support to read Zip files, all classes related to zip file processing are located in the java.util.zip package. One of the most common tasks related to zip archive is to read a Zip file and display what entries it contains, and then extract them in a folder. In this tutorial, we will learn how to do this task in Java. There are two ways you can iterate over all items in a given zip archive, you can use either java.util.zip.ZipFile or java.util.zip.ZipInputStream. Since a Zip file contains several items, each of them has a header field containing the size of items in a number of bytes. This means you can iterate all entries without actually decompressing the zip file.

Friday, February 4, 2022

How to access Private Field and Method Using Reflection in Java? Example Tutorial

Reflection in Java is a very powerful feature and allows you to access private methods and fields which is not possible by any other means in Java and because of this feature of reflection many code coverage tools, static analysis tools,s and Java IDE like Eclipse and Netbeans has been so helpful. In the last article, we have seen details about private keywords in Java and learned why we should always make fields and methods private in Java. There we have mentioned that private fields and methods are only accessible in the class they are declared but with reflection, you can call the private method and access private fields outside the class. In this article, we will see a simple example of accessing a private field using reflection and invoking a private method using reflection in Java.