2017-06-01 1 views
1

Ich versuche, eine Map, die eine Zeichenkette als Schlüssel und eine Menge als Wert enthält, wieder zu verwenden. Wie soll ich das machen? Das ist, was ich habe.Wie man eine HashMap deklariert und initialisiert, die ein HashSet enthält

/** 
* A Class 
*/ 
public class Catalog 
{ 
    private HashMap <String , Set<String> varieties> aCatalog; 

    /** 
    * Constructor for objects of class Catalog 
    */ 
    public Catalog() 
    { 
     // initialise instance variables 
     varieties = new HashSet<>(); 
     aCatalog = new HashMap<String,varieties>(); 
    } 
} 

Dies funktioniert nicht, ich habe einige ähnliche Fragen, aber ich konnte keine Lösung finden.

Danke für die Hilfe Jungs!

+0

* Dies funktioniert nicht *: Können Sie beschreiben, was es bedeutet? – Jens

+1

'aCatalog = new HashMap >();' – Eran

+0

Es wird nicht kompiliert, bei der Deklaration Privat HashMap Sorten> aCatalog; – curiousBadger

Antwort

3

Ihre Karte initialisieren Sie müssen nur die generischen Typen definieren:

aCatalog = new HashMap<String, Set<String>>(); 

Seit 1.7 Java können Sie den Diamant-Operator verwenden:

aCatalog = new HashMap<>(); 

Um einen Wert in Ihre Karte setzen Sie brauchen einen Satz zu initialisieren:

aCatalog.put("theKey", varieties); 
+0

Ich kann nicht kompilieren als die Deklaration, 'private HashMap Varietäten> aCatalog;' es heißt, es gibt einen Fehler zwischen den Set * HIER * Sorten.die IDE fragt nach einem ">" – curiousBadger

+0

entfernen "Sorten" aus der generischen Erklärung –

+0

Das dann kompiliert, aber würde ich dann 'private HashSet Sorten hinzufügen müssen;' als zweite Erklärung – curiousBadger

0

können Sie tun:

// declare: 
    Map<String, Set<String>> aCatalog; 
    // init java 7 
    aCatalog = new HashMap<>(); 
    // insert: 
    aCatalog.put("AA", new HashSet<>()); 

oder in älteren Versionen:

// declare: 
    Map<String, Set<String>> aCatalog; 
    // init java 
    aCatalog = new HashMap<String, Set<String>>(); 
    // insert: 
    aCatalog.put("AA", new HashSet<String>()); 
0

Do it yourself nicht tun, gibt es schöne Multimap by Google Guava das, dass Sie ist tun wollen. So zu initialisieren Sie Builder wie diese verwenden:

SetMultimap<String, String> yourMap = MultimapBuilder.hashKeys() 
     .linkedHashSetValues().build(); 

Hoffe, es hilft!

+0

Ich würde gerne, leider sind wir nicht so eine Zauberei für meine Prüfung erlaubt! – curiousBadger

0

Das ist nicht richtig Erklärung:

private HashMap <String , Set<String> varieties> aCatalog; 
//---------------------------------------------^ 

Es sollte:

private HashMap <String , Set<String>> aCatalog, varieties; 
//-----------------------------------^---------^ 

Oder erklären sie separat

private HashMap<String, Set<String>> aCatalog; 
private HashMap<String, Set<String>> varieties; 

Ihrer Karte inisliaze Sie verwenden müssen:

aCatalog = new HashMap<>(); 

Aber ich denke, dass Sie so etwas wie dies wollen:

private HashMap<String, Set<String>> aCatalog;//declare the HashMap 
private Set<String> varieties;//declare the HashSet 
//-------^^^ 

public Catalog() { 
    // initialise instance variables 
    varieties = new HashSet<>();//insialize the HashSet 
    aCatalog = new HashMap<>();//Inisialize the HashMap 

    //put some values to your Map 
    aCatalog.put("key", varieties); 
    //----------------------^^ 
} 
0

Okey, Sie haben ein Problem mit der Art Ihrer Variablen, und Sie sind verwirrend, wie Sie Ihr Objekt zu initialisieren, ist hier zwei Wege zu tun Sie es:

import java.util.HashMap; 
import java.util.HashSet; 
import java.util.Map; 
import java.util.Set; 

public class Catalog { 

    //is better to use interface for the type of the variable 
    private Map<String, Set<String>> aCatalog; 

    // here in your constructor, you just initialize an empty Hash, the most 
    // external one 
    public Catalog() { 
     aCatalog = new HashMap<String, Set<String>>(); 
     // aCatalog = new HashMap<>(); //this is also valid since 1.7 
    } 

    //you can also create another constructor, and create the map outside and give it as parameter 
    public Catalog(Map<String, Set<String>> catalog) { 
     this.aCatalog = catalog; 
    } 



    public Map<String, Set<String>> getaCatalog() { 
     return aCatalog; 
    } 


    public static void main(String[] args) { 
     //here is an example of creating the map inside the Catalog class 
     Catalog catalog1 = new Catalog(); 

     String key1 = "k1"; 
     Set<String> valueSet1 = new HashSet<String>(); 
     valueSet1.add("something 1"); 
     valueSet1.add("something 2"); 
     valueSet1.add("something 3"); 
     String key2 = "k2"; 
     Set<String> valueSet2 = new HashSet<String>(); 
     valueSet2.add("other thing1"); 
     valueSet2.add("other thing2"); 
     valueSet2.add("other thing3"); 

     catalog1.getaCatalog().put(key1, valueSet1); 
     catalog1.getaCatalog().put(key2, valueSet2); 

     //and here is an example of givin as constructor parameter 

     Map<String, Set<String>> anotherCatalog = new HashMap<>(); 
     anotherCatalog.put(key1, valueSet2); 
     anotherCatalog.put(key2, valueSet1); 

     Catalog catalog2 = new Catalog(anotherCatalog); 


    } 
} 
+0

die Deklaration von 'aCatalog' wird nicht wegen' Varietés' im generischen Teil kompiliert –

+0

@Stefan Warminski Sie sind so richtig vielen Dank, ich habe die Antwort aktualisiert, hoffe, dass das jetzt korrekt ist –

+0

@Damian Lattenero Wäre ich nicht nötig Benennen Sie das Set aber? as'varieties = new HashSet (); 'auch du verpasst ein"> "' aCatalog = new HashMap >(); ' – curiousBadger

Verwandte Themen