Java Collections summary

Let’s start from types.
Two major types in java world: primitive types and reference types. (I did spend some time to figure out reference type, object, types those concepts in Java and how they can compare to points)
To hold primitive types, we can use arrays, like int[] i = new int[5];
Or we can use containers. And we further divide container into two categories: collection and map.
And as you know, a figure saves a thousand word.
Figures

Limitation of Arrays: if we define

int[] a = new int[5];

first we can use .length to retrieve the size of a; second we can use “[]” operator to access this array a.

What if we would like to put more than 5 elements into a, can we achieve this by a = new int[10]? Answer it is NO.
So to harvest the pleasure of dynamically expanding the size of object holder, Collection & Map come into play.
Basically you can image that you can put any amount of same reference into the containers without significantly harming the container’s performance. ( I did not compare the performance of array and containers here. )
Q: what is the most efficient way of holding a group of objects? 
Now it is time to talk about containers: List, Queue, Set and Map.
1, List, Queue, Set and Map:
List, Queue and Set are different from Map by: we put key-value pair into a map.
I don’t know if you buy into this explanation: for List/Queue/Set, we can access each element by index (and index is normally integer number). for map, we access the value by key.
For example I am writing down the activity schedule for myself from Monday to Wednesday:
public static void main(String[] args) {
 List<String> lsact = new ArrayList<String>();
 lsact.add("Yoga");
 lsact.add("Jogging");
 lsact.add("Biking");
 Map<String, String> mapact = new HashMap<>();
 mapact.put("Monday", "Yoga");
 mapact.put("Tuesday", "Jogging");
 mapact.put("Wednesday", "Biking");
 
 System.out.println(lsact);
 System.out.println(mapact);
 }
 The output looks like:

[Yoga, Jogging, Biking]
{Monday=Yoga, Wednesday=Biking, Tuesday=Jogging}
Guess you can see the list is not sufficient to express enough information.

Leave a comment