2017-04-19 1 views
0

So hinzufügen Ich habe diese Datenein Verfahren Schreibe eine String-Sammlung in einer Karte

Key  Value 
---  ----- 
fruit apple 
fruit banana 
fruit grapes 

cars  honda 
cars  lexus 
cars  bmw 

schools harvard 
schools yale 
... 

Und ich würde die Daten an einen Map<String, Collections<String>> konstruieren möchten und ein Verfahren zu machen, die alle Daten hinzufügt. Bisher habe ich einen Konstruktor, der meine Karte Variable So

public Map<String, Collection<String>> keys; 

public newMap() { 
    keys = new HashMap<>(); 
} 

public void addkeys(String K, String V) { 
    Collection<String> names = new HashSet<String>(); 

    if (keys.containsKey(K) && !keys.values().contains(V)) { 
     names.add(V); 
     courses.put(K, names); 

    } else if (!keys.containsKey(K) && !keys.values().contains(V)) { 
     names.add(V); 
     courses.put(student, classes); 
    } 
} 

instanziiert, wenn ich meinen Test laufen

newMap.addkeys("fruit", "apple"); 
newMap.addkeys("fruit, "banana"); 
newMap.addkeys("fruit", "grapes"); 
newMap.addkeys("cars, "honda"); 
newMap.addkeys("cars", "lexus"); 
newMap.addkeys("cars, "bmw"); 
newMap.addkeys("schools", "harvard"); 
newMap.addkeys("schools, "yale"); 

es

fruit = [apple, banana, grapes] 
cars = [honda, lexus, bmw] 
schools = [hardvard, yale] 

zurückkehren sollte, sondern ich

fruit = [grapes] 
cars = [bmw] 
schools = [yale] 

scheint es so Ich füge nur die letzte Instanz hinzu, denn wenn ich Collection<String> names = new HashSet<String>() rufe, instanziiere ich names, aber wenn ich diese Instanziierung zum Beginn der Klasse mache, fügt sie einfach alles hinzu. So gibt es fruit = [apple, banana, grapes, honda, lexus, bmw, hardvard, yale] zurück.

Antwort

1

Pre-Java 8:

Verwenden Sie die Tatsache, dass Map#get kehrt null, wenn es nicht eine Abbildung ist Um festzustellen, ob Sie eine neue Sammlung in der Karte put benötigen, fügen Sie den Wert zur Sammlung hinzu.

public void addkeys(String K, String V) { 
    Collection<String> values = keys.get(K); 
    if (values == null) { 
     values = new HashSet<>(); 
     keys.put(K, values); 
    } 
    values.add(V); 
} 

Java 8+:

Verwenden Map#computeIfAbsent eine neue Kollektion auf der Karte hinzufügen, wenn es nicht eine Abbildung ist, und dann den Wert der zurückgegebenen Sammlung.

public void addkeys(String K, String V) { 
    keys.computeIfAbsent(K, k -> new HashSet<>()).add(V); 
} 
0

Sie können Ihre Funktion wie folgt umschreiben:

public void addkeys(String K, String V) { 
    if (keys.containsKey(K)) { 
     keys.get(K).add(V); // add to existing hashset 
    } else { 
     Collection<String> names = new HashSet<String>(); 
     names.add(V); 
     keys.put(K, names); // add new hashset for key 
    } 
} 
0

Versuchen Sie, diese

public Map<String, Set<String>> myMap; 

public newMap() { 
    myMap = new HashMap<>(); 
} 

public void putInMap(String key, String val) { 

    if (myMap.containsKey(key)) { 
     myMap.get(K).add(V); 
    } else { 
     Set<String> values = new HashSet<String>(); 
     values.add(val); 
     myMap.put(key, values); 
    } 
} 
Verwandte Themen