Hibernate Tutorial

(Brent) #1

TUTORIALS POINT


}


System.out.println();

// Modify objects being iterated
ListIterator litr = al.listIterator();
while(litr.hasNext()) {
Object element = litr.next();
litr.set(element + "+");
}
System.out.print("Modified contents of al: ");
itr = al.iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();

// Now, display the list backwards
System.out.print("Modified list backwards: ");
while(litr.hasPrevious()) {
Object element = litr.previous();
System.out.print(element + " ");
}
System.out.println();
}
}

This would produce the following result:


Original contents of al: C A E B D F
Modified contents of al: C+ A+ E+ B+ D+ F+
Modified list backwards: F+ D+ B+ E+ A+ C+

How to use a Comparator?


Both TreeSet and TreeMap store elements in sorted order. However, it is the comparator that defines precisely
what sorted order means.


This interface lets us sort a given collection any number of different ways. Also, this interface can be used to sort
any instances of any class(even classes we cannot modify).


SN Iterator Methods with Description

1


Using Java Comparator


Here is a list of all the methods with examples provided by Comparator Interface.

Using Java Comparator


Both TreeSet and TreeMap store elements in sorted order. However, it is the comparator that defines precisely
what sorted order means.


The Comparator interface defines two methods: compare( ) and equals( ). The compare( ) method, shown here,
compares two elements for order:


The compare Method:


int compare(Object obj1, Object obj2)
Free download pdf