7 Replies - 9319 Views - Last Post: 29 November 2010 - 03:19 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

SortedMap duplicate keys

Posted 29 November 2010 - 11:11 AM

Hello ,
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

Is This A Good Question/Topic? 0
  • +

Replies To: SortedMap duplicate keys

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,174
  • Joined: 27-December 08

Re: SortedMap duplicate keys

Posted 29 November 2010 - 11:29 AM

cfoley has an excellent Multimap snippet you could adapt.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,924
  • Joined: 20-September 08

Re: SortedMap duplicate keys

Posted 29 November 2010 - 11:37 AM

No. There's no such thing as a duplicate-key Map. What you can do is store multiple values using some kind of collection
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: SortedMap duplicate keys

Posted 29 November 2010 - 12:26 PM

The MultiMap snippet is much closer to what i am trying to achieve but is there a way that it would not give me a list of values with the same key and rather than unique values?

Let me rephrase the point why i need to store duplicate keys.
Basically what i need is a data structure that i can constantly update. Meaning: i have integers as keys and then arrays as values. I want only 200 best keys to be kept in my data structre. When i add a new entry, i put it according to its key. If data structre is full (200 entries) then the worst one is pushed off. Therefore there is a possility of same keys.

This post has been edited by darek9576: 29 November 2010 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,174
  • Joined: 27-December 08

Re: SortedMap duplicate keys

Posted 29 November 2010 - 12:31 PM

Perhaps you'll want to design your own PriorityQueueMap class. :)
Was This Post Helpful? 0
  • +
  • -

#6 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: SortedMap duplicate keys

Posted 29 November 2010 - 12:56 PM

Hey ,

My last attempt to solve the issue. I have found a bit of code on the forum:


public class TComp implements Comparator{  
   public int compare(Object o1, Object o2) {  
      return compare((Map.Entry)o1, (Map.Entry)o2);  
   }  
   public int compare(Map.Entry e1, Map.Entry e2) {  
      int cf = ((Comparable)e1.getValue)().compareTo(e2.getValue());  
      if (cf == 0) {  
         cf = ((Comparable)e1.getKey()).compareTo(e2.getKey());  
      }  
      return cf;  
   }  
}  






but it wont compile as there is an error on the line:

int cf = ((Comparable)e1.getValue)().compareTo(e2.getValue());  



saying that cant convert from Comparable to int.

(If that fails i give up : ( )
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,924
  • Joined: 20-September 08

Re: SortedMap duplicate keys

Posted 29 November 2010 - 01:07 PM

You could use a sorted (on name) list of name-value pairs
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,174
  • Joined: 27-December 08

Re: SortedMap duplicate keys

Posted 29 November 2010 - 03:19 PM

That code is deprecated anyways b/c it isn't using Generics. Also, you should define your Entry class so that value is Comparable: Map.Entry<K, V extends Comparable<? super V>>, and have your Comparator simply invoke the compareTo() method when comparing Values in the Entry.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1