• 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 retrieve an object from a TreeSet or HashSet?

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of months ago, in this thread:

https://coderanch.com/t/617504/java/java/Working-HashMaps-Compressed-Matrix

the best working solution given there was to use a HashMap<Point, Integer>.

Now, there's nothing wrong with that, of course, but I kept the feeling that this was a bit of a waste .
Surely, if we have a class like this, for example:


then all we need is a HashSet/TreeSet<PietsPoint>(), saving on the Integer.

But I ran into a problem that I could only partially solve. The question is: given a Point p,
how to retrieve the associated PietsPoint? The idea is that some x, y values are given, and
I have to update the count. But, as far as I know, there is no 'get()' method defined for a
set, other than using some iterator. But the use of an iterator would be unacceptably slow.
I need some direct access.

For a TreeSet, I got it sort-of working, by (when having an x,y pair given) using:



And that worked, but it turned out to be about 60% slower than the HashMap-solution.
But for a HashSet, I was unable to find a solution, so I do not know if a HashSet would
turn out to be faster and/or more economical on memory useage.

I am currently working on a problem, where I have to use a similar structure. Therefore my question:

Anyone an idea on using a HashSet?

Greetz,
Piet


 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:The question is: given a Point p, how to retrieve the associated PietsPoint?



If we ignore the rest of your post, that suggests a Map<Point, PietsPoint>. I don't know whether that's practical given the rest of what you have, though.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Paul,

thanks for the reply.

Indeed I'm using a HashMap<Point, Statistics> at the moment, and it works as it should.

A HashSet and a TreeSet have methods that can determine very rapidly if a given element
is present within the set or not. I find it strange then, that there is no method that actually
retrieves this element. If there were, we would not need to use a Map.

But anyway, a Map is a fine structure to work with.

Greetz,
Piet
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to get an object from a set. Just use the contains method on an object you already have and if it returns true then that is the object you are looking for.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the crux is: I don't know the object, only the key of the object.

As stated, a Map is the obvious way to go, but I was hoping to be able to
save on one object.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Well, the crux is: I don't know the object, only the key of the object.

As stated, a Map is the obvious way to go, but I was hoping to be able to
save on one object.


The key should be the object for hashset purposes. If you find that you need to separate the key from the rest of the object's properties then yes, the hashset shouldn't be used to begin with and a map is the natural choice.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:A HashSet and a TreeSet have methods that can determine very rapidly if a given element
is present within the set or not. I find it strange then, that there is no method that actually
retrieves this element.



If the Set interface had a get(Object o) method, it would simply return an object from the Set which was equal to the object passed to the get() method. The hidden assumption here is that if two objects are equal then they can be used interchangeably, and that makes the get() method unnecessary. As E Armitage just suggested in the above post. So if you have two objects which are equal but can't be used in place of each other, there's possibly a problem with your design.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Well, the crux is: I don't know the object, only the key of the object.

As stated, a Map is the obvious way to go, but I was hoping to be able to
save on one object.



The fact that you're referring to a "key" suggests to me that a map is the correct abstraction, and not a set. Why try and avoid using a map when a map represents exactly the functionality you're looking for - retrieving an object based on a key?

And remember that a HashSet uses a HashMap internally for the implementation, so I'm not sure you'd be saving anything if you did get it to work.
 
reply
    Bookmark Topic Watch Topic
  • New Topic