close
Cart icon
User menu icon
User icon
Lightbulb icon
How it works?
FAQ icon
FAQ
Contact icon
Contact
Terms of service icon
Terms of service
Privacy policy icon
Privacy Policy
Zdjęcie główne artykułu.

Java - introduction to collections

Collections are, simply-put, ready-to-use data structures. They are a part of the standard Java library. In practice, collections make it easier to implement other popular structures, like lists, queues, stacks etc.

The most convenient thing about Java’s collections is the fact that you can use them whenever you want. You don't have to write the code from the ground up - import the part of the library you need and it's ready to go. Let's take a look at a few of these built-in Java collections in some easy examples.

Look out - import

Before we can use collections, we need to import them first. This is how you do it:


import java.util.ArrayList;
import java.util.List;

With the code above we import the ArrayList class and the List interface from the package java.util. From now on, we can use these two elements in our program. Alternatively, you could import the entire util package:


import java.util.*;

Just use an asterisk.

Example - ArrayList

The ArrayList collection is a structure known colloquially as a list. It is a kind of an extension for the standard arrays available in Java. Take a look:


import java.util.*;
 
public class Main {
   public static void main(String[] args) {
       List countries = new ArrayList<>();
 
       countries.add("Belgium");
       countries.add("Norway");
       countries.add("Poland");
 
       System.out.println(countries.get(0));
   }
}

First we create a new list object: List countries = new ArrayList<>(). Next, we use the add() method to add a few values to it. Notice how our list takes only string type values - that was established during the object’s creation.

The last part of our example - the get() method - is used to get an element from the list with a certain index number. It works just like it does for standard arrays. There is an advantage to using the ArrayList collection though - its size changes dynamically and does not have to be declared from the beginning. The number of elements it currently has equals the size of the list.

Example - Stack

The most characteristic thing about stacks is that we only have access to the one element on the very top. If we want to get to an element that’s a bit lower in the stack, we first need to “take off” or “pop” the elements above it. Bottom line: the last added element will be the first one to leave.

Take a look at this example:


import java.util.*;
 
public class Main {
   public static void main(String[] args) {
       Stack stack = new Stack<>();
 
        stack.push("-1-");
        stack.push("-2-");
        stack.push("-3-");
 
        System.out.println(stack);
        stack.pop();
        System.out.println(stack);
   }
}

First we create a new Stack object. Note how it will only be taking string type data. Then, we add a few elements using the push() method.

If we run the code, this is the result:


[-1-,-2-,-3-]
[-1-,-2-]

We print our stack to the console - as you can see it has three elements. Then, using the pop() method we get rid of the top one. Here you can clearly see the way stacks work - the last element added is the first element out. You can only remove the elements from the top.

Example - PriorityQueue

The PriorityQueue collection is a popular one. The rule is simple - the earlier you get into the queue, the earlier you get to leave. PriorityQueue, however, has a distinct feature: elements leave the queue according to their “priority”.

Let's move right on to the code:


​​import java.util.*;
 
public class Main {
   public static void main(String[] args) {
       PriorityQueue queue = new PriorityQueue<>();
       queue.offer(1);
       queue.offer(100);
       queue.offer(200);
       queue.offer(2);
 
       System.out.println(queue);
 
       while(!queue.isEmpty()) {
           System.out.println("Result: " + queue.poll());
       }
   }
}

As usual we start with creating a new instance. This time it’s a PriorityQueue class object that will be storing integer type data.

Using the offer() method we add new elements, then we print the entire thing to console.

The next step is emptying the queue using a loop. We print consecutive elements until they run out and our queue is empty. Take a look at the result:


[1, 2, 200, 100]
Result: 1
Result: 2
Result: 100
Result: 200

The elements leave the queue according to their “priority". The value of 1 has the greatest priority. And the value of 200 has the lowest priority. So they leave the queue in an ascending order.

Summary

The three examples we have shown in the article are just the beginning. The built-in Java collections have way more in store for you, and you should definitely get to know some basics.

Do you want to learn more about collections? Here is a course just for you: Java - Collections. There you can find over a dozen different structures that will help you in your work with Java. During the course you will solve a multitude of exercises and practical tasks - and with that gain real skills that will extend into your everyday coding.