java examples

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

Want to learn how to count character occurrences in Java String? Below is the program that uses a Map to store chars as keys and the number of occurrences as values. class Test { public static void main(String[] args) { String str = “moiujjmhrqdoasdsooqavvae”; Map<Character, Integer> map = new HashMap<>(); for (int i = 0;…

Read More Count Character occurrences in Java String

In this post, you will learn to check if a String contains only digits in Java. We will cover two methods: Using the Character.isDigit() method Using the Integer.parseInt() method Check if a String contains only digits in Java using the Character.isDigit() method In this example, we use a for loop to iterate over the elements…

Read More Check if a String contains only digits 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

public class Test { public static void main(String[] args) { Integer[] array = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // reverse an array Integer[] reversedArray = reverse(array); // print the elements Arrays.stream(reversedArray).forEach(item -> System.out.print(item + ” “)); } public static Integer[] reverse(Integer[] array) { List arrayList = Arrays.asList(array); Collections.reverse(arrayList); return…

Read More Reverse an Array 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(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); System.out.println(numbers.stream().mapToInt(Integer::intValue).average().getAsDouble()); } } Output: 5.5   Here, we used the mapToInt(Integer::intValue) to convert the Stream<Integer> which is the result of the stream() method, to IntStream. import java.util.*; public class…

Read More Find the Average of Numbers in Java