Tuesday, January 31, 2017

6-Collections operational APIs in common

Sorting

  • Already sorted Collections are as below. 
    • TreeSet
    • TreeMap
However for the rest, a comparator needs to be implemented in 3 following ways:
  • sort() method for sorting List:
    • List l = new ArrayList(Arrays.asList(3, 5, 4, 2, 3, 2, 1, 3));
    • Collections.sort(l); //increasing order
    • System.out.println("Increasing order:"+l);
    • O/p: Increasing order: [1, 2, 2, 3, 3, 3, 4, 5]
    • .
    • However, The element’s class must implement Comparable interface. This is mandatory since this version of sort() uses element’s compareTo() method to compare them. Otherwise a ClassCastException will be thrown.


  • custom Sort method for sorting List:Steps to create custom Comparator are below:
    • create a custom comparator class that implements Comparator interface, and implement its compare() method as below:
      • class IntegerComparator implements Comparator {
      •     public int compare(Object o1, Object o2) {
      •       return (Integer)o2 - (Integer)o1;
      •     }
      • }
    • Note that the Object that is compared(Integer in this case) needs to implement Comparable interface in order to be use a comparator(IntegerComparator  in this case) for comparison.
      • This customComparator(IntegerComparator) then used as in below code:
        • Collections.sort(l, new IntegerComparator());//decreasing order
        • System.out.println("Decreasing order\n"+l);
    • Using unnamed Comparator class(within sort() call)
      • Collections.sort(l, new Comparator() {
              • public int compare(Object o1, Object o2) {
              • return (Integer)o2 - (Integer)o1;
              • }}
            • ); //decreasing order
    _______________________________________________________________________


    Shuffling

    • The opposite of sort is shuffle which arranges list elements arbitrarily.
    • Two versions of shuffle() are :
      1. One operates on a List using a default source of randomness.
        • Collections.shuffle(l);
      2. Another requires a Random object to be specified explicitly.
        • Collections.shuffle(l, new Random());

    _______________________________________________________________________

    Manipulation

    • Reversing Collections.reverse(list);
    • Swapping : swaps elements of two specified positions of a specified list
      • Collections.swap(list,2, 4);
    • Copying : Copy all the elements on lists1 into list2
      • Collections.copy(list1, list2);
    • Filling : fill all elements of list1 with "0", wrt to example below
      • Collections.fill(list,0);
    • Adding: Collections.addAll(l1, -1, -2, -3);//Adds three integers to the list l1.
    _______________________________________________________________________

    Search

    • binarySearch(): It is common to use sort() method to sort the List before a call to binarySearch().
      • Collections.sort(list); //list=[1, 2, 2, 3, 3, 3, 4, 5]
      • int in = Collections.binarySearch(l, 4); //in = 6
      • in = Collections.binarySearch(list, 2); //in = 1
      • in = Collections.binarySearch(list, 3); //in = 3
    • For unsuccessful search, it returns a value (-(insertion point) - 1),
      • in = Collections.binarySearch(list, 0); //in = -1
    _______________________________________________________________________

    min()/max()

    • int min = (int)Collections.min(list); //min = 0
    • int max = (int)Collections.max(list); //max = 6
    _______________________________________________________________________

    frequency()
    • int fre = Collections.frequency(list,2); //fre = 2
    • fre = Collections.frequency(list,3); //fre = 3


    _______________________________________________________________________

    No comments:

    Post a Comment