如何将set集合中的数据转化成map的key

我是这样做的,不要用循环然后put这样很麻烦。
public static void main(String[] args) {
    Map<String,Integer> hashMap = new HashMap<>();
    Set<String> set = new HashSet<>();
    set.add("key1");
    set.add("key2");
    set.add("key3");
    set.add("key4");
    hashMap.keySet().addAll(set);//添加不进去
    System.out.println(hashMap);
}

然而直接翻车
Exception in thread "main" java.lang.UnsupportedOperationException

循环就是最方便的方法,如果你不需要value的话,那么就不用把set转到map

楼上正解,楼上正解!

private static void testMap2Set() {

    Map<String, String> map = new HashMap<String, String>();    
    map.put("A", "ABC");    
    map.put("K", "KK");    
    map.put("L", "LV");    

    // 将Map 的键转化为Set      
    Set<String> mapKeySet = map.keySet();    
    System.out.println("mapKeySet:"+mapKeySet);  

    // 将Map 的值转化为Set      
    Set<String> mapValuesSet = new HashSet<String>(map.values());    
    System.out.println("mapValuesSet:"+mapValuesSet);  
}