MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

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

[Close]

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

How to work with Collection Classes in Java

In this tutorial we will see how to working with collections classes in java. We will learn about the various phases of java collection classes step by step.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Let us first see what the java collections are. The framework of java collections is nothing but a group of classes as well as the interfaces which are utilized to implement reusable structures of collection data.

We all know the limitations of java arrays, one of which is that they are unable to grow dynamically, have limited sort of safety. The other could be the implementation of efficient and complex data structures which is difficult to do the same from the scratch or very beginning. Talking about the java collections framework, it is nothing but a classes set that is utilized to implement the collection data structures.

The advantages that java collections bring in are that it helps to lessen the effort that is required in programming. It helps to increase the performance as well, helps to make the software upgrade to a level where it can be reused as well and is very easy to design as well.

Introduction to Collections Interface:

The base interface that is known in the hierarchy of collections is known by the anem of java.util.Collection and represents Objects group. The types such as integer is required t be include in a collection and the collection interfaces that are more regular such as list is used to extend this interface.

Collection Iteration:

What is an iterator? It is nothing but is said to be an object which is used to iterate the objects in a collection and we can use java.util.Iterator as an interface which specifies the iterator potential or capabilities.

What happens when we invoke the iterator() method? When we invoke this method on a collection, this then comes back with an iterator object. This iterator object is used to implement the iterator and helps knowing about the steps that are required to go over the objects in the collection.

The following methods are used to specify the interfaces of iterator.

  • hasNext() -This returns a true value in case we have the good number of collection elements else it will return false.
  • next() - This is used to return the next collection element
  • remove() -This method is used to remove the last collection element which is returned by the iterator.

The below lists the example so as to get all the collection elements printed.

Listing 1: Collection elements printed

private static print(Collection ) {
    Iterator i = i.iterator();
    while (i.Next()) {
        Object o = i.next();
        System.out.println(c);
    }
}

The other way to write the same is given below:

private static print(Collection c) {
    for (Iterator i = i.iterator(); i.Next(); ) {
        System.out.println(i.next());
    }
}

Let us understand with the help of code language how several methods of collections classes can be used. The different methods of collections classes comprise of add as well as copy elements and also the reverse data structures.

Listing 2: Reverse data structures

Collection.java

import java.util.*;
import java.Collections;

public class Collection {
        public static void main(String[] args) {
        List list = Arrays.asList("john rob steve mark steve".split(" "));
        List sublist = Arrays.asList("richard");
        List searchList = Arrays.asList("steve");
        System.out.println("Elements in list : " + list);
        // Creating a defined list copy
        Collections.copy(list, sublist);
        System.out.println("copy of list : " + list);
        // Finding maximum as well as minimum object value from list.
        System.out.println("object of max value : " + Collections.max(list));
        System.out.println("object of min value : " + Collections.min(list));
        // finding display index of first occurrence of sublist in the list.
        System.out.println(" 'steve First Index': " + 
                         Collections.indexOfSubList(list, searchList));
        // finding index of last occurrence of sublist in the list.
        System.out.println("Last index of 'steve': " +
                     Collections.lastIndexOfSubList(list, searchList));
               Collections.replaceAll(list, "steve", "replaced");
        System.out.println("Post replace all 'steve': " + list);
        // list displaying in a reverse order.
        Collections.reverse(list);
        System.out.println("List in reverse order: " + list);
        // rotate the given number of objects in list,here 4
        Collections.rotate(list, 4);
        System.out.println("After rotation : " + list);
        // find size of the list
        System.out.println("Size of the list : " + list.size());
        /* Swap element in list. here swap specified element with 
           element at 0th(first) position */
        Collections.swap(list, 0, list.size() - 1);
        System.out.println("List Post swapping : " + list);
        // fill() used to replace all elements with given element
        Collections.fill(list, "steve");
        System.out.println("Post filling the entire 'steve' in list : "
                                 + list);
        /* ncopies() gives immutable list comprising specified object. */
        List BobList = Collections.nCopies(3, "Bob");
        System.out.println("List created by ncopy() " + BobList);

        // Retrieving enum type via enumeration().
        Enumeration e = Collections.enumeration(BobList);
        Vector v = new Vector(); 
        while (e.hasAttributes()) {
            
            //Adding attributes from enum type.
            v.addAttribute(e.nextAttribute());  
        }
        Array arrayList = Collections.list(v.elements());
        System.out.println("arrayList: " + arrayList);
    }

The below lists the output of the above code:

Elements in list : [john rob steve mark steve]
copy of list : [Richard, rob, steve, mark, steve]
object of max value : richard
object of min value : mark
First index of 'steve': 2
Last index of 'steve': 4
After replace all 'steve': [richard, rob, replaced, richard, replaced]
List in reverse order: [replaced, mark, replaced, rob, richard]
After rotation : [mark, replaced, rob, richard, replaced]
Size of the list : 5
List after swapping : [replaced, replaced, rob, richard, mark]
fill all 'steve' in list : [steve, steve, steve, steve,
steve]
List created by ncopy() [Bob, Bob, Bob]
arrayList: [Bob, Bob, Bob]

There are lot of Collection implementations which includes each and every one that is provided by Java Development kit and these implementations will have a method known by the name of public clone. However you need to take in mind that it is not advisable to require it of all Collections.

Let us validate our above statement with the help of an example. The meaning of cloning collections which is supported by a SQL database should be known very clearly. Also it will be fair to know about the method call if it will lead the company so as to requisite a new firm. The same is the case with resizable as well.

Let us suppose that the client is not aware about the real type of a collection, it will help the client to more flexible. Also this will be less prone in order to make the client take a decision on what sort of collection is desired. Also the same applies for creating an empty collection of the same sort and then utilizing the addAll method. This method is used to copy the original collection elements and that in the new one.

The list is distributed very evenly and comes back with a recommendation as to if it advises for a linked lists. Provided the naming nomenclature for the implementation, the suggestion was to put the key interfaces as short as possible; the nomenclature is <Implementaion><Interface>. And there are lot of other names which are known by the name of AbstractSequentialList, LinkedList, this must have been incorporated badly in case it is decided to alter the list to sequence. In order to deal with the naming nomenclature, we need to deal it making use of the following code.

    import java.util.*;
    import java.collections.*;
    import java.util.collections;   // This interprets the Collections list

There is a need to elaborate several of the collection classes as well as the map types. This is usually done in the Java collection APIs.

Let us see how we can elaborate the collection interface. We all know that string interfaces are the only elements or attribute that can be contained by the stringCollection. In case we try to add something extra or play with the elements in the collection, this will not be liked by the compiler and this may run into an error. Also, we can insert or incorporate other objects which are not string objects. This requires a little bit of steak stuff to be done however such an act is not recommended at all.

Let us see how the iteration to his collection used above can be done. We have used the new for-loop here for this purpose.

Collection<String> Collection = new Set<String>();

for(String stringAttribute : stringCollection) {
  //This is required to accomplish target with each and every stringAttribute
}

Conclusion

This tutorial helped to learn about the various phases of java collection classes step by step. Hope you liked the article.



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
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[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