When i have a SortedMap in Java (TreeMap to be precise) it does not allow for duplicate keys. Is there ANY already implemented data structure in Java with same functionality as TreeMap (ordered insertion) that does allow duplicates or is there a way tweaking TreeMap to let it store duplicate keys.
My sample code:
package com.test;
import java.util.*;
public class Main {
public static void main(String[] args)
{
SortedMap<Integer, String> sm = new TreeMap<Integer,String>();
sm.put(2, "TWO");
sm.put(5, "FIVE");
sm.put(10, "TEN");
sm.put(1, "ONE");
sm.put(0, "ZERO");
sm.put(0, "THOUSAD");
Set s = sm.entrySet();
Iterator i = s.iterator();
while(i.hasNext()){
Map.Entry m = (Map.Entry) i.next();
System.out.println(m.getKey() + " " +m.getValue());
}
}//end main method
}
Output:
0 THOUSAD
1 ONE
2 TWO
5 FIVE
10 TEN

New Topic/Question
Reply




MultiQuote







|