CodeToLive

Collections in Java

The Java Collections Framework provides a set of classes and interfaces for storing and manipulating groups of objects. It includes implementations of lists, sets, maps, and more, making it easier to work with collections of data.

List

A List is an ordered collection that allows duplicate elements. Common implementations include ArrayList and LinkedList.


import java.util.*;

public class Main {
    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        // Iterate using for-each loop
        for (String fruit : list) {
            System.out.println(fruit);
        }

        // Access elements by index
        System.out.println("First element: " + list.get(0));
    }
}
      

Set

A Set is a collection that does not allow duplicate elements. Common implementations include HashSet, LinkedHashSet, and TreeSet.


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Set set = new HashSet<>();
        set.add("Apple");
        set.add("Banana");
        set.add("Apple");  // Duplicate, will not be added

        // Iterate using for-each loop
        for (String fruit : set) {
            System.out.println(fruit);
        }
    }
}
      

Map

A Map stores key-value pairs and does not allow duplicate keys. Common implementations include HashMap, LinkedHashMap, and TreeMap.


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Map map = new HashMap<>();
        map.put("Apple", 1);
        map.put("Banana", 2);
        map.put("Cherry", 3);

        // Iterate using entrySet
        for (Map.Entry entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // Access value by key
        System.out.println("Value for 'Apple': " + map.get("Apple"));
    }
}
      

Iterators

An Iterator is used to traverse through a collection. It provides methods like hasNext() and next() to iterate over elements.


import java.util.*;

public class Main {
    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
      

Sorting Collections

Collections can be sorted using the Collections.sort() method or by implementing the Comparable or Comparator interfaces.


import java.util.*;

public class Main {
    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add("Banana");
        list.add("Apple");
        list.add("Cherry");

        // Sort using Collections.sort()
        Collections.sort(list);

        for (String fruit : list) {
            System.out.println(fruit);
        }
    }
}
      

Best Practices

Back to Tutorial