Collections in Java
Bola Adebesin
Posted on July 18, 2021
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);
}
}
}
Despite the order of the strings added being:
- watermelon
- banana
- cherry
- pear
- cantaloupe
- blackberry
- ginseng
- strawberry
- iris
- 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
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);
}
}
}
The result of the above code is:
0:Hello World
1:Goodbye Mars
2:Salutations Venus
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);
}
}
}
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
Bonus:
In the three examples above, the following syntax is used:
for(String text: set){
System.out.println(text);
}
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);
}
Posted on July 18, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.