Collections Class in Java Is One of The Utility Classes in Java
Collections Class in Java Is One of The Utility Classes in Java
Java Course Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithrea
Example 1: Here, we will use ArrayList, which is a class from the Java
Collections framework. It allows to store elements in a list by
maintaining the insertion order and also allows duplicates.
Java
1 import java.util.ArrayList;
2
3 public class Geeks {
4 public static void main(String[] args) {
5
6 // Create an ArrayList to store elements
7 ArrayList<String> al = new ArrayList<>();
8
9 // Add elements to the list
10 al.add("Apple");
11 al.add("Banana");
12 al.add("Apple"); // Duplicates are allowed
13
14 System.out.println("" + al);
15 }
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 1/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
16 }
Output
1. ArrayList
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 2/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Syntax:
2. Vector
Syntax:
3. Stack
Syntax:
4. LinkedList
Syntax:
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 3/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
5. HashSet
Syntax:
6. LinkedHashSet
Syntax:
7. TreeSet
Syntax:
8. PriorityQueue
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 4/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Syntax:
9. ArrayDeque
Syntax:
10. HashMap
Syntax:
11. EnumMap
Syntax:
12. AbstractMap
Syntax:
13. TreeMap
Syntax:
Now let us do discuss methods that are present inside this class so that
we can use these inbuilt functionalities later on in our program. Below
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 6/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
are the methods have been listed below in a tabular format as shown
below as follows:
Methods Description
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 7/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Methods Description
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 8/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Methods Description
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 9/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Methods Description
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 10/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Methods Description
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 11/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Methods Description
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 12/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Methods Description
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 13/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Methods Description
list.
Now, we have listed all the methods, It is clear how important they are
in writing optimized Java code. The Collections class is widely used and
its methods appear in almost every optimized Java program. Here, we
will implement these methods also discuss their operations.
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 14/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Example:
Java
1 // Adding Elements
2 // Using addAll() method
3
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.List;
7
8 class Geeks {
9
10 public static void main(String[] args) {
11
12 List<String> l = new ArrayList<>();
13
14 // Adding elements to the list
15 l.add("Shoes");
16 l.add("Toys");
17
18 // Add one or more elements
19 Collections.addAll(l, "Fruits", "Bat",
"Ball");
20
21 for (int i = 0; i < l.size(); i++) {
22 System.out.print(l.get(i) + " ");
23 }
24 }
25 }
Output
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 15/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
2. Sorting a Collection
Example:
Java
33 Collections.sort(l,
Collections.reverseOrder());
34
35 // Printing the reverse order
36 for (int i = 0; i < l.size(); i++) {
37 System.out.print(l.get(i) + " ");
38 }
39 }
40 }
Output
3. Searching in a Collection
Example:
Java
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 17/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Output
4. Copying Elements
The copy() method of Collections class is used to copy all the elements
from one list into another. After the operation, the index of each copied
element in the destination list will be identical to its index in the source
list. The destination list must be at least as long as the source list. If it is
longer, the remaining elements in the destination list are unaffected.
Example:
Java
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 18/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 19/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Output
5. Disjoint Collection
Example:
Java
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 20/26
12/24/24, 7:15 PM Collections Class in Java - GeeksforGeeks
Output
true
Similar Reads
Difference between Traditional Collections and Concurrent…
https://www.geeksforgeeks.org/collections-class-in-java/?ref=next_article 21/26