在HashMap中使用keySet()方法

发布于 2024-08-14 06:19:37 字数 681 浏览 7 评论 0原文

我有一个方法可以遍历面板中的可能状态并将它们存储在 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()?

   }
  }

}

我对键组感兴趣。我应该怎么做才能将它们全部打印出来?

HashSet t = map.keySet() 被编译器拒绝

LinkedHashSet t = map.keySet()

I have a method that goes through the possible states in a board and stores them in a 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

LinkedHashSet t = map.keySet()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

再浓的妆也掩不了殇 2024-08-21 06:19:37

用途:

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

始终尝试为这些方法返回的集合指定接口类型。这样,无论这些方法返回的 Set 的实际实现类如何(在您的情况下为 map.keySet()),您都会没问题。这样,如果下一个版本 jdk 人员对返回的 Set 使用不同的实现,您的代码仍然可以工作。

map.keySet() 返回地图键上的视图。对此视图进行更改会导致底层地图发生更改,尽管这些更改是有限的。请参阅 Map 的 javadoc:

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

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. This way if the next release the jdk guys use a different implementation for the returned Set your code will still work.

map.keySet() returns a View on the Keys of the map. Making changes to this view results in changing the underlying map though those changes are limited. See the javadoc for Map:

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

甜味超标? 2024-08-21 06:19:37
Map<String, String> someStrings = new HashMap<String, String>();
for(Map.Entry<String, String> entry : someStrings.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
}

这就是我喜欢迭代地图的方式。如果您特别想要 keySet(),则答案位于本页的其他位置。

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.

人生百味 2024-08-21 06:19:37
for ( String key : map.keySet() ) { 
 System.out.println( key );
}
for ( String key : map.keySet() ) { 
 System.out.println( key );
}
避讳 2024-08-21 06:19:37

Set t = map.ketSet()

API 没有指定返回什么类型的 Set。

您应该尝试将变量声明为接口而不是特定的实现。

Set t = map.ketSet()

The API does not specify what type of Set is returned.

You should try to declare variables as the interface rather than a particular implementation.

美男兮 2024-08-21 06:19:37

只是

Set t = map.keySet();

Just

Set t = map.keySet();
心如狂蝶 2024-08-21 06:19:37

除非您使用的是较旧的 JDK,否则我认为在使用 Collections 类时使用泛型会更干净一些。

那么

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

,如果您只是迭代它们,那么您可以使用您想要的任何类型的循环。但是如果您要根据此 keySet 修改映射,则必须使用 keySet 的迭代器。

Unless you're using an older JDK, I think its a little cleaner to use generics when using the Collections classes.

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.

白首有我共你 2024-08-21 06:19:37

keySet() 所保证的一切都是实现接口 Set 的。这可能是一些未记录的类,例如 SecretHashSetKeys$foo,因此只需对接口 Set 进行编程即可。

我试图获取 TreeSet 的视图时遇到了这个问题,经过仔细检查,返回类型最终为 TreeSet$3

All that's guaranteed from keySet() is something that implements the interface Set. And that could possibly be some undocumented class like SecretHashSetKeys$foo, so just program to the interface Set.

I ran into this trying to get a view on a TreeSet, the return type ended up being TreeSet$3 on close examination.

ˇ宁静的妩媚 2024-08-21 06:19:37
    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);

它将映射中的键值放入集合中。

    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.

东北女汉子 2024-08-21 06:19:37

从 Javadocs 来看,HashMap 有几种可用于操作 hasmap 和从 hasmap 中提取数据的方法。

公共集keySet()
返回此映射中包含的键的集合视图。该集合由地图支持,因此对地图的更改会反映在集合中,反之亦然。如果在对集合进行迭代时修改映射(除非通过迭代器自己的删除操作),则迭代的结果是不确定的。该集合支持元素删除,即通过 Iterator.remove、Set.remove、removeAll、retainAll 和clear 操作从映射中删除相应的映射。它不支持add或addAll操作。
指定者:
接口Map中的keySet
覆盖:
AbstractMap 类中的 keySet
返回:
此映射中包含的键的集合视图

,因此如果您有任何数据类型的映射 myMap ,例如定义为 map 的映射,如果您按如下方式迭代它:

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

例如,如果映射被定义为 Map

那么对于上面的例子我们将有:

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

From Javadocs HashMap has several methods that can be used to manipulate and extract data from a hasmap.

public Set<K> keySet()
Returns a Set view of the keys contained in this map. 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. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.
Specified by:
keySet in interface Map
Overrides:
keySet in class AbstractMap
Returns:
a set view of the keys contained in this map

so if you have a map myMap of any datatype , such that the map defined as map<T> , if you iterate it as follows:

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

e.g if the map was defined as 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文