Fetch value from a set object in java -
i'm iterating set object find particular value. there short way fetch instead of iterating it? here code
for(tree t : assignedtrees) { println t.treename; }
the above code return expected value.
assignedtrees set object
set<tree> assignedtrees = new hashset<tree>() println assignedtrees return [tree{id=null, treename=mango}]
can fetch treename instead of iterating?
you can fetch object set calling myset.get(object)
. however, in case wish fetch object based on 1 of attributes. best way map - e.g.
map<string, tree> trees = new hashmap<>(); trees.put(treeobject.treename, treeobject); tree mytree = trees.get("mytreename");
note if you're putting own objects sets or maps, must override equals , hashcode methods, or strange things happen.
Comments
Post a Comment