How-to articles, tricks, and solutions about COLLECTIONS

Convert JSON to Map

You can use the Gson library to convert a JSON string to a Map in Java.

Converting 'ArrayList<String> to 'String[]' in Java

You can use the toArray() method of the ArrayList class to convert an ArrayList of strings to a string array. Here's an example:

Easiest way to convert a List to a Set in Java

To convert a List to a Set in Java, you can use the new HashSet<>(list) constructor to create a new HashSet and initialize it with the elements of the List.

get string value from HashMap depending on key name

To get the string value from a HashMap depending on the key name in Java, you can use the get() method of the Map interface.

Getting an element from a Set

A Set is a collection of elements in which each element can only occur once. A Set does not have a specific order and does not provide a way to access its elements by their position.

How can I convert List<Integer> to int[] in Java?

You can use the toArray() method of the List interface to convert a List<Integer> to an int[] in Java.

How can I initialise a static Map?

To initialize a static Map in Java, you can use the Map interface's of method, which was added in Java 9. The of method creates an immutable Map with a fixed set of key-value pairs.

How can I sort a List alphabetically?

To sort a List alphabetically in Java, you can use the Collections.sort method and pass in your List as an argument.

How can I turn a List of Lists into a List in Java 8?

You can use the flatMap method from the Stream API to turn a List&lt;List&lt;T&gt;&gt; (a list of lists) into a List&lt;T&gt; in Java 8. Here's an example of how you can do this:

How do I convert a Map to List in Java?

You can use the entrySet() method of the Map interface to get a Set view of the mappings contained in the map, and then create a List from this set using the new

How do I efficiently iterate over each entry in a Java Map?

There are several ways to iterate over the entries in a Map in Java. Here are some options:

How do I remove repeated elements from ArrayList?

To remove repeated elements from an ArrayList in Java, you can use the removeAll method and pass it a Collection containing the elements to be removed.

How to clone ArrayList and also clone its contents?

To clone an ArrayList and also clone its contents, you can use the clone method of the ArrayList class, which creates a shallow copy of the list.

How to convert a Collection to List?

In Java, you can convert a Collection (such as Set, Queue, etc.) to a List using the following methods:

How to convert an Array to a Set in Java

To convert an array to a Set in Java, you can use the Arrays.asList() method to create a List from the array, and then use the List.toSet() method to create a Set from the List.

How to convert comma-separated String to List?

To convert a comma-separated String to a List in Java, you can use the String.split() method to split the string into an array of substrings, and then use the Arrays.asList() method to create a list from that array.

How to convert int[] into List<Integer> in Java?

To convert an int[] array into a List<Integer> in Java, you can use the Arrays.stream() method to create a stream of the array, and then use the mapToObj() method to map each element of the stream to an Integer object.

How to count the number of occurrences of an element in a List

To count the number of occurrences of an element in a List in Java, you can use the Collections.frequency(Collection, Object) method, which returns the number of times the specified element appears in the collection.

How to directly initialize a HashMap in Java?

To directly initialize a HashMap in Java, you can use the put() method to add elements to the map.

How to filter a Java Collection (based on predicate)?

To filter a Java collection based on a predicate, you can use the removeIf method of the Collection interface. This method removes all elements from the collection that match the specified predicate.

How to get the first element of the List or Set?

To get the first element of a List or Set in Java, you can use the following methods:

How to Initialization of an ArrayList in one line in Java

You can initialize an ArrayList in one line in Java using the following syntax:

How to initialize HashSet values by construction?

To initialize the values of a HashSet when constructing the set, you can use one of the HashSet constructors that takes a Collection as an argument.

How to make a new List in Java

In Java, you can create a new list using the java.util.ArrayList class or the java.util.LinkedList class.

How to sort a List/ArrayList?

To sort a List or ArrayList in Java, you can use the sort method of the Collections class from the java.util package. The sort method takes a List and sorts it in ascending order according to the natural ordering of its elements.

1 2