簡體   English   中英

如何聲明和初始化包含HashSet的HashMap

[英]How to declare & Initialize a HashMap that Contains A HashSet

我試圖讀取一個包含字符串作為鍵的映射,並將一個集合作為值,我該怎么做呢? 這就是我所擁有的。

/**
 * 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>();
   }
}

這不起作用,我看了一些類似的問題,但我找不到解決方案。

謝謝你的幫助!

要初始化地圖,您只需要定義泛型類型:

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

從java 1.7開始,您可以使用菱形運算符:

aCatalog = new HashMap<>();

要將值放入地圖,您需要初始化一組:

aCatalog.put("theKey", varieties);

你可以做:

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

或舊版本:

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

不要自己做, 谷歌番石榴有美麗的Multimap,這是你想要的。 要初始化,您可以使用以下構建器:

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

希望能幫助到你!

好吧,你的變量類型有問題,而且你對如何初始化對象感到困惑,這有兩種方法:

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);


    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM