如何申报&初始化包含HashSet的HashMap

时间:2017-06-01 13:03:40

标签: java hashmap initialization declaration hashset

我正在尝试读取包含字符串作为键的Map,并将set设置为值,我该怎么做呢?这就是我所拥有的。

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

这不起作用,我看了一些类似的问题,但我找不到解决方案。

感谢帮助人员!

4 个答案:

答案 0 :(得分:3)

要初始化地图,您只需要定义泛型类型:

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

从java 1.7开始,您可以使用菱形运算符:

aCatalog = new HashMap<>();

要将值放入地图,您需要初始化一个集合:

aCatalog.put("theKey", varieties);

答案 1 :(得分:0)

你可以这样做:

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

答案 2 :(得分:0)

不要自己做,有美丽的Multimap by Google Guava就是你想要的。 要初始化,您可以使用以下构建器:

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

希望它有所帮助!

答案 3 :(得分:0)

Okey,你的变量类型有问题,而且你对如何初始化对象感到困惑,这是两种方法:

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


    }
}