• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 954
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks buddy...
Jack
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic