Top 11 Collection API Tricks in JAVA

Java: Top 11 Collection API Tricks

Java’s Collection API is robust and offers a multitude of methods to manipulate data. Here are the top 11 tricks you can use to get the most out of the Collection API.

1. Creating Immutable Collections

Java provides convenient factory methods to create immutable collections. This is useful when you want to create a collection that should not be modified.

List<String> list = List.of("Java", "Python", "C++");
Set<String> set = Set.of("Java", "Python", "C++");
Map<String, Integer> map = Map.of("Java", 10, "Python", 20, "C++", 30);

Output:

list = [Java, Python, C++]
set = [Python, C++, Java]
map = {Python=20, C++=30, Java=10}

2. Using the copyOf Method

The copyOf method creates a new collection that is a copy of an existing collection.

List<String> original = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
List<String> copy = List.copyOf(original);

Output: copy = [Java, Python, C++]

3. Using the removeIf Method

The removeIf method removes all elements from a collection that satisfy a given predicate.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
languages.removeIf(lang -> lang.startsWith("C"));

Output: languages = [Java, Python]

4. Using the replaceAll Method

The replaceAll method replaces each element of a list with the result of applying a function to that element.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
languages.replaceAll(String::toUpperCase);

Output: languages = [JAVA, PYTHON, C++]

5. Using the sort Method

The sort method sorts a list according to the order induced by a Comparator.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
languages.sort(Comparator.naturalOrder());

Output: languages = [C++, Java, Python]

6. Using the forEach Method

The forEach method performs an action for each element of a collection.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
languages.forEach(System.out::println);

Output:

Java
Python
C++

7. Using the spliterator Method

The spliterator method creates a Spliterator over the elements in a collection.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
Spliterator<String> spliterator = languages.spliterator();

Output: spliterator = java.util.ArrayList$Itr@15db9742

8. Using the stream and parallelStream Methods

The stream and parallelStream methods return a sequential or parallel Stream respectively with the collection as its source.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
Stream<String> stream = languages.stream();
Stream<String> parallelStream = languages.parallelStream();

Output:

stream = java.util.stream.ReferencePipeline$Head@6d06d69c
parallelStream = java.util.stream.ReferencePipeline$Head@7852e922

9. Using the toArray Method

The toArray method returns an array containing all elements in a collection.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
String[] array = languages.toArray(new String[0]);

Output: array = [Java, Python, C++]

10. Using the contains Method

The contains method returns true if a collection contains a specified element.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
boolean containsJava = languages.contains("Java");

Output: containsJava = true

11. Using the isEmpty Method

The isEmpty method checks if a collection is empty or not.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "Python", "C++"));
boolean isEmpty = languages.isEmpty();

Output: isEmpty = false

These are just a few of the many powerful features available in the Collection API. #ni18blogs

Comments

Popular posts from this blog

GTM for SFCC

Java Interview Questions

JavaScript and Deployment