Java Collections for Beginners

Java Collections for Beginners

Java Collections are objects that can include many other objects. And these objects can be treated as one set of data.

Collection interfaces (and their most popular implementations)

– List (ArrayList, LinkedList)

– Queue (PriorityQueue):      Deque (ArrayDeque)

– Set (HashSet):     SortedSet              NavigableSet

Let’s look at using List interface (and its ArrayList and LinkedList implementations) that is the most popular.

We could write the code in this way:

package com.linaittech;

import java.util.*;

public class Kolekcje {

public static void main(String args[]) {

List list = new ArrayList();

list.add(“Ela”);
list.add(“Ula”);
list.add(“Ola”);
list.add(“Iza”);
list.add(“Mika”);

System.out.println(list);
System.out.println(“2: ” + list.get(4));
System.out.println(“0: ” + list.get(0));
System.out.println(“1: ” + list.get(1));

LinkedList queue = new LinkedList();

queue.addFirst(“Benek”);
queue.addFirst(“Olek”);
queue.addFirst(“Bolek”);
queue.addFirst(“Tolek”);
queue.addFirst(“Gienek”);
queue.addFirst(“Gienek”);

System.out.println(queue);

queue.removeLast();
queue.removeLast();

System.out.println(queue);

queue.removeFirst();
queue.removeFirst();

System.out.println(queue);

}

}

We have the output:

[Ela, Ula, Ola, Iza, Mika]
2: Mika
0: Ela
1: Ula
[Gienek, Gienek, Tolek, Bolek, Olek, Benek]
[Gienek, Gienek, Tolek, Bolek]
[Tolek, Bolek]

But it’s a bad way. We should write in this way:

package com.linaittech;

import java.util.*;

public class Kolekcje {

public static void main(String args[]) {

List<String> list = new ArrayList<String>();

list.add(“Ela”);
list.add(“Ula”);
list.add(“Ola”);
list.add(“Iza”);
list.add(“Mika”);
list.add(“Wiki”);

System.out.println(list);
System.out.println(“2: ” + list.get(4));
System.out.println(“0: ” + list.get(0));
System.out.println(“1: ” + list.get(1));

LinkedList<String> queue = new LinkedList<String>();

queue.addFirst(“Benek”);
queue.addFirst(“Olek”);
queue.addFirst(“Bolek”);
queue.addFirst(“Tolek”);
queue.addFirst(“Gienek”);
queue.addFirst(“Gienek”);

System.out.println(queue);

queue.removeLast();
queue.removeLast();

System.out.println(queue);

queue.removeFirst();
queue.removeFirst();

System.out.println(queue);

}

}

We have the output:

[Ela, Ula, Ola, Iza, Mika, Wiki]
2: Mika
0: Ela
1: Ula
[Gienek, Gienek, Tolek, Bolek, Olek, Benek]
[Gienek, Gienek, Tolek, Bolek]
[Tolek, Bolek]

The output is the same, so what’s the problem? Because there can some problems with implementing some interfaces using the first example, you should use the second method, in which we see some generics. We use simply generic types here. Compare

List list = new ArrayList();

and

List<String> list = new ArrayList<String>();

The difference is that we know that we will deal with String type. References to generic types should be parametrized. It’s a safer way for us. We can also use Integer, for example.

package com.linaittech;  
 
import java.util.*;  
 
public class Kolekcje {  
 
    public static void main(String[] args) {  
        List <Integer> firstList = new ArrayList<Integer>();  
        List <Integer> secondList = new ArrayList<Integer>();
        
        firstList.add(1);  
        firstList.add(2);  
        firstList.add(3);  
        firstList.add(4);  
        firstList.add(5);  
      
          
        secondList.add(10);  
        secondList.add(20);  
        secondList.add(30);  
        secondList.add(40);  
        secondList.add(50);  
          
        System.out.println(“Our First List-“+ FirstList);  
        System.out.println(“Our Second List-“+ SecondList);
        
        Collections.copy(SecondList, FirstList );
        
        System.out.println(“Our First List After Having Copied-“+ FirstList);
        System.out.println(“Our Second List After Having Copied-“+ SecondList);
        
        Collections.shuffle(FirstList);
        Collections.shuffle(SecondList);
        
        System.out.println(“Our First List After Having Copied-“+ FirstList);
        System.out.println(“Our Second List After Having Copied-“+ SecondList);
    }  
}

Now, we have

List <Integer> FirstList = new ArrayList<Integer>(); 

and

List <Integer> SecondList = new ArrayList<Integer>();

Thanks to generics we know we will deal with Integer type. We can’t add a type that isn’t Integer, for example String. Thanks to that, we can use all methods that are in the Integer class,  and there won’t any complaints during compilation (in Eclipse IDE, for example).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s