I\'ve seen other questions about getting objects from Set
\'s based on index value and I understand why that is not possible. But I haven\'t been able to find a
The reason why there is no get is simple:
If you need to get the object X from the set is because you need something from X and you dont have the object.
If you do not have the object then you need some means (key) to locate it. ..its name, a number what ever. Thats what maps are for right.
map.get( "key" ) -> X!
Sets do not have keys, you need yo traverse them to get the objects.
So, why not add a handy get( X ) -> X
That makes no sense right, because you have X already, purist will say.
But now look at it as non purist, and see if you really want this:
Say I make object Y, wich matches the equals of X, so that set.get(Y)->X. Volia, then I can access the data of X that I didn have. Say for example X has a method called get flag() and I want the result of that.
Now look at this code.
Y
X = map.get( Y );
So Y.equals( x ) true!
but..
Y.flag() == X.flag() = false. ( Were not they equals ?)
So, you see, if set allowed you to get the objects like that It surely is to break the basic semantic of the equals. Later you are going to live with little clones of X all claming that they are the same when they are not.
You need a map, to store stuff and use a key to retrieve it.
Your last sentence is the answer.
get(Object o)
would run through the HashSet
looking for another object being equal to o
(using equals(o)
method). So it is indeed the same as contains(o)
, only not returning the same result.
A common use case of a get
method on Set
might be to implement an intern set. If that's what you're trying to achieve, consider using the Interner interface and Interners factory from Google Guava.
I've got the same problem as the thread author and I've got a real reason why a Set should have a get method: I overwrote equals of e.g. X, the content of the set Set and so the contained object is not necessarily the same as the checked one. In my scenario I'll remove semantic doubles in an other collection and enrich the "original" with some relations of the "double" so I need the "original" to be able to drop the double.
if you only want know what are in the Hashset, you can use .toString();
method to display all Hashset Contents separated by comma.
A Set
is a Collection
of objects which treats a.equals(b) == true
as duplicates, so it doesn't make sense to try to get the same object you already have.
If you are trying to get(Object)
from a collection, a Map
is likely to be more appropriate.
What you should write is
Map<String, String> map = new LinkedHashMap<>();
map.put("1", "Number 1");
map.put("2", null);
String description = map.get("1");
if an object is not in the set (based on equals), add it, if it is in the set (based on equals) give me the set's instance of that object
In the unlikely event you need this you can use a Map
.
Map<Bar, Bar> map = // LinkedHashMap or ConcurrentHashMap
Bar bar1 = new Bar(1);
map.put(bar1, bar1);
Bar bar1a = map.get(new Bar(1));