-3

May i know why my hashtable number is not in order? after reach certain number. Please, really need help from expert. Any helps would be appreciated. Thanks alot!! Code in jsp file.

<%
Vector vRow2      = new Vector();
Vector vFruit     = new Vector(); 
Hashtable htItem  = new Hashtable();

vRow2.addElement("apple");
vRow2.addElement("banana");
vFruit.addElement(vRow2);

htItem.put("1", vFruit);
htItem.put("2", vFruit);
htItem.put("3", vFruit);
htItem.put("4", vFruit);
htItem.put("5", vFruit);
htItem.put("6", vFruit); // if htItem put only up to 6 it show correct order result 6,5,4,3,2,1
htItem.put("7", vFruit); // if htItem put up to 7 it show incorrect order result 6,5,4,3,2,1,7
htItem.put("8", vFruit); // if htItem put up to 8 it show result 6,5,4,3,2,1,8,7
htItem.put("9", vFruit); // if htItem put up to 9 it show correct order 9,8,7,6,5,4,3,2,1
htItem.put("10", vFruit); // if htItem put up to 10 it show incorrect order result 9,8,6,5,4,3,2,10,1

System.err.println("htItem==="+htItem);
%>

output

htItem==={9=[[apple, banana]], 8=[[apple, banana]], 7=[[apple, banana]], 6=[[apple, banana]], 5=[[apple, banana]], 4=[[apple, banana]], 3=[[apple, banana]], 2=[[apple, banana]], 10=[[apple, banana]], 1=[[apple, banana]]}

expected output : 10,9,8,7,6,5,4,3,2,1

how to make the number of list are in correct sequence every time htItem put new number.?

lmo
  • 497
  • 5
  • 23
user3835327
  • 1,194
  • 1
  • 14
  • 44

5 Answers5

2

Read Documentation and you'll understand that HASHTABLE is not ordered, but a TreeMap offers a compare mechanism like here

Community
  • 1
  • 1
Hichamov
  • 636
  • 6
  • 8
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Alex Char Dec 16 '14 at 10:34
  • Tried once to copy pertinent part of a link in my answer and got downvoted by another person ... – Hichamov Dec 16 '14 at 10:36
  • This doesn't make the current answer better. – Markus W Mahlberg Dec 16 '14 at 10:52
  • All the links are to Oracle docs (They will be around for a long time) or StackOverFlow questions. Also, me not detailing answer because the question shouldn't be asked at all, since the smallest google serch gives directe link to [duplicate](http://stackoverflow.com/questions/1419708/how-to-keep-the-order-of-elements-in-hashtable?answertab=active#tab-top) raised in the comments – Hichamov Dec 16 '14 at 11:00
2

Keys in hash table are not ordered by definition. The order depends on the key's hashCode().

If you want to have your keys sorted use either LinkedHashMap that guarantees that keys will be retrieve in the same order as they were added or TreeMap with appropriate Comparator that guarantees that keys will be retrieved according to Comparator implementation.

Since you are using contained number strings as keys you will have to implement comparator like following:

public class NumericStringComparator() {
    public int compare(String s1, String s2) {
        return Integer.parseInt(s1) - Integer.parseInt(s2);
    }
}

Please note that my version is not null safe. You are welcome to improve it.

AlexR
  • 114,158
  • 16
  • 130
  • 208
2

You need a list and not a hashtable to get a correct ordered list.

List<Vector> list = new ArrayList<Vector>();
list.add(0,vFruit);
list.add(1,vFruit);
list.add(2,vFruit);
list.add(3,vFruit);
list.add(vFruit); //<- index 5 
TwilightTitus
  • 190
  • 1
  • 9
0

HashTable is not ordered by key. Instead of using HashTable, you should use TreeMap which will sort by key that you enter. Since you need your map to be sorted by key in descending order you could do the following:

Map<String, Vector> map = new TreeMap<String, Vector>(
            new Comparator<String>() {
                public int compare(String key1, String key2) {
                    return Integer.parseInt(key2) - Integer.parseInt(key1);
                }
            });
SMA
  • 36,381
  • 8
  • 49
  • 73
0

Well this works. I've made some changes using Eclispe. The problem may be the size of the Hashtable, which might mean the variables were not byte aligned in memory.The key value pairs would then be store in memory in a non sequiential order.

// added <String>
Vector<String> vRow2      = new Vector<String>();
Vector<String> vFruit     = new Vector<String>(); 
Hashtable<String, Vector<String>> htItem  = new Hashtable<String, Vector<String>>();

vRow2.addElement("apple");
vRow2.addElement("banana");
vFruit.addAll(vRow2);    //changed to addAll()

htItem.put("0", vFruit);
//the above line of code makes everything work
//I don't understand why tho
htItem.put("1", vFruit);
htItem.put("2", vFruit);
htItem.put("3", vFruit);
htItem.put("4", vFruit);
htItem.put("5", vFruit);
htItem.put("6", vFruit); // if htItem put only up to 6 it show correct order result 6,5,4,3,2,1
htItem.put("7", vFruit); // if htItem put up to 7 it show incorrect order result 6,5,4,3,2,1,7
htItem.put("8", vFruit); // if htItem put up to 8 it show result 6,5,4,3,2,1,8,7
htItem.put("9", vFruit); // if htItem put up to 9 it show correct order 9,8,7,6,5,4,3,2,1
htItem.put("10", vFruit); // if htItem put up to 10 it show incorrect order result 9,8,6,5,4,3,2,10,1

//loop added to list each key
int i = 0;
for (i=0; i < htItem.size(); i++){
//modified to print results
    String res = String.valueOf(i);
    System.err.println("htItem==="+res+"\t"+htItem.get(res));
}

Output in Eclipse console:
htItem===0 [apple, banana]
htItem===1 [apple, banana]
htItem===2 [apple, banana]
htItem===3 [apple, banana]
htItem===4 [apple, banana]
htItem===5 [apple, banana]
htItem===6 [apple, banana]
htItem===7 [apple, banana]
htItem===8 [apple, banana]
htItem===9 [apple, banana]
htItem===10 [apple, banana]

Output without key "0":

htItem===0 null
htItem===1 [apple, banana]
htItem===2 [apple, banana]
htItem===3 [apple, banana]
htItem===4 [apple, banana]
htItem===5 [apple, banana]
htItem===6 [apple, banana]
htItem===7 [apple, banana]
htItem===8 [apple, banana]
htItem===9 [apple, banana]

To reverse output change loop to:

int i = htItem.size()-1;
while (i >= 1){
    String res = String.valueOf(i);
    System.err.println("htItem==="+res+"\t"+htItem.get(res));
    i--;
}

Eclipse output:

htItem===10 [apple, banana]
htItem===9 [apple, banana]
htItem===8 [apple, banana]
htItem===7 [apple, banana]
htItem===6 [apple, banana]
htItem===5 [apple, banana]
htItem===4 [apple, banana]
htItem===3 [apple, banana]
htItem===2 [apple, banana]
htItem===1 [apple, banana]

After editing for reformatted output:

int i = htItem.size()-1;
        System.err.print("htItem==={");
        while (i >= 1){
            String res = String.valueOf(i);
            System.err.print(res+"=["+htItem.get(res)+"], ");
            i--;
            if (i == 1){
                res = String.valueOf(i);
                System.err.print(res+"=["+htItem.get(res)+"]");
                System.err.println("}");
                return;
            }
        }

Eclipse output:

htItem==={10=[[apple, banana]], 9=[[apple, banana]], 8=[[apple, banana]], 7=[[apple, banana]], 6=[[apple, banana]], 5=[[apple, banana]], 4=[[apple, banana]], 3=[[apple, banana]], 2=[[apple, banana]], 1=[[apple,banana]]}

  • i need to display it in 1 htItem .. which result = htItem==={10=[[apple, banana]], 9=[[apple, banana]], 8=[[apple, banana]], 7=[[apple, banana]], 6=[[apple, banana]], 5=[[apple, banana]], 4=[[apple, banana]], 3=[[apple, banana]], 2=[[apple, banana]], 1=[[apple, banana]]} – user3835327 Dec 17 '14 at 01:31