Public Class Public Static Void Final New: What Are Fail-Fast Iterators in Java? Explain With Code?
Public Class Public Static Void Final New: What Are Fail-Fast Iterators in Java? Explain With Code?
Iterator<String> it = list.iterator();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
while(it.hasNext()){
System.out.println(it.next());
}
}
}
When you run this code you get ConcurrentModificationException thrown at line
it.next()After the creation of Iterator, the container cannot be modified at any time by any
method other than the Iterator's own remove or add methods. Iterators that do this are
known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-
deterministic behavior at an undetermined time in the future.
If you change the code in the following way even then it throws the same Exception
list.add("a");
list.add("b");
list.add("c");
Iterator<String> it = list.iterator();
list.add("d");
while(it.hasNext()){
System.out.println(it.next());
}
Note: http://www.jroller.com/rajasekar/entry/java_fail_fast_iterators_exposed
You can get the detailed explanation in the above mentioned url.
Demonstrate the Hashtable class, and an Enumeration
import java.util.Enumeration;
import java.util.Hashtable;
Enumeration k = h.keys();
while(k.hasMoreElements()) {
String key = (String) k.nextElement();
System.out.println("Key " + key ));
System.out.println(" Value " + (String) h.get(key));
}
Set set= h.keySet();
Iterator iter = set.iterator ( ) ;
int i=1;
while ( iter.hasNext () ) {
System.out.println( ""+i+" ) "+h.get (iter.next ( ))) ;
i++;
}
}
}
Comparable vs Comparator
Comparable
A comparable object is capable of comparing itself with another object. The class
itself must implements the java.lang.Comparable interface in order to be able to
compare its instances.
Comparator
A comparator object is capable of comparing two different objects. The class is not
comparing its instances, but some other class’s instances. This comparator class
must implement the java.lang.Comparator interface.
Example on Comparable
package comparable.ex01;
Testing Class
package comparable.ex01;
import java.util.Arrays;
import java.util.ArrayList;
System.out.println("Natural Order");
Arrays.sort(persons);
System.out.println();
System.out.println("Sorted by age");
Sorted by age
Clark, Stanley. Age:8
Graff, Jane. Age:16
Goodyear, Elvis. Age:56
Goodyear, Nancy. Age:69
Example on Compararator interface
Person Class
package comparable.ex02;
package comparable.ex02;
import java.util.Comparator;
if (!(lastName1.equals(lastName2)))
return lastName1.compareTo(lastName2);
else
return firstName1.compareTo(firstName2);
}
}
FirstNameComparator Class
package comparable.ex02;
import java.util.Comparator;
Testing Class
package comparable.ex02;
import java.util.Arrays;
import java.util.ArrayList;
System.out.println("Natural Order");
for (int i=0; i<4; i++) {
Person person = persons[i];
String lastName = person.getLastName();
String firstName = person.getFirstName();
int age = person.getAge();
System.out.println(lastName + ", " + firstName + ". Age:" + age);
}
Arrays.sort(persons);
System.out.println();
System.out.println("Sorted by age");
Natural Order
Goodyear, Elvis. Age:56
Clark, Stanley. Age:8
Graff, Jane. Age:16
Goodyear, Nancy. Age:69
Sorted by age
Clark, Stanley. Age:8
Graff, Jane. Age:16
Goodyear, Elvis. Age:56
Goodyear, Nancy. Age:69
Note: In Natural Order we did not sort the array , so elements appeared in the way
we have entered.
import java.util.*;
vec.addElement(new Integer(-200));
vec.addElement(new Integer(100));
vec.addElement(new Integer(400));
vec.addElement(new Integer(-300));
}
}
}
Note : similarly case is for arrays also. In arrays the following instruction we have to
use.
Arrays.sort(employee, new Comparer());