• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is LinkedHashSet ordered?

 
James Quinton
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On what page do they make that statement?

I found the statement HashSets do not guarantee any ordering on page 562.
 
James Quinton
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The printed version is correct.

The error is in the electronic book.
 
Ade Barkah
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic is already in the errata list.

My (printed) copy of the book has the error as well; Must be an older printing.
 
Matt Russell
Ranch Hand
Posts: 165
 
Nidhi Singhal
Ranch Hand
Posts: 89
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LinkedHashSet is ordered by the order of insertion of elements.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic