GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - The Collection Algorithms
Selected Reading
Java - The Collection AlgorithmsThe collections framework defines several algorithms that can be applied to collections and maps. These algorithms are defined as static methods within the Collections class. Several of the methods can throw a ClassCastException, which occurs when an attempt is made to compare incompatible types, or an UnsupportedOperationException, which occurs when an attempt is made to modify an unmodifiable collection. The methods defined in collection framework's algorithm are summarized in the following table −
Example 1Following is an example, which demonstrates various algorithms using LinkedList to add elements and sort its elements in reverse order. Using iterator, list is iterated then shuffled and lastly minimum and maximum values are retrieved and printed. import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class AlgorithmsDemo {
public static void main(String args[]) {
// Create and initialize linked list
List<Integer< ll = new LinkedList<<();
ll.add(Integer.valueOf(-8));
ll.add(Integer.valueOf(20));
ll.add(Integer.valueOf(-20));
ll.add(Integer.valueOf(8));
// Create a reverse order comparator
Comparator<Integer< r = Collections.reverseOrder();
// Sort list by using the comparator
Collections.sort(ll, r);
// Get iterator
Iterator<Integer< li = ll.iterator();
System.out.print("List sorted in reverse: ");
while(li.hasNext()) {
System.out.print(li.next() + " ");
}
System.out.println();
}
}
OutputList sorted in reverse: 20 8 -8 -20 Example 2Following is an example, which demonstrates various algorithms using LinkedList to add elements and sort its elements in natural order. Using iterator, list is iterated then shuffled and lastly minimum and maximum values are retrieved and printed. import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class AlgorithmsDemo {
public static void main(String args[]) {
// Create and initialize linked list
List<Integer> ll = new LinkedList<>();
ll.add(Integer.valueOf(-8));
ll.add(Integer.valueOf(20));
ll.add(Integer.valueOf(-20));
ll.add(Integer.valueOf(8));
// Sort list by using the default comparator
Collections.sort(ll);
// Get iterator
Iterator<Integer> li = ll.iterator();
System.out.print("List sorted: ");
while(li.hasNext()) {
System.out.print(li.next() + " ");
}
System.out.println();
Collections.shuffle(ll);
// display randomized list
li = ll.iterator();
System.out.print("List shuffled: ");
while(li.hasNext()) {
System.out.print(li.next() + " ");
}
System.out.println();
System.out.println("Minimum: " + Collections.min(ll));
System.out.println("Maximum: " + Collections.max(ll));
}
}
OutputList sorted: -20 -8 8 20 List shuffled: -20 -8 20 8 Minimum: -20 Maximum: 20 Example 3Following is an example, which demonstrates various algorithms using LinkedList to add String elements and sort its elements in natural order. List is printed then shuffled and lastly minimum and maximum values are retrieved and printed. import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class AlgorithmsDemo {
public static void main(String args[]) {
// Create and initialize linked list
List<String> list = new LinkedList<>();
list.add("Ram");
list.add("Mohan");
list.add("Julie");
list.add("Raheem");
// Sort list by using the default comparator
Collections.sort(list);
// print the sorted list
System.out.println("Sorted List: " + list);
// shuffle the list
Collections.shuffle(list);
// print the shuffled list
System.out.println("Shuffled List: " + list);
System.out.println("Minimum: " + Collections.min(list));
System.out.println("Maximum: " + Collections.max(list));
}
}
OutputSorted List: [Julie, Mohan, Raheem, Ram] Shuffled List: [Mohan, Raheem, Julie, Ram] Minimum: Julie Maximum: Ram java_collections.htm
Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |