简体   繁体   English

在HashMap中使用keySet()方法

[英]Using the keySet() method in HashMap

I have a method that goes through the possible states in a board and stores them in a HashMap 我有一个方法遍历一个板中的可能状态并将它们存储在HashMap中

void up(String str){
  int a = str.indexOf("0");
  if(a>2){
   String s = str.substring(0,a-3)+"0"+str.substring(a-2,a)+str.charAt(a-3)+str.substring(a+1);
   add(s,map.get(str)+1);
   if(s.equals("123456780")) {
    System.out.println("The solution is on the level  "+map.get(s)+" of the tree");

        //If I get here, I need to know the keys on the map
       // How can I store them and Iterate through them using 
      // map.keySet()?

   }
  }

} }

I'm interested in the group of keys. 我对这组钥匙很感兴趣。 What should I do to print them all? 我应该怎么做才能打印出来?

HashSet t = map.keySet() is being rejected by the compiler as well as HashSet t = map.keySet()被编译器拒绝了

LinkedHashSet t = map.keySet()

Use: 使用:

Set<MyGenericType> keySet = map.keySet();

Always try to specify the Interface type for collections returned by these methods. 始终尝试为这些方法返回的集合指定接口类型。 This way regardless of the actual implementation class of the Set returned by these methods (in your case map.keySet()) you would be ok. 这种方式无论这些方法返回的Set的实际实现类(在你的情况下是map.keySet())你都可以。 This way if the next release the jdk guys use a different implementation for the returned Set your code will still work. 这样,如果下一个版本的jdk家伙为返回的Set使用不同的实现,你的代码仍然可以工作。

map.keySet() returns a View on the Keys of the map. map.keySet()返回地图键上的视图。 Making changes to this view results in changing the underlying map though those changes are limited. 虽然这些更改有限,但对此视图进行更改会导致更改基础地图。 See the javadoc for Map: 查看地图的javadoc:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#keySet%28%29 http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#keySet%28%29

Map<String, String> someStrings = new HashMap<String, String>();
for(Map.Entry<String, String> entry : someStrings.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
}

This is how I like to iterate through Maps. 这就是我喜欢迭代地图的方式。 If you specifically want just the keySet(), that answer is elsewhere on this page. 如果您只想要keySet(),那么该答案就在本页的其他地方。

for ( String key : map.keySet() ) { 
 System.out.println( key );
}

Set t = map.ketSet() 设t = map.ketSet()

The API does not specify what type of Set is returned. API未指定返回的Set类型。

You should try to declare variables as the interface rather than a particular implementation. 您应该尝试将变量声明为接口而不是特定的实现。

只是

Set t = map.keySet();

Unless you're using an older JDK, I think its a little cleaner to use generics when using the Collections classes. 除非您使用较旧的JDK,否则我认为在使用Collections类时使用泛型更加清晰。

So thats 所以那是

Set<MyType> s = map.keySet();

And then if you just iterate through them, then you can use any kind of loop you'd like. 然后,如果你只是遍历它们,那么你可以使用你想要的任何类型的循环。 But if you're going to be modifying the map based on this keySet, you you have to use the keySet's iterator. 但是如果您要基于此keySet修改地图,则必须使用keySet的迭代器。

All that's guaranteed from keySet() is something that implements the interface Set . 所有来自keySet()的保证都是实现接口Set东西。 And that could possibly be some undocumented class like SecretHashSetKeys$foo , so just program to the interface Set . 这可能是一些未记录的类,如SecretHashSetKeys$foo ,所以只需编程到接口Set

I ran into this trying to get a view on a TreeSet , the return type ended up being TreeSet$3 on close examination. 我试图在TreeSet上获取一个视图,返回类型在仔细检查时最终成为TreeSet$3

    Map<String, Object> map = new HashMap<>();
    map.put("name","jaemin");
    map.put("gender", "male");
    map.put("age", 30);
    Set<String> set = map.keySet();
    System.out.println("this is map : " + map);
    System.out.println("this is set : " + set);

It puts the key values in the map into the set. 它将映射中的键值放入集合中。

From Javadocs HashMap has several methods that can be used to manipulate and extract data from a hasmap. 从Javadocs开始, HashMap有几种方法可用于操作和从hasmap中提取数据。

public Set<K> keySet() Returns a Set view of the keys contained in this map. public Set<K> keySet()返回此映射中包含的键的Set视图。 The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. 该集由地图支持,因此对地图的更改将反映在集中,反之亦然。 If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. 如果在对集合进行迭代时修改了映射(除了通过迭代器自己的remove操作),迭代的结果是未定义的。 The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. 该集支持元素删除,它通过Iterator.remove,Set.remove,removeAll,retainAll和clear操作从地图中删除相应的映射。 It does not support the add or addAll operations. 它不支持add或addAll操作。 Specified by: keySet in interface Map Overrides: keySet in class AbstractMap Returns: a set view of the keys contained in this map 指定者:接口Map中的keySet覆盖:类AbstractMap中的keySet返回:此映射中包含的键的set视图

so if you have a map myMap of any datatype , such that the map defined as map<T> , if you iterate it as follows: 所以,如果你有任何数据类型的地图myMap,这样地图定义为map<T> ,如果你按如下方式迭代它:

for (T key : myMap.keySet() ) { 
     System.out.println(key); // which represent the value of datatype T
}

eg if the map was defined as Map<Integer,Boolean> 例如,如果地图定义为Map<Integer,Boolean>

Then for the above example we will have: 然后,对于上面的例子,我们将:

for (Integer key : myMap.keySet()){
  System.out.println(key) // the key printed out will be of type Integer
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM