• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to design a container which is a map of unrepeated values?  RSS feed

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say I had 4 agents, which possess some values.
So In the first step, I consider using a Map
Then for all agents, all of these values must be unique
So
Agent 1
1,3,5
Agent 2
2,4,7
Agent 3
8,9,10
Agent 4
6,11,12
etc

But I just can't do the following

I wonder what is the correct way of doing it?
Thanks a lot
Jack
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think you can do this way. Probably you need store them in some collection and each time you are adding any element into it,
you need to compare that with all the values present the map.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:I dont think you can do this way. Probably you need store them in some collection and each time you are adding any element into it,
you need to compare that with all the values present the map.


So then I need to use a Map of ArrayList instead?
Thanks
Jack
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I think Map<Integer, Set> is a good starting point. I don't understand why you used two different types of Set in your original attempt, though. What's wrong with

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks buddy...
Jack
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:No, I think Map<Integer, Set> is a good starting point. I don't understand why you used two different types of Set in your original attempt, though. What's wrong with


I'd go with the following myself:

This allows any SortedSet implementation to be used, not just TreeSet. In the end the implementation hardly ever matters.

Of course with Java 7 and beyond that can be shortened:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!