top of page
Interesting Recent Posts :
Writer's pictureRajan Chopra

Java 17 :Collections



Java 17 comes with a rich set of collection classes that allow you to store, manipulate and retrieve data efficiently. These collection classes are part of the Java Collections Framework (JCF), which provides a unified architecture for representing and manipulating collections of objects. In this answer, I will provide a brief introduction to some of the commonly used collection classes in Java 17 along with an example for each.

  1. ArrayList ArrayList is a resizable array implementation of the List interface. It provides dynamic resizing, which means that it can increase or decrease in size as needed. ArrayList allows you to add, remove and access elements at a specific position.

Example:


import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        
        System.out.println("The names are: " + names);
        
        names.remove("Charlie");
        
        System.out.println("After removing Charlie, the names are: " + names);
        
        System.out.println("The name at index 1 is: " + names.get(1));
    }
}

Output:


The names are: [Alice, Bob, Charlie]
After removing Charlie, the names are: [Alice, Bob]
The name at index 1 is: Bob
  1. HashSet HashSet is an implementation of the Set interface that uses a hash table to store elements. It doesn't allow duplicate elements and the order of the elements is not guaranteed. HashSet provides constant-time performance for the basic operations (add, remove, contains and size) assuming a good hash function.

Example:


import java.util.HashSet;

public class HashSetExample {
    public static void main(String[] args) {
        HashSet<String> names = new HashSet<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        
        System.out.println("The names are: " + names);
        
        names.remove("Charlie");
        
        System.out.println("After removing Charlie, the names are: " + names);
        
        System.out.println("Does the set contain Alice? " + names.contains("Alice"));
    }
}

Output:


The names are: [Bob, Charlie, Alice]
After removing Charlie, the names are: [Bob, Alice]
Does the set contain Alice? true
  1. HashMap HashMap is an implementation of the Map interface that uses a hash table to store key-value pairs. It provides constant-time performance for the basic operations (put, get, remove and size) assuming a good hash function. HashMap doesn't maintain any order of the keys.

Example:


import java.util.HashMap;

public class HashMapExample {
    public static void main(String[] args) {
        HashMap<Integer, String> students = new HashMap<>();
        students.put(1, "Alice");
        students.put(2, "Bob");
        students.put(3, "Charlie");
        
        System.out.println("The students are: " + students);
        
        students.remove(3);
        
        System.out.println("After removing Charlie, the students are: " + students);
        
        System.out.println("The name of student with id 2 is: " + students.get(2));
    }
}

Output:


The students are: {1=Alice, 2=Bob, 3=Charlie}
After removing Charlie, the students are: {1=Alice, 2=Bob}
The name of student with id 2 is: Bob

These are just a few examples of the collection classes available in Java 17. There are many other collection classes available such as TreeSet, TreeMap.

12 views

Recent Posts

See All

Machine Learning : KNN model

Introduction to K-Nearest Neighbors (KNN) Algorithm in Machine Learning: K-Nearest Neighbors (KNN) is one of the simplest and most...

Comentarios


bottom of page