//Sample Example for available methods of Collection class
package com.corejava.gaurav.examples;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
public class CollectionsClassMethodTestExample {
public static void main(String args[]){
ArrayList<Integer> lst = new ArrayList<Integer>(0);
lst.add(9);
lst.add(7);
lst.add(3);
lst.add(10);
Object obj = Collections.min(lst);
System.out.println("miminum obj is-"+obj);
Object obj1 = Collections.max(lst);
System.out.println("maximum object is-"+obj1);
ArrayList<Integer> numLst = new ArrayList<Integer>(0);
numLst.add(15);
numLst.add(21);
numLst.add(33);
numLst.add(44);
Collections.copy(numLst, lst);
System.out.println("numlist is-"+numLst);
ArrayList<String> slst = new ArrayList<String>(0);
slst.add("A");
slst.add("B");
slst.add("C");
slst.add("D");
Enumeration e = Collections.enumeration(slst);
while(e.hasMoreElements()){
System.out.println("elements are-"+e.nextElement());
}
Collections.sort(lst);
System.out.println("Elements after sorting-"+lst);
int index = Collections.binarySearch(lst, 9);
System.out.println("element available at index-> "+index);
Collections.swap(lst,0,3);
System.out.println("Lst is after swap="+lst);
Comparator comparater = Collections.reverseOrder();
Collections.sort(slst,comparater);
System.out.println("slst elements are after sort in desc order-"+slst);
}
}
No comments:
Post a Comment