dimanche 10 juillet 2016

Map vs List specifics on method return

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:

  1. Duplicates existence;
  2. Performance.

Aucun commentaire:

Enregistrer un commentaire