25/03/25 ---------- Q)What are the differences between Iterator and ListIterator? Criteria Iterator ListIterator ************************************************ 1)How to by calling by calling get the refernce iterator() listIterator() of the cursor? on collection on collection 2)direction unidirectional bidirectional of traversing 3)can be applied any collection only list based collection to 4)operations READ & DELETE all CRUD operations on collection ************************************************************** Q)Example program on sorting of integers and Strings. //SortingTest1.java import java.util.*; class SortingTest1{ public static void main(String[] args){ ArrayList list1=new ArrayList<>(); Collections.addAll(list1,6,4,87,12,3,47); System.out.println("Before sorting:"+list1); Collections.sort(list1); System.out.println("After sorting:"+list1); ArrayList list2=new ArrayList<>(); Collections.addAll(list2,"aunt","uncle","daughter","son","father","mom"); System.out.println("Names before sorting:"+list2); Collections.sort(list2); System.out.println("Names after sorting:"+list2); } } Q)Example program on sorting of user defined objects(Employee instances) //SortingTest2.java import java.util.*; class Employee { int empno; String name; double salary; Employee(int empno,String name,double salary){ this.empno=empno; this.name=name; this.salary=salary; } public String toString(){ return empno+"\t"+name+"\t"+salary; }//overridingng Object class method to display object's state in textual format }//Employee class SortingTest2 { public static void main(String[] args) { ArrayList emps=new ArrayList<>(); emps.add(new Employee(102,"Rama",70000)); emps.add(new Employee(103,"Rahim",50000)); emps.add(new Employee(101,"Salman",60000)); System.out.println("Before sorting .......\n"); for(Employee e:emps) System.out.println(e); Collections.sort(emps); System.out.println("After sorting .......\n"); for(Employee e:emps) System.out.println(e); } } Note:- The above application causes compilation errors. Reason:- Collections.sort() method performs sorting of collection of objects if the objects are Comparable. =>java.lang.Integer class and java.lang.String class are Comparable. Therefore their sorting was working in SortingTest1.java Q)Explain about Comparable. =>java.lang.Comparable is a functional interface(that has only one method). =>This interface has the following abstract method. int compareTo(T);//any type of element =>Comparable interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class' natural ordering and compareTo() method is referred to as its natural comparision method. =>compareTo() method is used to compare the current object with the specified object. This method returns a +ve integer if the current object is greater than the specified object. It returns a -ve integer if the current object is lesser than the specified object. It returns zero if the current object is equal to the specified object. Q)Example program on Comparable interface. //ComparableApplication.java import java.util.*; class Employee implements Comparable { int empno; String name; double salary; Employee(int empno,String name,double salary){ this.empno=empno; this.name=name; this.salary=salary; } public String toString(){ return empno+"\t"+name+"\t"+salary; }//overridingng Object class method to display object's state in textual format public int compareTo(Employee e){ //return this.empno-e.empno; //return (int) (this.salary-e.salary); return this.name.compareTo(e.name);//sorting based on employee name } }//Employee class ComparableApplication { public static void main(String[] args) { ArrayList emps=new ArrayList<>(); emps.add(new Employee(102,"Rama",70000)); emps.add(new Employee(103,"Rahim",50000)); emps.add(new Employee(101,"Salman",60000)); System.out.println("Before sorting .......\n"); for(Employee e:emps) System.out.println(e); Collections.sort(emps); System.out.println("After sorting .......\n"); for(Employee e:emps) System.out.println(e); } } Q)What are the limitations of Comparable interface in sorting of the elements of a collection? 1)multiple sorting criteria can't be designed. 2)user defined object(element of colection object) is forced to implement Comparable interface in order to be sorted. Note:- java.util.Comparator interface overcomes these limitations.