top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Java Collections Framework

Java collections is a collection of interfaces and classes which helps in storing and processing the data efficiently. This framework has several useful classes which have tons of useful functions which makes a programmer task super easy. In simple words this has flexible methods where we store the data and operate on those data.


To understand the collection better we can imagine a big bag where we can put all our stuff inside. We can keep inside the bag the items like user, product ,cart and all other objects. If we want to retrieve exactly user we cannot retrieve based on the index like in array, what we can do is put the bag on the floor and go over the items one by one. We need specific methods to over each element. Let see the below diagram




In this diagram we can see the list of methods collection interface have. Lets understand the method with an example

+contains(o: Object) : boolean

Here the "+" sign means the method is public, followed by a method signature which has the method name and method parameters followed by a colon ":" and then the return type of the method which is boolean in the above example.


Lets now come back to our original question how could we go over each of the element with these methods. We can check the number of the elements in the bag which is done by the "size()" method, Or if we can check whether the bag is empty with the method "isEmpty()". We can check if the bag contains specific element with the "contains()" method.


Iterable:


The Iterable interface is the root of the entire collection hierarchy, which means that every class and interface implements it. The primary function of an iterator is to allow the user to traverse through all of the collection class objects as if they were simple sequences of data items.


The iterator method returns object of iterator type, this object will help us to iterate over element in the collection.


Why Collection?

Lets summarize the difference between the Array and the collections



Collection :


The Collection interface extends the Iterable interface. It has the basic methods required for using all the other collections in the framework to add, delete, and manipulate data


There are three main interfaces that extends from the collection interface.


  1. List:


A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements. Elements can be inserted or accessed by their position in the list, using a zero-based index. The classes that implements List interface are:


  • ArrayList

  • LinkedList

  • Vector

  • Stack


a) Array List:


The Array List implements the List interface. The objects of this class are dynamic arrays. The ArrayList is resizable , it implements all of the List methods and allow all elements even null elements. The Array List object have a capacity, which can initially have a different size and increases the size the new elements are added to it which is not possible in arrays. The syntax of ArrayList object creation is


ArrayList<?> arrayList = new ArrayList<?>();

Lets see an example of ArrayList and some of the methods implemented to organize data


add()

ArrayList<String> a = new ArrayList<String>();
a.add("Selenium");
a.add("Java");
a.add("TestNG");
a.add("Cucumber");
a.add(0, "Java");
System.out.println(a);

Output:

[Java, Selenium, Java, TestNG, Cucumber]

The "add()" method adds a new element to the ArrayList object a. In this example "Java" is added twice, List interface accepts duplicate values. The array List stores the data in a sequential order, so retrieving those data can be done with the index of the element.

System.out.println(a.get(0));

Output:

Java

add(index , value)

After adding the elements we decided we do not want Java in the first index, we want Testing as the first element. We can add any element to any index in this list the ArrayList will dynamically resize the array.


a.add(0, "Testing");

Output:

[Testing, Java, TestNG, Cucumber, Java]

remove()


We can remove any element in the list object with the help of the remove method. We can remove either by the index or the value.


a.remove(1);
//OR
a.remove("Java");
System.out.println(a);

output:

[Testing, TestNG, Cucumber, Java]

indexOf()


We can get the particular index of a element from the object by the index of method.


System.out.println(a.indexOf("Cucumber"));

ouput:


2

b)  LinkedList


The LinkedList class implements the List interface as well as the Deque interface. The LinkedList is the class implementation of the linked list data structure, where every element has a pointer to the next element forming a link. Since each element has an address of the next element, the linked list elements, referred to as nodes, can be stored at non-contiguous locations in memory.


Syntax:

LinkedList<?> linkedListName = new LinkedList<?>();










Output:







2. Set


A Set is a Collection that cannot contain duplicate elements. There are three main implementations of Set interface:


  • HashSet

  • TreeSet

  • LinkedHashSet


a)Hashset

The Hash set implement the set interface. There is no guarantee that the elements may in the same order as the order of insertion. It does not accept duplicate values. When an element is added into the HashSet a HashCode is calculated and the element is added to the appropriate bucket ( a bucket is a slot in any Hash structure ). A good HashSet algorithm will uniformly distribute the elements so that the time performance of the structure remains constant. A constant-time performance means it takes constant time for basic operations like insert, deletes, and search.














Output:

[USA, UK]
[USA]

In the above example we are using the add() method to add element to the "hs" object, then we are removing the element from the set using the remove() method. The hasNext() method and next() method are methods of the Iterable interface that are used to check if there is the next element and to retrieve the next element respectively in any Collection. Using the constructor of the LinkedHashSet, all of the elements of the HashSet are added to it. An Iterator is created to traverse through it and using it the elements are printed on the console.



The Map interface is a structure that maps a key to every value. A Map does not allow duplicate elements as one key cannot have multiple mappings. A Map has three different views, a Set view of the keys, a Set view of key-value mappings, and a Collection view of the values. The three implementations of map are


  • HashMap

  • TreeMap

  • LinkedHashMap


Output:












In the above example the elements are added to the map object using the put() method, the elements are stored as key value pair. we remove an element from the map using the remove() method. The iterator traverse though the hashmap and and print each key an value with the getKey() and the getValue() method.


This blog covered only some of the useful collection interface methods, classes and the implementations. Hope this helps you to understand the collection framework. Happy Learning!

35 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page