java lists

Accessing elements from an ArrayList is a fundamental operation in Java programming. ArrayLists are commonly used to store and manage collections of objects dynamically. Being able to retrieve elements enables you to work with the data stored within the ArrayList effectively. Whether you need to display elements, perform calculations, or manipulate the data in any…

Read More How to Get an Element from an ArrayList in Java?

In this tutorial, you will learn how to convert a JSON array to a Java list using a powerful Java library called Jackson. By the end of this tutorial, you will have a solid understanding of various methods available to achieve this conversion, allowing you to extract meaningful data from JSON arrays and leverage them…

Read More Convert JSON Array to Java List using Jackson

Want to learn how to merge two ArrayLists in Java? In this post, we will cover the following ways: Using the List.addAll() method Using the Java 8 Streams Using the ListUtils.union() method Merge two ArrayLists in Java using the List.addAll() method There is addAll() method from the List interface that appends all of the elements in…

Read More How to merge two ArrayLists in Java?

In this post, you will learn how to Serialize and Deserialize an ArrayList in Java using the FileOutputStream and FileInputStream classes. How to Serialize an ArrayList in Java? Serialization is the process of changing the state of an object into a byte stream. For example, we use it to write some Java objects into a file. In…

Read More Serialize and Deserialize an ArrayList in Java

In one of the previous examples, we covered how to sort an ArrayList in ascending order. In this post, you will learn how to sort ArrayList in descending order in Java. We will explore the following ways: Using the sort() method Using the Collections.sort() and Collections.reverse() methods Sort ArrayList in descending order in Java using the…

Read More Sort ArrayList in descending order in Java

We often need to compare two ArrayList in Java. In this post, you will learn how to do that in two ways: Using the ArrayList.equals() method Using the CollectionUtils.isEqualCollection() method Compare two ArrayList in Java using the ArrayList.equals() method The ArrayList.equals() method compares two ArrayList. It compares the sizes of the lists, elements equality, and the…

Read More Compare two ArrayList in Java

import java.util.*; public class Test { public static void main(String[] args) { List<String> names = new ArrayList<>(Arrays.asList(“Megan”, “Tom”, “Steve”, “Megan”, “Meliisa”, “Tom”, “John”)); Set<String> uniqueNames = new HashSet<>(names); // creating a Set from the List System.out.println(uniqueNames); } } Output: [Megan, Tom, Steve, John, Meliisa] Remove duplicates from a List in Java using the Java 8…

Read More Remove Duplicates from a List in Java

import java.util.*; public class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(7, 12, 5, 28, 97, 14, 2, 7, 8, 12, 19, 22)); Collections.sort(numbers); System.out.println(numbers); } } Output: [2, 5, 7, 7, 8, 12, 12, 14, 19, 22, 28, 97] Using the sort() method from ArrayList class public class Test…

Read More Sort a List in Java

public class Test { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(7, 19, 14, 1, 12, 22, 18, 3, 28, 5)); System.out.println(“Maximum value: ” + Collections.max(list)); System.out.println(“Minimum value: ” + Collections.min(list)); } } Output: Maximum value: 28 Minimum value: 1 Finding the maximum and minimum values in List using the Java 8…

Read More Find Max and Min Values of a List in Java

LinkedList<String> list = new LinkedList<>(); List<Integer> list = new LinkedList<>(); class Test { public static void main(String[] args) { List<String> list = new LinkedList<>(); list.add(“Steve”); list.add(“Megan”); list.add(“Melissa”); list.add(“Ryan”); System.out.println(list); } } Output: [Steve, Megan, Melissa, Ryan] class Test { public static void main(String[] args) { List<String> list = new LinkedList<>(); list.add(“Steve”); list.add(“Megan”); list.add(“Melissa”); list.add(“Ryan”); List<String>…

Read More LinkedList in Java

ArrayList<String> list = new ArrayList<>(); List<Integer> list = new ArrayList<>(); class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add(“Steve”); list.add(“Megan”); list.add(“Melissa”); list.add(“Ryan”); System.out.println(list); } } Output: [Steve, Megan, Melissa, Ryan]   Using the addAll() method, we can add all the elements of an existing list to the newly created…

Read More ArrayList in Java