My Journey To Java 003 — Array, list collection, set collection and map collection

Islam Taha
4 min readJan 30, 2019

Hello, my friends. Today we have a new journey to Java. Let’s say that we wanna to store names of students and there grides. How could we do that? hmm, we can create a variable for each name and a variable for each grade, but this is a stupid solution :D and there is always a smarter solution. To do this simple task we can start talking about the collection of data.

1- Array:

An array is a container that stores multiple values of the same data type. To declare an array of an integer does int arr_var[] = new int[]; but we have a fixed size of this array. Here is a full example of creating an array with different ways and access them

public static void main(String[] args) {
int[] arr_1 = new int[5];
arr_1[0] = 1;
arr_1[1] = 2;
arr_1[2] = 3;
arr_1[3] = 4;
arr_1[4] = 5;
int arr_2[] = new int[]{1, 2, 3, 4, 5}; int[] arr_3 = new int[]{1, 2, 3, 4, 5}; for (int i =0; i < arr_1.length; i++){
System.out.println(arr_1[i]);
System.out.println(arr_2[i]);
System.out.println(arr_3[i]);
}
}

Here we create an array with 5 elements each of them is int then we assign a value for those elements. Each element has an index and it starts from 0 like string i.e, you can access the first element by doing arr_1[0] and access the second element by doing arr_1[1] and so on. Did you notice arr_1.length? As u understood, It returns the length of the array.

Try to create a string array and store your friends’ names in it then print all elements of this array.

Let me tell you a magic thing about for loop and the arrays ... check the following code:

public static void main(String[] args) {
int magic[] = new int[]{1, 2, 3, 4, 5};
for (int x : magic){
System.out.println(x);
}
}

as you can see here, we declared a variable x and loop over all elements in the array without using the index of the element.

2-Collections:

What is it? Collection framework defines several classes and interfaces to represent a group of objects as a single unit.

Why do we need it? To store and retrieve a group of data in a single unit

There are three major collections we will talk about here.

  • ArrrayList from the list collection.
  • HashSet from the set collection.
  • HashMap and HashTable from the Map collection.

2.1- List Collections:

List collection allows us to store any number of data and provide too many functions to make different tasks of this data. We can add a new element or remove an element at any index. We can get the index of a specific element and we can insert an element between two elements and many many other functions we can use. List collection has ArrayList, LinkedList and Vector. We are going to talk about ArrayList.

2.1.1- ArrayList:

Read the following example carefully to be familiar with the ArrayList

public static void arrayList(){
ArrayList<String> arrList_obj = new ArrayList<String>();
//add method
arrList_obj.add("0xIslamTaha");
arrList_obj.add(1, "Add me at index 1");
//get method
String index_1 = arrList_obj.get(1); //returns the value at index 1
System.out.println(index_1);
//Check if the list has this element or not
boolean chk = arrList_obj.contains("0xIslamTaha"); // return true
System.out.println(chk);
//get list size
System.out.println(arrList_obj.size());
//chck if list is empty
System.out.println(arrList_obj.isEmpty());

}

We created an ArrayList, add elements to it. note(there is no need to define the size like in Array). We check if it has specific elements and get an element in the specific position and get the size of the list and at the end, we checked if the list is empty or not. There are more functions and you can check them by yourself.

2.2- Set Collection

Set collection help us to store values but not duplicate ones. There are HashSet, TreeSet and LinkedHashSet. We will discuss the HashSet.

2.2.1 HashSet:

The following example will help us to understand than talking :D

static void hashSet(){
HashSet<String> hashSet_var = new HashSet<String>();
// add new element
hashSet_var.add("0xIslamTaha");

// try to add a duplicate element
hashSet_var.add("0xIslamTaha");
System.out.println(hashSet_var);
// print length of it
System.out.println(hashSet_var.size());

// remove an element
System.out.println(hashSet_var.remove("0xIslamTaha"));
}

output:

[0xIslamTaha]
1
true

You can see here we only be able to add one 0xIslamTaha and all the duplicate values will be ignored.

2.3- Map Collection

hmm, what if we need to store the student name and his math grad? Do we create a two list on for the names and another for the grides and link them by the index hmm, I prefer a smarter solution. Here we can introduce the Map. Map let us create a collection with key:value. Let's see the code

static void hashMap(){
// define it
HashMap<String, Integer> students = new HashMap<String, Integer>();

// put elements
students.put("0xIslamTaha", 100);
students.put("0xMRDream", 50);
System.out.println(students);

// Remove an element
students.remove("0xMRDream");
System.out.println(students);

// Iterate over map
for (String key : students.keySet()){
System.out.println(key);
// get value of the key
System.out.println(students.get(key));
}
}

We created a hashmap here then put(key, value) to it and then we remove an element using its key and at the end, we iterate over the map.

Don’t forget to check the code here and keep in touch there is more ….

--

--