• Post Reply Bookmark Topic Watch Topic
  • New Topic

Want Map without sort  RSS feed

 
pavan in
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am storing few values in a map. when I retrieve those values using the iterator, the values are obtained in some sorted manner. I want the values to be obtained in the same order as I had stored. How can I do that? I dont want to use Hashtable.
Say,I have stored values 56, 10, 35. When I retrieve the values, I should get it in same order and not in order 10, 35, 56.
Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the LinkedHashMap in JDK 1.4.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Since you can't add primitive type directly to the map or list. And It looks that you are don't want to use name-value pair. Why don't you try ArrayList. See the sample code below.
-arun
import java.util.Iterator;
import java.util.ArrayList;
public class OrderTest
{
public static void main( String args[] )
{
ArrayList oList = new ArrayList();
oList.add( String.valueOf(10) );
oList.add( String.valueOf(30) );
oList.add( String.valueOf(20) );
for ( Iterator itIndex = oList.iterator(); itIndex.hasNext(); )
{
System.out.println( itIndex.next() );
}
}
}
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pavan in:

When I retrieve the values, I should get it in same order and not in order 10, 35, 56.

If you want to retrieve the values in ANY particular order it is recommend that you don't use any Iterator. When you use an Iterator you can't write code that depends on recieveing the elements in a particular order. Because in the Iterator contract the only thing that it specifies is that you recieve every element exactly once, but the sequence is not mentioned.
So lets say that you do use the ArrayList.iterator() and it returns the elements in the order you want now. On the next release of Java they might re-implement that Iterator and your code is broken. I admit this is kinda far fetched, but none the less you don't want to get into bad programming practices.
If you need to have an Iterator which returns elements in a specific order you should just write your own. If you use and ArrayList its not really that hard. If you need I can show you how to do this.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input Ibrahim Hashimi.
Since i was making use this approch. I had implemented the same in one of my project. Can you tell me how to change the same with min impact.
-arun
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to retrieve the values in ANY particular order it is recommend that you don't use any Iterator.
Not true, Ibrahim. The general Iterator contract explicitly does not make any guarantees about order of iteration, true, but it also does not prevent other classes or interfaces from making guarantees about the behavior of a particular implementation of Iterator. Thus for example, the documentation for the iterator() method in the List interface can say:
Returns an iterator over the elements in this list in proper sequence.

Thus the Iterator for any List must return elements in the sequence they occur in the List. Sun may not change this in future releases without violating their published API for List. Similarly, the LinkedHashMap is described in the API as:
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

(Don't be put off by "normally" - this is explained in more detail later. The behavior is well-defined and predictable.)
Regarding Arun's comments: true. I assumed that Pavan actually does have a reason to use a Map as he said, even though it's not apparent in the example. If there aren't actually any key-value pairs, then use an ArrayList or LinkedList; if there are, use a LinkedHashList.
[ December 06, 2002: Message edited by: Jim Yingst ]
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, I dindn't look at the documentation for List. I didn't realize that it said:
Returns an iterator over the elements in this list in proper sequence.
I was simply trying to point out that the java.util.Iterator interface didn't say anything about the order of the elements. Thanks for the input.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!