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

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++){
public static void main(String[] args) {
int magic[] = new int[]{1, 2, 3, 4, 5};
for (int x : magic){


  • 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(1, "Add me at index 1");
//get method
String index_1 = arrList_obj.get(1); //returns the value at index 1
//Check if the list has this element or not
boolean chk = arrList_obj.contains("0xIslamTaha"); // return true
//get list size
//chck if list is empty


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

// try to add a duplicate element
// print length of it

// remove an element

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);

// Remove an element

// Iterate over map
for (String key : students.keySet()){
// get value of the key



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store