Tuesday, January 6, 2015

TreeMap Methods Demo

1. Create Java Project.
2. Create package com.demo
3. Copy and paste the code.

 package com.demo;  
 import java.util.HashMap;  
 import java.util.Map;  
 import java.util.NavigableMap;  
 import java.util.SortedMap;  
 import java.util.TreeMap;  
 import java.util.Map.Entry;  
 import com.demo.model.Employee;  
 public class TreeMapMethodsDemo {  
      public static void main(String[] args) {  
           Map<String, String> treeMap = new TreeMap<String, String>();  
           treeMap.put("a", "aa");  
           treeMap.put("c", "cc");  
           treeMap.put("e", "ee");  
           treeMap.put("b", "bb");  
           System.out.println("--** printing the elements : constructor 1 **--");  
           for(Entry<String, String> entry : treeMap.entrySet()){  
                System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());  
           }  
           System.out.println("--** printing the elements : constructor 2 **--");  
           System.out.println("-- TreeMap with same salary having comparator --");  
           Map<Employee,Object> treeMap2 = new TreeMap<Employee,Object>(Employee.SalaryComparator);  
           treeMap2.put(new Employee("Ritu", 26, 10000), "Manager");  
           treeMap2.put(new Employee("Nupur", 28, 4000), "Assistant");  
           treeMap2.put(new Employee("Nagma", 40, 4000), "MD");  
           for(Entry<Employee, Object> entry : treeMap2.entrySet()){  
                System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());  
           }  
           System.out.println("-- TreeMap with diff salary having comparator --");  
           Map<Employee,Object> treeMap3 = new TreeMap<Employee,Object>(Employee.SalaryComparator);  
           treeMap3.put(new Employee("Ritu", 35, 12000), "Manager");  
           treeMap3.put(new Employee("Nupur", 56, 13000), "Assistant");  
           treeMap3.put(new Employee("Nagma", 24, 15000), "MD");  
           for(Entry<Employee, Object> entry3 : treeMap3.entrySet()){  
                System.out.println("key = "+entry3.getKey()+" val = "+entry3.getValue());  
           }  
           System.out.println("--** printing the elements : constructor 3 **--");  
           Map<String,String> map1 = new HashMap<String, String>();  
           map1.put("a", "A");  
           map1.put("b", "B");  
           Map<String,String> treeMap4 = new TreeMap<>(map1);  
           for(Entry<String, String> entry : treeMap4.entrySet()){  
                System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());  
           }  
           System.out.println("-- printing TreeMap values --");  
           TreeMap<Integer, String> teeMap = new TreeMap<Integer, String>();  
           teeMap.put(1, "AA");  
           teeMap.put(2, "BB");  
           teeMap.put(3, "CC");  
           teeMap.put(4, "DD");  
           teeMap.put(6, "GG");  
           teeMap.put(7, "UU");  
           teeMap.put(9, "FF");  
           teeMap.put(12, "OO");  
           for(Entry<Integer, String> entry : teeMap.entrySet()){  
                System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());  
           }  
           System.out.println("--printing first key => "+teeMap.firstKey());  
           System.out.println("--printing last key => "+teeMap.lastKey());  
           System.out.println("--poll first entry => "+teeMap.pollFirstEntry());  
           System.out.println("--poll last entry => "+teeMap.pollLastEntry());  
           System.out.println("--lower key => "+teeMap.lowerKey(3));  
           System.out.println("--lower entry => "+teeMap.lowerEntry(4));  
           System.out.println("--floor key => "+teeMap.floorKey(2));  
           System.out.println("--floor entry => "+teeMap.floorEntry(4));  
           System.out.println("-- ceilingKey => "+teeMap.ceilingKey(5));  
           System.out.println("-- ceilingEntry => "+teeMap.ceilingEntry(4));  
           System.out.println("-- Print the desending treeMap --");  
           NavigableMap<Integer, String> desTreeMap = teeMap.descendingMap();  
           for(Entry<Integer, String> entry : desTreeMap.entrySet()){  
                System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());  
           }  
           System.out.println("-- Print the sorted HeadMap --");  
           SortedMap<Integer, String> sortedHeadMap = teeMap.headMap(6);  
           for(Entry<Integer, String> entry : sortedHeadMap.entrySet()){  
                System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());  
           }  
           System.out.println("-- Print the sorted TailMap --");  
           SortedMap<Integer, String> sortedTailMap = teeMap.tailMap(6);  
           for(Entry<Integer, String> entry : sortedTailMap.entrySet()){  
                System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());  
           }  
           System.out.println("-- Print the sorted submap --");  
           SortedMap<Integer, String> sortedSubMap = teeMap.subMap(2, 7);  
           for(Entry<Integer, String> entry : sortedSubMap.entrySet()){  
                System.out.println("key = "+entry.getKey()+" val = "+entry.getValue());  
           }  
      }  
 }  
OUTPUT 

--** printing the elements : constructor 1 **--
key = a val = aa
key = b val = bb
key = c val = cc
key = e val = ee
--** printing the elements : constructor 2 **--
-- TreeMap with same salary having comparator --
key = Employee [name=Nupur, age=28, salary=4000.0] val = MD
key = Employee [name=Ritu, age=26, salary=10000.0] val = Manager
-- TreeMap with diff salary having comparator --
key = Employee [name=Ritu, age=35, salary=12000.0] val = Manager
key = Employee [name=Nupur, age=56, salary=13000.0] val = Assistant
key = Employee [name=Nagma, age=24, salary=15000.0] val = MD
--** printing the elements : constructor 3 **--
key = a val = A
key = b val = B
-- printing TreeMap values --
key = 1 val = AA
key = 2 val = BB
key = 3 val = CC
key = 4 val = DD
key = 6 val = GG
key = 7 val = UU
key = 9 val = FF
key = 12 val = OO
--printing first key => 1
--printing last key => 12
--poll first entry => 1=AA
--poll last entry => 12=OO
--lower key => 2
--lower entry => 3=CC
--floor key => 2
--floor entry => 4=DD
-- ceilingKey => 6
-- ceilingEntry => 4=DD
-- Print the desending treeMap --
key = 9 val = FF
key = 7 val = UU
key = 6 val = GG
key = 4 val = DD
key = 3 val = CC
key = 2 val = BB
-- Print the sorted HeadMap --
key = 2 val = BB
key = 3 val = CC
key = 4 val = DD
-- Print the sorted TailMap --
key = 6 val = GG
key = 7 val = UU
key = 9 val = FF
-- Print the sorted submap --
key = 2 val = BB
key = 3 val = CC
key = 4 val = DD
key = 6 val = GG
 

Monday, January 5, 2015

Collections Utility Class Methods Demo

1. Create the Java project.
2. Create the package com.demo.
3. Copy and paste the following code and run it.


 package com.demo;  
 import java.util.ArrayList;  
 import java.util.Collections;  
 import java.util.List;  
 import java.util.Random;  
 import com.demo.model.Employee;  
 public class CollectionsClassMethodsDemo {  
      public static void main(String[] args) {  
           List<Integer> list1 = new ArrayList<Integer>();  
           list1.add(5);  
           list1.add(3);  
           list1.add(2);  
           list1.add(4);  
           list1.add(1);  
           System.out.println("-- printing list1 elements --");  
           for(Integer list : list1){  
                System.out.println(list);  
           }  
           System.out.println("----------------------------------------");  
           System.out.println("-- start of collections class methods --");  
           System.out.println("----------------------------------------");  
           System.out.println("---------------------");  
           System.out.println("-- *** sorting *** --");  
           System.out.println("---------------------");  
           Collections.sort(list1);  
           System.out.println("-- printing list1 elements after sorting--");  
           for(Integer list : list1){  
                System.out.println(list);  
           }  
           System.out.println("-- Creating and printing the employees --");  
           Employee emp1 = new Employee("ABC", 49, 1000);  
           Employee emp2 = new Employee("XYZ", 30, 2000);  
           Employee emp3 = new Employee("UVW", 26, 4000);  
           List<Employee> employees = new ArrayList<Employee>();  
           employees.add(emp1);  
           employees.add(emp2);  
           employees.add(emp3);  
           for(Employee emp : employees){  
                System.out.println(emp);  
           }  
           System.out.println("-- printing employee list elements after natural sorting : which is by age --");  
           Collections.sort(employees);  
           for(Employee emp : employees){  
                System.out.println(emp);  
           }  
           System.out.println("-- printing employee list elements after sorting with Comparator : which is byName --");  
           Collections.sort(employees, Employee.NameComparator);  
           for(Employee emp : employees){  
                System.out.println(emp);  
           }  
           System.out.println("-----------------------");  
           System.out.println("-- *** searching *** --");  
           System.out.println("-----------------------");  
           System.out.println(" ** binarySearch **" +  
                     "\n /*The list must be sorted into ascending order according to the natural ordering of its elements*/" +  
                     "\n returns : \n 1. the index of the search key, if it is contained in the list" +  
                     "\n 2.(-(insertion point) - 1) : insertion point is defined as the point at which the key would be inserted into the list: " +  
                     "\n the index of the first element greater than the key"   
                     );  
           System.out.println("search index of key (2) => "+Collections.binarySearch(list1, 2));  
           System.out.println("search index of key (8) => "+Collections.binarySearch(list1, 8));  
           System.out.println("search index of key (0) => "+Collections.binarySearch(list1, 0));  
           System.out.println("-- using binarySearch 2nd form : which is comparator --" +  
                     "\n The list must be sorted into ascending order according to the specified comparator (as by the sort(List, Comparator) method), prior to making this call. " +  
                     "\n If it is not sorted, the results are undefined. " +  
                     "\n If the list contains multiple elements equal to the specified object, there is no guarantee which one will be found. ");  
           System.out.println("-- printing employee list elements after sorting with Comparator : which is bySalary--");  
           System.out.println("before sort => "+Collections.binarySearch(employees, emp2, Employee.SalaryComparator));  
           Collections.sort(employees, Employee.SalaryComparator);  
           for(Employee emp : employees){  
                System.out.println(emp);  
           }  
           System.out.println("after sort => "+Collections.binarySearch(employees, emp2, Employee.SalaryComparator));  
           System.out.println("---------------------");  
           System.out.println("-- *** reverse *** --");  
           System.out.println("---------------------");  
           System.out.println("-- reverse the emp list which was sorted with SalaryComparator --");  
           Collections.reverse(employees);  
           for(Employee emp : employees){  
                System.out.println(emp);  
           }  
           System.out.println("-----------------------");  
           System.out.println("-- *** shuffling *** --");  
           System.out.println("-----------------------");  
           Collections.shuffle(employees);  
           System.out.println("-- shuffle the emp list --");  
           for(Employee emp : employees){  
                System.out.println(emp);  
           }  
           System.out.println("-- shuffle the emp list with 2nd form --");  
           long seed = System.nanoTime();  
           Collections.shuffle(employees, new Random(seed));  
           for(Employee emp : employees){  
                System.out.println(emp);  
           }  
           System.out.println("-----------------------");  
           System.out.println("-- *** swapping *** --");  
           System.out.println("-----------------------");  
           System.out.println("-- swapping 2nd element with 4th of list1the --");  
           Collections.swap(list1, 2, 4);  
           for(Integer ele : list1){  
                System.out.println(ele);  
           }  
           System.out.println("------------------");  
           System.out.println("-- *** fill *** --");  
           System.out.println("------------------");  
           List<Integer> list2 = new ArrayList<>();  
           list2.add(9);  
           list2.add(10);  
           Collections.fill(list2, 1);  
           for(Integer ele : list2){  
                System.out.println(ele);  
           }  
           System.out.println("------------------");  
           System.out.println("-- *** Copy *** --");  
           System.out.println("------------------");  
           List<Integer> list3 = new ArrayList<Integer>();  
           list3.add(11);  
           list3.add(12);  
           list3.add(13);  
           list3.add(14);  
           list3.add(15);  
           Collections.copy(list3, list1);  
           for(Integer ele : list3){  
                System.out.println(ele);  
           }  
           System.out.println("------------------");  
           System.out.println("-- *** Min *** --");  
           System.out.println("------------------");  
           System.out.println("-- min of list3 --"+Collections.min(list3));  
           System.out.println("-- min of employees --"+Collections.min(employees));  
           System.out.println("-- min of employees 2nd form --"+Collections.min(employees,Employee.SalaryComparator));  
           System.out.println("------------------");  
           System.out.println("-- *** Max *** --");  
           System.out.println("------------------");  
           System.out.println("-- max of list3 --"+Collections.max(list3));  
           System.out.println("-- max of employees --"+Collections.max(employees));  
           System.out.println("-- max of employees 2nd form --"+Collections.max(employees,Employee.SalaryComparator));  
           System.out.println("--------------------");  
           System.out.println("-- *** Rotate *** --");  
           System.out.println("--------------------");  
           System.out.println("-- Before rotation --");  
           for(Integer ele : list3){  
                System.out.println(ele);  
           }  
           Collections.rotate(list3, 2);  
           System.out.println("-- after rotation --");  
           for(Integer ele : list3){  
                System.out.println(ele);  
           }  
           System.out.println("-------------------------");  
           System.out.println("-- *** Replace All *** --");  
           System.out.println("-------------------------");  
           List<Integer> list4 = new ArrayList<Integer>();  
           list4.add(22);  
           list4.add(23);  
           list4.add(25);  
           list4.add(22);  
           System.out.println("-- Before replace --");  
           for(Integer ele : list4){  
                System.out.println(ele);  
           }  
           Collections.replaceAll(list4, 22, 21);  
           System.out.println("-- after replace --");  
           for(Integer ele : list4){  
                System.out.println(ele);  
           }  
           System.out.println("------------------------");  
           System.out.println("-- *** frequency *** --");  
           System.out.println("------------------------");  
           System.out.println("-- frequency of 21 in list4 -- "+Collections.frequency(list4, 21));  
           System.out.println("-----------------------");  
           System.out.println("-- *** disjoint *** --");  
           System.out.println("-- true if the two specified collections have no elements in common --");  
           System.out.println("-----------------------");  
           List<Integer> list5 = new ArrayList<Integer>();  
           list5.add(22);  
           list5.add(23);  
           System.out.println("-- elements of list5 --");  
           for(Integer ele : list5){  
                System.out.println(ele);  
           }  
           System.out.println("-- disjoint of list4 & list5 --"+Collections.disjoint(list4, list5));  
           System.out.println("----------------------------");  
           System.out.println("-- *** indexOfSubList *** --");  
           System.out.println("----------------------------");  
           List<String> list6 = new ArrayList<String>();  
           list6.add("I");  
           list6.add("We");  
           list6.add("You");  
           list6.add("They");  
           List<String> list7 = new ArrayList<String>();  
           list7.add("You");  
           list7.add("They");  
           System.out.println("-- indexOfSubList list5 in list4 -- "+Collections.indexOfSubList(list6, list7));  
           System.out.println("--------------------------------");  
           System.out.println("-- *** lastIndexOfSubList *** --");  
           System.out.println("--------------------------------");  
           System.out.println("-- lastIndexOfSubList list5 in list4 -- "+Collections.lastIndexOfSubList(list6, list7));  
      }  
 }  


OUTPUT




-- printing list1 elements --
5
3
2
4
1
----------------------------------------
-- start of collections class methods --
----------------------------------------
---------------------
-- *** sorting *** --
---------------------
-- printing list1 elements after sorting--
1
2
3
4
5
-- Creating and printing the employees --
Employee [name=ABC, age=49, salary=1000.0]
Employee [name=XYZ, age=30, salary=2000.0]
Employee [name=UVW, age=26, salary=4000.0]
-- printing employee list elements after natural sorting : which is by age --
Employee [name=UVW, age=26, salary=4000.0]
Employee [name=XYZ, age=30, salary=2000.0]
Employee [name=ABC, age=49, salary=1000.0]
-- printing employee list elements after sorting with Comparator : which is byName --
Employee [name=ABC, age=49, salary=1000.0]
Employee [name=UVW, age=26, salary=4000.0]
Employee [name=XYZ, age=30, salary=2000.0]
-----------------------
-- *** searching *** --
-----------------------
 ** binarySearch **
 /*The list must be sorted into ascending order according to the natural ordering of its elements*/
 returns :
 1. the index of the search key, if it is contained in the list
 2.(-(insertion point) - 1) : insertion point is defined as the point at which the key would be inserted into the list:
 the index of the first element greater than the key
search index of key (2) => 1
search index of key (8) => -6
search index of key (0) => -1
-- using binarySearch 2nd form : which is comparator --
 The list must be sorted into ascending order according to the specified comparator (as by the sort(List, Comparator) method), prior to making this call.
 If it is not sorted, the results are undefined.
 If the list contains multiple elements equal to the specified object, there is no guarantee which one will be found.
-- printing employee list elements after sorting with Comparator : which is bySalary--
before sort => -2
Employee [name=ABC, age=49, salary=1000.0]
Employee [name=XYZ, age=30, salary=2000.0]
Employee [name=UVW, age=26, salary=4000.0]
after sort => 1
---------------------
-- *** reverse *** --
---------------------
-- reverse the emp list which was sorted with SalaryComparator --
Employee [name=UVW, age=26, salary=4000.0]
Employee [name=XYZ, age=30, salary=2000.0]
Employee [name=ABC, age=49, salary=1000.0]
-----------------------
-- *** shuffling *** --
-----------------------
-- shuffle the emp list --
Employee [name=UVW, age=26, salary=4000.0]
Employee [name=XYZ, age=30, salary=2000.0]
Employee [name=ABC, age=49, salary=1000.0]
-- shuffle the emp list with 2nd form  --
Employee [name=UVW, age=26, salary=4000.0]
Employee [name=ABC, age=49, salary=1000.0]
Employee [name=XYZ, age=30, salary=2000.0]
-----------------------
-- *** swapping *** --
-----------------------
-- swapping 2nd element with 4th of list1the --
1
2
5
4
3
------------------
-- *** fill *** --
------------------
1
1
------------------
-- *** Copy *** --
------------------
1
2
5
4
3
------------------
-- *** Min *** --
------------------
-- min of list3 --1
-- min of employees --Employee [name=UVW, age=26, salary=4000.0]
-- min of employees 2nd form --Employee [name=ABC, age=49, salary=1000.0]
------------------
-- *** Max *** --
------------------
-- max of list3 --5
-- max of employees --Employee [name=ABC, age=49, salary=1000.0]
-- max of employees 2nd form --Employee [name=UVW, age=26, salary=4000.0]
--------------------
-- *** Rotate *** --
--------------------
-- Before rotation --
1
2
5
4
3
-- after rotation --
4
3
1
2
5
-------------------------
-- *** Replace All *** --
-------------------------
-- Before replace --
22
23
25
22
-- after replace --
21
23
25
21
------------------------
-- *** frequency *** --
------------------------
-- frequency of 21 in list4 -- 2
-----------------------
-- *** disjoint *** --
-- true if the two specified collections have no elements in common --
-----------------------
-- elements of list5 --
22
23
-- disjoint of list4 & list5 --false
----------------------------
-- *** indexOfSubList *** --
----------------------------
-- indexOfSubList list5 in list4 -- 2
--------------------------------
-- *** lastIndexOfSubList *** --
--------------------------------
-- lastIndexOfSubList list5 in list4 -- 2

Collection Interface Methods Demo

1. Create a java project.
2. create the package com.demo under src folder.
3. copy & paste the following class code.

 package com.demo;  
 import java.util.ArrayList;  
 import java.util.Iterator;  
 import java.util.List;  
 public class CollectionIterfaceMethodDemo {  
      public static void main(String[] args) {  
           List<String> demoList1 = new ArrayList<String>();  
           demoList1.add("A");  
           demoList1.add("B");  
           demoList1.add("C");  
           demoList1.add("D");  
           System.out.println("-- Printing elements of List1 using iterator with WHILE --");  
           Iterator<String> itr = demoList1.iterator();  
           while (itr.hasNext()) {  
                Object element = itr.next();  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("-- Printing elements of List1 using iterator with FOR --");  
           for (Iterator<String> flavoursIter = demoList1.iterator(); flavoursIter.hasNext();){  
                 Object element = flavoursIter.next();  
                 System.out.print(element + " ");  
             }  
           List<String> demoList2 = new ArrayList<String>();  
           demoList2.add("E");  
           demoList2.add("F");  
           demoList2.add("G");  
           demoList2.add("H");  
           System.out.println();  
           System.out.println("-- Prinitng elements of List2 with For-Each loop --");  
           for (String element : demoList2) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("--------------------------------------------");  
           System.out.println("-- start of Collection interface methods --");  
           System.out.println("--------------------------------------------");  
           List<String> demoList3 = new ArrayList<String>();  
           System.out.println("* demoList3.isEmpty() => " + demoList3.isEmpty());  
           System.out.println("* demoList1.contains('B') => "  
                     + demoList1.contains("B"));  
           System.out.println("* demoList1.contains('E') => "  
                     + demoList1.contains("E"));  
           System.out.println("* demoList2.size() => " + demoList2.size());  
           System.out.println("-- adding elements to List3 --");  
           demoList3.add("I");  
           demoList3.add("J");  
           demoList3.add("G");  
           for (String element : demoList3) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("------------------------------------------------");  
           System.out.println("-- start of Collection interface bulk methods --");  
           System.out.println("------------------------------------------------");  
           System.out.println("* demoList1.containsAll(demoList3) => "  
                     + demoList1.containsAll(demoList3));  
           System.out.println("* demoList2.containsAll(demoList3) => "  
                     + demoList2.containsAll(demoList3));  
           System.out.println("* demoList2.addAll(demoList3) => "  
                     + demoList2.addAll(demoList3));  
           System.out.println("-- Printing demoList2 after addAll() --");  
           for (String element : demoList2) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("* demoList2.retainAll(demoList3) => "  
                     + demoList2.retainAll(demoList3));  
           System.out.println("-- Printing demoList2 after retainAll() --");  
           for (String element : demoList2) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("* demoList2.removeAll(demoList3) => "  
                     + demoList2.removeAll(demoList3));  
           System.out.println("-- demoList2 size after demoList2.removeAll() => "  
                     + demoList2.size());  
           demoList3.clear();  
           System.out.println();  
           System.out.println("* demoList3.clear() => " + demoList3.size());  
           System.out.println("----------------------------------------------------------");  
           System.out.println(" == removing element 'C' from demoList1 using iterator ==");  
           System.out.println("----------------------------------------------------------");  
           Iterator<String> itr2 = demoList1.iterator();  
           while (itr2.hasNext()) {  
                if (itr2.next().equals("C")) {  
                     itr2.remove();  
                }  
           }  
           for (String element : demoList1) {  
                System.out.print(element + " ");  
           }  
           System.out.println();  
           System.out.println("----------------------------------------------");  
           System.out.println("== converting list1 to array with toArray() ==");  
           System.out.println("----------------------------------------------");  
           Object[] arr = demoList1.toArray();  
           for (Object element : arr) {  
                System.out.println("Element = " + element);  
           }  
           System.out.println("---------------------------------------------------------");  
           System.out.println("== converting list1 to array again with toArray(T[] a) ==");  
           System.out.println("---------------------------------------------------------");  
           String[] strArr = demoList1.toArray(new String[demoList1.size()] );  
           for (Object element : strArr) {  
                System.out.println("Element = " + element);  
           }  
      }  
 }  

Employee Class -
 package com.demo.model;  
 import java.util.Comparator;  
 public class Employee implements Comparable<Employee> {  
      private String name;  
      private int age;  
      private double salary;  
      public Employee(String name, int age, double salary) {  
           super();  
           this.name = name;  
           this.age = age;  
           this.salary = salary;  
      }  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public int getAge() {  
           return age;  
      }  
      public void setAge(int age) {  
           this.age = age;  
      }  
      public double getSalary() {  
           return salary;  
      }  
      public void setSalary(double salary) {  
           this.salary = salary;  
      }  
      @Override  
      public int compareTo(Employee emp) {  
           // TODO Auto-generated method stub  
           return (this.age - emp.age);  
      }  
      @Override  
      public String toString() {  
           return "Employee [name=" + name + ", age=" + age + ", salary=" +salary +"]";  
      }  
      public static Comparator<Employee> NameComparator = new Comparator<Employee>() {  
     @Override  
     public int compare(Employee e1, Employee e2) {  
       return e1.getName().compareTo(e2.getName());  
     }  
   };  
   public static Comparator<Employee> SalaryComparator = new Comparator<Employee>() {  
     @Override  
     public int compare(Employee e1, Employee e2) {  
       return (int)(e1.getSalary() - e2.getSalary());  
     }  
   };  
 }  
OUTPUT 

-- Printing elements of List1 using iterator with WHILE --
A B C D
-- Printing elements of List1 using iterator with FOR --
A B C D
-- Prinitng elements of List2 with For-Each loop --
E F G H
--------------------------------------------
-- start of Collection interface methods --
--------------------------------------------
* demoList3.isEmpty() => true
* demoList1.contains('B') => true
* demoList1.contains('E') => false
* demoList2.size() => 4
-- adding elements to List3 --
I J G
------------------------------------------------
-- start of Collection interface bulk methods --
------------------------------------------------
* demoList1.containsAll(demoList3) => false
* demoList2.containsAll(demoList3) => false
* demoList2.addAll(demoList3) => true
-- Printing demoList2 after addAll() --
E F G H I J G
* demoList2.retainAll(demoList3) => true
-- Printing demoList2 after retainAll() --
G I J G
* demoList2.removeAll(demoList3) => true
-- demoList2 size after demoList2.removeAll() => 0

* demoList3.clear() => 0
----------------------------------------------------------
 == removing element 'C' from demoList1 using iterator ==
----------------------------------------------------------
A B D
----------------------------------------------
== converting list1 to array with toArray() ==
----------------------------------------------
Element = A
Element = B
Element = D
---------------------------------------------------------
== converting list1 to array again with toArray(T[] a) ==
---------------------------------------------------------
Element = A
Element = B
Element = D