How do I save a String to a text file using Java ?

We suppose that we have a text and we want to save this text inside a file using Java. To do this we can use the PrintWriter , lets see the following solution : Then, write your String to it, just like you would to any output stream : Be sure to call out.close() when […]

En Lire Plus

JAX-RS : Read Response Body from a Post Request

To read a Response body from a Post Request , JAX-RS provide response.readEntity(String.class). The next example is tested with Jersey 3.x Below is a JAX-RS POST request endpoint to accept a JSON input and return a JSON response. So the question here is how to read the response body from a post request ? Solution […]

En Lire Plus

java.lang.OutOfMemoryError : How to simulate in Java

In this Topic we will try to simulate the java.lang.OutOfMemoryError !! We will create an object holding 1MB of bytes, add it into a List, and repeat the create and add process until the JVM throw OutOfMemoryError. java.lang.OutOfMemoryError: Java heap space This example will run a while then it will throws java.lang.OutOfMemoryError. The Output : […]

En Lire Plus

Hello World Java Swing example

In This post we will show you and example of Hello World using Java Swing . We will configures a JFrame and attach a JLabel to display a “hello world” centered in the JLabel component. The output : Also read : Java Basics

En Lire Plus

Convert Instant to ZonedDateTime in JAVA

In this tutorial , we will show a Java 8 example of how to convert from Instant to ZonedDateTime So the output will be : If we need to convert from ZonedDateTime to Instant The output will be : Also read : How to convert String to Date in Java ? References : JavaDoc mkYong

En Lire Plus

(SOLVED) convert String to Date in Java ?

In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners Always find this problem of Date conversion. so we decided to post this solution !! let understand how to convert String Date in JAVA. java.text.SimpleDateFormat provide many date and time patterns : Letter Description Examples y Year 2013 […]

En Lire Plus

(SOLVED) How to get file extension in Java ?

In this tutorial , we will see how we can get the extension of file in Java. In the next example , we will use lastIndexOf to get the file extension of a file. The above process, will fail if the directory name or file path includes a dot or double dots AND the file […]

En Lire Plus

(SOLVED) how to Convert File to Path in Java ?

In this tutorial , we will see how we can convert file to path using Java ? The java.nio.file package includes Java virtual machine interfaces and classes for accessing files, file attributes, and file systems. Convert File to Path If we want to convert from java.io.File to java.nio.file.Path we can use toPath() Convert Path to […]

En Lire Plus

JAVA Map , HashMap , LinkedHashMap, TreeMap

Java Map : There are no duplicate values allowed in the map. A map object’s keys must be unique. HashMap, TreeMap, and LinkedHashMap are three groups that implement the Map interface in the Java collection system. SortedMap is a special interface for keeping all of the elements sorted. HashMap vs LinkedHashMap The ordering of the […]

En Lire Plus

Different ways iterate a list in Java

Iterate a list in Java : in this Post we will discover different ways to browse a list in Java. (ArrayList) List.toString() If we want to iterate over a list in java , we can do that by converting the list to a string using the toString() function, then outputting it Output : Using For […]

En Lire Plus

Difference between ArrayList and LinkedList in Java

ArrayList vs LinkedList : Both ArrayList and LinkedList implement the List interface, but what’s the difference then ? The main difference between ArrayList and LinkedList is in ArrayList which is implemented using an extensible array. As more items are added to ArrayList, its size dynamically increases. Its elements can be accessed directly using the get() […]

En Lire Plus

Comparator example Java 8 Lambda expression

We’ll show you how to write a Comparator to sort a List using Java 8 Lambda expressions in this example. Classic Comparator example Lambda expression equivalent. Sort without Lambda A comparison of Developer objects based on their age is an example. Normally, you use Collections.sort and pass an anonymous Comparator class like this : Output […]

En Lire Plus

(SOLVED) Minimum number of increment

Minimum number of increment, We’ve been given an array of n components. You can choose any one element and increase the rest of the n-1 elements by one at each operation. You must make all elements equal and replicate the process as many times as you like. Determine the smallest number of operations required. Examples: […]

En Lire Plus

forEach Java 8 Examples

To loop or iterate a Map, List, Set, or Stream in Java 8, we can use the new forEach method. Loop a Map 1,1 A standard method for looping a Map is shown below. 1.2 We can use forEach in Java 8 to loop through a Map and print out its entries. Output 1.3 The […]

En Lire Plus

Consumer Java 8 Examples, how to use ?

In Java 8, Consumer is a functional interface, it takes an argument and returns nothing. An interface with just one abstract method is referred to as a functional interface. They are limited to displaying only one type of feature. Lambda expressions can be used to describe a functional interface instance starting with Java 8. Some examples of […]

En Lire Plus

Tips to Learn Programming Faster

Tips to Learn Programming Faster The most frequently asked questions today are, “How long does it take to learn Java?” “How can I learn to code quickly and kick-off my career as a software developer?” Not only the freshmen but also a majority of coding auto-didacts are also in search of a quick and straightforward […]

En Lire Plus

JAVA : example of Arrays.sort()

Arrays.sort(): The class java.util.Arrays have many methods, one of these methods is the sort(), this method doesn’t return any value ! Syntax : In the next example we will show you how to sort an array of integers in ascending order ? Output : read also : How to find duplicate elements in a Stream […]

En Lire Plus

Multithreading in Java

Multithreading is a Java mechanism that allows concurrent execution of many block of a program for maximum utilization of CPU. Each block is called a thread. So, threads are light-weight processes within a process. To create Threads we have two ways:1. Extending the Thread class2. Implementing the Runnable Interface Example of extending the Thread class […]

En Lire Plus

Thread Class vs Runnable Interface in JAVA

Thread Class vs Runnable Interface ? To create a Thread we can extends Thread Class or we can implements Runnable Interface , so what is the difference between the two ways ? Answer 1. When we extend the Thread class, we cannot extend any other class because Java doesn’t support multiple inheritance. But, for the […]

En Lire Plus

How to use Synchronized in Java ? (Example)

Synchronized in Java : To make sure that only one Thread can access to the resource at a given point of time, we use Synchronized !! In many time, Multi-threaded programs provide a situation where multiple threads try to access the same resources and finally produce erroneous result ! To solve this problem , Java provides […]

En Lire Plus