my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is LinkedHashSet ordered? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "is LinkedHashSet ordered?" Watch "is LinkedHashSet ordered?" New topic

is LinkedHashSet ordered?

James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
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();
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Oct 02, 2006
Posts: 94
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] + " ");
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

Joined: Feb 07, 2005
Posts: 2367
The printed version is correct.

The error is in the electronic book.
Ade Barkah
Ranch Hand

Joined: Mar 17, 2004
Posts: 65
This topic is already in the errata list.

My (printed) copy of the book has the error as well; Must be an older printing.

-Ade<br /><a href="" target="_blank" rel="nofollow"></a>
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
It's mentioned in the list of errata:

Inquisition: open-source mock exam simulator for SCJP and SCWCD
Nidhi Singhal
Ranch Hand

Joined: Sep 19, 2004
Posts: 89

LinkedHashSet is ordered by the order of insertion of elements.
Don't get me started about those stupid light bulbs.
subject: is LinkedHashSet ordered?
Similar Threads
Difference between ordering by index and ordering by position
Interface Set's get() method>>>>
Please help me to explain this result
Collection VS ArrayList.