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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Introduction to Collection Algorithms 
 

The Collections and Arrays classes, available as a part of the Collections Framework, support various algorithms.

 

Introduction to Collection Algorithms

                         

Algorithms:

The Collections and Arrays classes, available as a part of the Collections Framework, support various algorithms. The  Java platform provides great majority of the algorithms to perform different kind of operations such as sorting and searching.

I. Sorting Algorithm:

The sort algorithm reorders a List such that its elements are in ascending order according to an ordering relationship. The sort operation uses a slightly optimized merge sort algorithm which is fast and stable. TreeSet and TreeMap classes offers a sorted version of sets and maps, there is no sorted List collection implementation. Sorting of a List is done with the sort( ) method.

For example, the following program prints the arguments (the arguments, given through command line) of a  List in an alphabetical order.

import java.util.*;

public class SortDemo {
    public static void main(String[] args) {
        List<String> list = Arrays.asList(args);
        Collections.sort(list);
        System.out.println(list);
    }
}

 Output of this program:

C:\nisha>java SortDemo this is a commandline argument
[a, argument, commandline, is, this]

C:\nisha>

Download this program:

Searching Algorithm :

Besides sorting, the Collections and Arrays classes provide a mechanism to search a List or an array, as well as to find the first and last values within a Collection. The binarySearch algorithm searches for a specified element in a sorted List. This algorithm  takes a List and an element to search for the search key. This form assumes that the List is sorted in ascending order according to the natural ordering of its elements.

Before searching an element, the List must be sorted, Once you have sorted the List, using Collection.sort( ) method, you can perform a quickly binary search  operation using the overridden binarySearch( ) method. 

For example, the following program prints the sorted arguments list (the arguments, given through command line) and then search the position of a specified key value.

import java.util.*;

public class SearchDemo {
    public static void main(String[] args) {
    try{
        List<String> list = Arrays.asList(args);
        Collections.sort(list);
        System.out.println("The sorted list is: "+list);
    int pos = Collections.binarySearch(list, list.get(2));
    System.out.println("The position of the searched element is : "+pos 
                     +
" and the element is:"+list.get(2));
    }
    catch(Exception e){}
           
    }
}

 Output of this program:

C:\nisha>java SearchDemo this is a commandline argument
The sorted list is: [a, argument, commandline, is, this]
The position of the searched element is : 2 and the element is:commandline

C:\nisha>

Download this program:

                         

» View all related tutorials
Related Tags: c algorithm api ide collections orm ant data form interface framework software io multiple ip order vi collection tag int

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 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

these codes are very helpful, keep up the good job

Posted by solomon on Monday, 11.5.07 @ 23:10pm | #35686

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.