posted 11 years ago
K&B SCJP5 book 7.2 says:
HashSets and LinkedHashSets do not guarantee any ordering.
I doubt it (not mentioning the typo "s" following the class name).
Is LinkedHashSet supposed to be ordered?
I tried following code:
Set<String> s = new LinkedHashSet();
s.add("a");
s.add("b");
s.add("c");
s.add("d");
for(String n: s){
System.err.print(n+" ");
}
It consistently gives me ordered output: a b c d
Actually the book is contradictory to itself on 7.1 says LinkedHashSet is ordered by insertion order.
HashSets and LinkedHashSets do not guarantee any ordering.
I doubt it (not mentioning the typo "s" following the class name).
Is LinkedHashSet supposed to be ordered?
I tried following code:
Set<String> s = new LinkedHashSet();
s.add("a");
s.add("b");
s.add("c");
s.add("d");
for(String n: s){
System.err.print(n+" ");
}
It consistently gives me ordered output: a b c d
Actually the book is contradictory to itself on 7.1 says LinkedHashSet is ordered by insertion order.
James Quinton
Ranch Hand
Posts: 94
posted 11 years ago
same page number 562. oh. boy. what happened to my book? I copied the following excerpt from the CD on chapter 7.2:
Using Sets
Remember that Sets are used when you don't want any duplicates in your collection. If you attempt to add an element to a set that already exists in the set, the duplicate element will not be added, and the add() method will return false. Remember, HashSets tend to be very fast because, as we discussed earlier, they use hashcodes.
You can also create a TrecSet, which is a Set whose elements are sorted. You must use caution when using a TreeSet (we're about to explain why):
import java.util.*;
class SetTest {
public static void main(String [] args) {
boolean[] ba = new boolean[5];
// insert code here
ba[0] = s.add("a");
ba[1] = s.add(new Integer(42));
ba[2] = s.add("b") ;
ba[3] = s.add("a") ;
ba[4] = s.add(new Object());
for(int x=0; x<ba.length; x++)
System.out.print(ba[x] + " ");
System.out.println("\n");
for(Object o : s)
System.out.print(o + " ");
}
}
If you insert the following line of code you'll get output something like this:
Set s = new HashSet(); // insert this code
true true true false true
a java.lang.Object@e09713 42 b
It's important to know that the order of objects printed in the second for loop is not predictable: HashSets and LinkedHashSets do not guarantee any ordering.
Using Sets
Remember that Sets are used when you don't want any duplicates in your collection. If you attempt to add an element to a set that already exists in the set, the duplicate element will not be added, and the add() method will return false. Remember, HashSets tend to be very fast because, as we discussed earlier, they use hashcodes.
You can also create a TrecSet, which is a Set whose elements are sorted. You must use caution when using a TreeSet (we're about to explain why):
import java.util.*;
class SetTest {
public static void main(String [] args) {
boolean[] ba = new boolean[5];
// insert code here
ba[0] = s.add("a");
ba[1] = s.add(new Integer(42));
ba[2] = s.add("b") ;
ba[3] = s.add("a") ;
ba[4] = s.add(new Object());
for(int x=0; x<ba.length; x++)
System.out.print(ba[x] + " ");
System.out.println("\n");
for(Object o : s)
System.out.print(o + " ");
}
}
If you insert the following line of code you'll get output something like this:
Set s = new HashSet(); // insert this code
true true true false true
a java.lang.Object@e09713 42 b
It's important to know that the order of objects printed in the second for loop is not predictable: HashSets and LinkedHashSets do not guarantee any ordering.
Keith Lynn
Ranch Hand
Posts: 2410
posted 11 years ago
It's mentioned in the list of errata:
http://www.coderanch.com/t/257589/java-programmer-SCJP/certification/SCJP-Errata-Updated
http://www.coderanch.com/t/257589/java-programmer-SCJP/certification/SCJP-Errata-Updated
Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
