If I have some instance variable, let's say
private Map<String,MyClass> myMap;
and if I want to create a method to return myMap values (or keys), do I have to guarantee that method return a Set (not to have duplicates) or is that already guaranteed by instance variable definition? Is there any case where duplicates can occur if I decide to return List type from method (I believe List is much more efficient than set)?
Highlights
public List<MyClass> myValues(){
return
myMap.values().stream().map(MyClass :: clone).collect(Collectors.toList());
}
versus
public Set<MyClass> myValues(){
return
myMap.values().stream().map(MyClass :: clone).collect(Collectors.toSet());
}
In terms of:
- Duplicates existence;
- Performance.
Aucun commentaire:
Enregistrer un commentaire