Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Working with java Collections class 
 

In this section we will learn how to wok with Collections class of java.util package.

 

Working with java Collections class

                         

In this section we will learn how to wok with Collections class of java.util package. Actually java collections framework is a set of classes and interfaces that are used to implement reusable collection data structures.

Code given below shows that how to use various methods of Collections class. For example- add element, copy elements, reverse data structure etc.

 

 

 

 

Collection.java

import java.util.*;
import java.util.Collections;
public class Collection {
    public static void main(String[] args) {
        List list = Arrays.asList("komal girish mahendra amit 
                    mahendra".split(" "));
        List sublist = Arrays.asList("vineet");
        List searchList = Arrays.asList("mahendra");
        System.out.println("Elements in list : " + list);
        // create a copy of defined list and print objects of copy list.
        Collections.copy(list, sublist);
        System.out.println("copy of list : " + list);
        // find and display maximum and 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));
        // find and display index of first occurance of sublist in the list.
        System.out.println("First index of 'mahendra': " + 
                         Collections.indexOfSubList(list, searchList));
        // find and display index of last occurance of sublist in the list.
        System.out.println("Last index of 'mahendra': " +
                     Collections.lastIndexOfSubList(list, searchList));
        // replace all objects in list by a new given object.
        Collections.replaceAll(list, "mahendra", "replaced");
        System.out.println("After replace all 'mahendra': " + list);
        // list in 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 after swapping : " + list);
        // Replace all the element with given element using fill()
        Collections.fill(list, "mahendra");
        System.out.println("After filling all 'mahendra' in list : "
                                 + list);
        /* ncopies() returns immutable list consisting of copies 
             of the specified object. */
        List raviList = Collections.nCopies(3, "Ravi");
        System.out.println("List created by ncopy() " + raviList);
        // getting an enum type of specified list through enumeration().
        Enumeration e = Collections.enumeration(raviList);
        Vector v = new Vector(); // create a vector object.
        while (e.hasMoreElements()) {
            
            // add elements in vector from enum type.
            v.addElement(e.nextElement());  
        }
        ArrayList arrayList = Collections.list(v.elements());
        System.out.println("arrayList: " + arrayList);
    }
}

 

Output :

Elements in list : [komal, girish, mahendra, amit, mahendra]
copy of list : [vineet, girish, mahendra, amit, mahendra]
object of max value : vineet
object of min value : amit
First index of 'mahendra': 2
Last index of 'mahendra': 4
After replace all 'mahendra': [vineet, girish, replaced, amit, replaced]
List in reverse order: [replaced, amit, replaced, girish, vineet]
After rotation : [amit, replaced, girish, vineet, replaced]
Size of the list : 5
List after swapping : [replaced, replaced, girish, vineet, amit]
fill all 'mahendra' in list : [mahendra, mahendra, mahendra, mahendra, 
                               mahendra]
List created by ncopy() [Ravi, Ravi, Ravi]
arrayList: [Ravi, Ravi, Ravi]

Download Source Code

                         

» View all related tutorials
Related Tags: java c string ide array io word vi new key this id element elements for example keyword with to ini

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.