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

Working with Java Collections Subtypes

This tutorial is a continuation of the other part “Introduction of Java Collections”. In this article we will see how to verify whether a Collection includes a Certain Element, Size of Collection and Iterating a Collection.

In continuation to where we left in Part 1 of Java Collections, we will look here on the following items

  • Verifying whether a Collection includes a Certain Element
  • Size of Collection
  • Iterating a Collection

Verifying whether a Collection includes a Certain Element

There are two methods namely contains() and containsAll() in Collection interface via which we can check whether there is one element or more than one. The below lists the example of the same:

Listing 1: Contains example

Collection collection   = new HashSet();
boolean containsElement = collection.contains("an element");

Collection elements     = new HashSet();
boolean containsAll     = collection.containsAll(elements);

The value true is returned for the contains() method in case the element is contained by the collection else false will be displayed. And when we talk about containsAll(), it will come with a true value when all the elements are contained in the collection else it will also display as false.

Size of Collection

The method size() can be made use of to verify the collection size which means that the number or quantity of elements present in the collection will be returned by this method. The below call illustrates the example of the same.

int numberOfElements = collection.size();    

Iterating a Collection

All the elements of the collection can be iterated via the means of receiving an iterator from the collection and iterating through the same. The below example explains the same.

Listing 2: Iterating a Collection example

Collection collection = new HashSet();
//... add elements to the collection

Iterator iterator = collection.iterator();
while(iterator.hasNext()){
    Object object = iterator.next();
//do something to object;    
}

New for-loop can also be made use of:

Listing 3: For-Loop example

Collection collection = new HashSet();
//... elements are added to collection
for(Object object : collection) {
  //do something to object;
}

Collection Subtypes

The subsequent interfaces extend the Collection interface:

  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Deque

Java.util.List is nothing but the subtype of the interface namely java.util.Collection and represents nothing but an ordered list of objects. This means that one can gain access to the elements of a List in an order specified as well as by index too. Also the same element can be added more than one time to a List.

The below topics will be covered as a part of this tutorial.

  1. List Implementations
  2. Elements Addition and Accessing
  3. Removing Elements
  4. Generic Lists

List Implementations

The methods that are available in the Collection interface holds valid for List interface as well. For the reason that List would be a interface that one would be interested in to instantiate, there would be a requirement of a concrete functionality of interface so that it can be used effectively. One can make use of the following or subsequent List implementations as far as Java Collections API is concerned.

  • java.util.ArrayList
  • java.util.LinkedList
  • java.util.Vector
  • java.util.Stack

A small number of illustrations of the process to create a List instance:

List listOne = new ArrayList();
List listTwo = new LinkedList();
List listThree = new Vector();
List listFour = new Stack();    

Elements Addition and Accessing

The method add() is called so as to add the elements to the list. The method is inherited from the interface of the Collection. Below lists few of the examples:

List list1 = new ArrayList();

List1.add("element 1");
List1.add("element 2");
List1.add("element 3");

List1.add(0, "element 0");

The add() calls at the first three list performs the task of adding a string instance appended to the list end whereas the last one adds a string at index0 position i.e. to the beginning of the list.

The order which is made use of to add the elements in the list is stored. This is for the reason that the same order can be used to retrieve or access the elements. The same can be done via the following the methods as get(int index) and iterator() method. Below example shows how:

Listing 4: Access via Index, Iterator and for-loop

List list1 = new ArrayList();
List1.add("element 0");
List2.add("element 1");
List3.add("element 2");

//access via index
String element0 = list1.get(0);
String element1 = list2.get(1);
String element3 = list3.get(2);
//access via Iterator
Iterator iterator = list1.iterator();
while(iterator.hasNext(){
  String element = (String) iterator.next();
}

//access via new for-loop
for(Object object : list1) {
    String element = (String) object;
}

The elements will be iterated in the same order as they were stored in the list on making use of iterator() method or the for-loop.

Removing Elements

Elements can be removed in the following two ways.

  1. remove(Object element)
  2. remove(int index)

remove(Object element) when used or called is used to remove the element in the list only in case if it present over there. All the remaining elements are then moved up in the list thereby decreasing their index by 1.

remove(int index) task is to remove the element at the specified index thereby making all the subsequent elements in the list move up in the queue. Here also the index is decreased by 1.

Generic Lists

With the introduction of Java 5, the limit to which the types of object one can insert into a list has been limited but before that one can put any number of Objects in a list.

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

As of now, this specified list can just have MyObject instanes that can be inserted into the same. One can then goes ahead and iterate the elements of the same without even having to cast them. Here is how it looks.

MyObject myObject = list.get(0);

for(MyObject anObject : list){
   //do someting to anObject...   
}

There is a lot more than this one can do as far as List is concerned; the next tutorial will continue on the other Collection subtypes focusing more on the Java Set Interface, digging into more detail.



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