Tuesday, January 28, 2014

Working with Java Collections- Collections and Arrays Classes

Collections and Arrays are two utility classes to work with the Java Collections.These classes are used for sorting ,searching and working with customized collections .To compare the elements the List object should be comparable.Shown below introduce some of the useful method of Collections class



package collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class WorkingWithCollections {
       public static void main(String[] args) {

              ArrayList<String> aList = new ArrayList<String>();
              aList.add("Raj");
              aList.add("Sun");
              aList.add("Tol");
              aList.add("Jap");

              Collections.sort(aList);
              System.out.println("Sort : " + aList);

              Collections.sort(aList, Collections.reverseOrder());

              Collections.sort(aList, Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER));

              System.out.println(aList);

              Collections.reverse(aList);
              System.out.println(aList);

              Collections.rotate(aList, 3);
              System.out.println(aList);

              Collections.shuffle(aList);
              System.out.println(aList);

              Collections.swap(aList, 2, 3);
              System.out.println("Swap : " + aList);

              ArrayList<Emp> empList = new ArrayList<Emp>();

              Emp e1 = new WorkingWithCollections().new Emp();
              e1.setId(1);
              e1.setName("Raj");
              empList.add(e1);

              e1 = new WorkingWithCollections().new Emp();
              e1.setId(2);
              e1.setName("Sun");
              empList.add(e1);

              e1 = new WorkingWithCollections().new Emp();
              e1.setId(3);
              e1.setName("Jap");
              empList.add(e1);

              Collections.sort(empList, new WorkingWithCollections().new EmpComparator());
              System.out.println(empList);
              for (Emp e : empList) {
                     System.out.println(e.getName());
              }

       }

       public class Emp implements Comparable {
              private int id;
              private String name;

              public int getId() {
                     return id;
              }

              public void setId(int id) {
                     this.id = id;
              }

              public String getName() {
                     return name;
              }

              public void setName(String name) {
                     this.name = name;
              }

              @Override
              public int compareTo(Object o) {
                     // TODO Auto-generated method stub
                     return 0;
              }

       }

       public class EmpComparator implements Comparator {

              @Override
              public int compare(Object o1, Object o2) {

                     String s1 = ((Emp) o1).getName();
                     String s2 = ((Emp) o2).getName();

                     return s1.compareTo(s2);
              }

       }
}

Arrays

Searching in Arrays the array needs to sort naturally.Arrays provide set of List views method.asList() in arrays and toArray() in List act as a bridge between collections and arrays.List return from asList() cannot be changed 

package collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class WorkingWithArrays {

    public static void main(String[] args) {

        String a[] = { "Jan", "Feb", "Mar", "Apr", "May" };
        Arrays.sort(a);
        for (String str : a) {
            System.out.println(str);
        }
        System.out.println("Search : " + Arrays.binarySearch(a, "Mar"));
        System.out.println("Search1 : " + Arrays.binarySearch(a, "Mar", String.CASE_INSENSITIVE_ORDER));

        System.out.println("Search1 : " + Arrays.binarySearch(a, "Mar", Collections.reverseOrder()));// Search wont work
                                                                                                        // here.

        Arrays.sort(a, Collections.reverseOrder());
        for (String str : a) {
            System.out.println(str);
        }

        System.out.println(Arrays.asList(a));// Array of primitive type cannot pass to this method

        // Avoid duplicates

        ArrayList<String> dList = new ArrayList<String>();
        dList.add("Sun");
        dList.add("Amay");
        dList.add("Amay");
        dList.add("Tar");

        Object oa[] = dList.toArray();
        Arrays.deepToString(oa);

        System.out.println(oa);
        Set<String> s = new HashSet<String>(dList);
        System.out.println("Deep to str :" + Arrays.toString(oa));

        System.out.println(Arrays.toString(a));
        Arrays.fill(a, "TTT");
        System.out.println(Arrays.toString(a));

    }

}
 
 

No comments: