Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Introduction of Java Collections

The objective of the tutorial is to make you familiar around Java collection classes to assist Java Developers in getting acquainted with classes and interfaces provided by java Collections API.

Java Developers are provided with the Java Collections API that compromises of set of classes as well as interfaces so as to make it simpler to manage collections of objects. Collections in some sense much work much similar on the lines of arrays. The only difference is that their size can change dynamically. Also they have a pretty enhanced behavior than arrays. Instead of writing the own classes of collection, Java has the ability to offer the already built classes. Through this tutorial, let us see more about Java Collection’s and overview on Java Collection classes.

Majority of the Java Collections are stores in the package namely java.util and the concurrent collections are located in java.util.concurrent package. Let have some overview on Interfaces that is necessary to understand the Java Collections effectively.

“Collections” & “Maps” are the two groups of interfaces and the below figures resembles the interface hierarchy of each of them.

Collection interface hierarchy

Figure 1: Collection interface hierarchy

Map Interface hierarchy

Figure 2: Map Interface hierarchy

“ava.lang.Iterable” is an “iterable” interface. This is one of the basic interfaces of Java Collection classes. The interface “Collections” broadens out “Iterable” thereby making all subtypes of Collection to be in same line of implementing the Interface “Iterable”.

New for-loop can be made use of by a class to implement the “Iterable”. Let’s take a look at the below example.

List list = new ArrayList();

for(Object o : list){
    //any activity o;    
}

There is only method for the Iterable interface:

Listing 1: Iterable Interface

public interface Iterable<T> {
  public Iterator<T> iterator();    
}

Java.util.collection, also known as the Collection interface is one of the primary or we can say, the rot interface for the Java collection classes. Even if the Collection is not instantiate directly, however, a collection subtype can be made use of and can be treated on the much similar lines of a Collection.

Here is a list of the topics covered in this text:

  1. Collection Subtypes
  2. Adding and Removing Elements

One will never see Java coming with a usable implementation as far as the Collection interface is concerned. Hence to makes necessary to use one or the other Collection subtypes. The task or the responsibility of the Collection interface is to define a set of behavior or methods that can be shared by every collection subtype. Hence, it makes difficult to ignore the particular type of collection that one is using and you would always treat it as a collection. This is nothing but a standard inheritance however as the time goes on, it can be a very effective attribute or feature. Let us take a look at the use of these operations.

Listing 2: Subsequent process working on a Collection

public class MyCollectionUtil{

  public static void doSomething(Collection collection) {
    
    Iterator iterator = collection.iterator();
    while(iterator.hasNext()){
      Object object = iterator.next();

      //something needs to be done with an object here...
    }
  }
}

And now, look at some of the ways to call this method with diverse Collection subtypes:

Listing 3: Calling method with Collection subtypes.

Set  set  = new HashSet();
List list = new ArrayList();

MyCollectionUtil.doSomething(set);
MyCollectionUtil.doSomething(list);    

      //something needs to be done with an object here...
    }
  }
}

Elements Addition and Removal

Irrespective of which subtype you are making use of, there are defined processes or methods that can assist in addition or removal of elements from a collection.

Listing 4: Listing the process to do the same.

String     anElement  = "an element";
Collection collection = new HashSet();

boolean didCollectionChange = collection.add(anElement);
boolean wasElementRemoved   = collection.remove(anElement);

Elements can be added to the collection making use the method add(). The value is returned true in case the collection is altered as a result of initiating or calling the method add(). There could be cases as well where the set for interface would have remains the same. This happens in case where the element is already present in the set thereby blocking him to add the same one again. Whereas on the other side, if add() is operated on a List with the assumption that the element is already there in the List, there will be two entries of the element in the List.

The method used to remove the element is remove(). The value is returned true in case the element was present in the collection and removed. remove(). On the other hand, the value “false” will be returned if the element was not present in the collection.

The collection of objects can also be added and removed in the following manner.

Listing 5: Add and remove Manner

Set  aSet  = ... // get Set  with elements from somewhere
List aList = ... // get List with elements from somewhere

Collection collection = new HashSet();

collection.addAll(aSet);    //returns boolean too, but ignored here.
collection.addAll(aList);   //returns boolean too, but ignored here.

collection.removeAll(aList);   //returns boolean too...
collection.retainAll(aSet);    //returns boolean too...

addAll() is the method that is used to add all the elements that are located in the Collection by passing elements as a one of the parameters to the method. The point to note here is that only the elements of the collection are added and not the collection object itself. In case you want to add the collection object and not the elements, the method add()need to contain the collection as a parameter.

The way addAll() method works is dependent on the subtypes of the Collection. There are subtypes that will permit the same element to get added more than once and others do not allow the same to happen.

The task of retainAll() is just on the other side of of removeAll() retainAll()i.e. it serves as opposite to what removeAll() does. The method retains all these elements and removes the other ones instead of removing all the elements. The elements that are located in the collection parameter and are not in the target collection, they are not just added by default, instead gets ignored.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login