DEV Community

Bola Adebesin
Bola Adebesin

Posted on

Collections in Java

Collections

In Java, a collection is a class whose main purpose is to store a collection(or group) of other elements. This could be a group of integers, strings, boolean values, objects, etc.

Collections are divided into 3 major groups: Set, List, and Map.

Set

An unordered group of elements, each. element in a collection is unique:

import java.util.HashSet;

public class Solution {
    public static void main(String[] args) throws Exception {
        HashSet<String> set = new HashSet<String>(); 
        set.add("watermelon"); 
        set.add("banana"); 
        set.add("cherry"); 
        set.add("pear"); 
        set.add("cantaloupe"); 
        set.add("blackberry"); 
        set.add("ginseng"); 
        set.add("strawberry"); 
        set.add("iris"); 
        set.add("potato"); 

        for(String word: set){
            System.out.println(word); 
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

Despite the order of the strings added being:

  1. watermelon
  2. banana
  3. cherry
  4. pear
  5. cantaloupe
  6. blackberry
  7. ginseng
  8. strawberry
  9. iris
  10. potato

When the System.out.println method is called on each element in the set (using a for each shorthand), this is the order that the words are printed:

banana
cherry
pear
iris
blackberry
ginseng
potato
strawberry
watermelon
cantaloupe
Enter fullscreen mode Exit fullscreen mode

The order is different.

Lists

In a list, each element is organized by an index. (A familiar example of a list is an array). Because each element is associated with a number, it is an ordered collection and one can access a specific element if one knows the correct index. It's kind of like looking for your friend when you have their address, it is much easier to locate them when you have this information that identifies their location.

import java.util.List;
import java.util.ArrayList; 

public class Solution {
    public static void main(String[] args) throws Exception {
    ArrayList<String> list = new ArrayList<String>(); 
        list.add("Hello World"); 
        list.add("Goodbye Mars"); 
        list.add("Salutations Venus"); 


        for(String word: list){
            System.out.println(list.indexOf(word) + ":" + word); 
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

The result of the above code is:

0:Hello World
1:Goodbye Mars
2:Salutations Venus
Enter fullscreen mode Exit fullscreen mode

Maps

Maps are a group of key-value pairs. Unlike lists, in which each element is identified by an integer beginning from zero, maps can be identified by name. In maps, each key must be unique, but the values do not have to be unique.

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public static void main(String[] args) throws Exception {
        HashMap<String, String> map = new HashMap<String, String>(); 
        map.put("K-Drama", "This is my first life"); 
        map.put("Drama", "Start Up"); 
        map.put("Anime", "One Piece"); 
        map.put("Comedy", "Grownish"); 
        map.put("Animation", "Bob's Burgers");
        map.put("Romance", "Pride and Prejudice"); 


        for(Map.Entry<String, String> pair: map.entrySet()){
            String key = pair.getKey(); 
            String value = pair.getValue(); 

            System.out.println(key + " : " + value); 
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

The output of the above code is:

Anime : One Piece
Drama : Start Up
K-Drama : This is my first life
Animation : Bob's Burgers
Romance : Pride and Prejudice
Comedy : Grownish
Enter fullscreen mode Exit fullscreen mode

Bonus:

In the three examples above, the following syntax is used:

for(String text: set){
  System.out.println(text); 
}
Enter fullscreen mode Exit fullscreen mode

This is Java shorthand for iterating through collections. It allows us to iterate through these groups of elements by using an implicit iterator. The longer version of this looks like"

Iterator<String> iterator = set.iterator(); 
while(iterator.hasNext()) // Check if there is another element 
{
   //Get the current element and move to the next line 
    String text = iterator.next(); 
    System.out.println(text); 
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)