在HashMap中使用keySet()方法
我有一个方法可以遍历面板中的可能状态并将它们存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
用途:
始终尝试为这些方法返回的集合指定接口类型。这样,无论这些方法返回的 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:
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
这就是我喜欢迭代地图的方式。如果您特别想要 keySet(),则答案位于本页的其他位置。
This is how I like to iterate through Maps. If you specifically want just the keySet(), that answer is elsewhere on this page.
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.
只是
Just
除非您使用的是较旧的 JDK,否则我认为在使用 Collections 类时使用泛型会更干净一些。
那么
,如果您只是迭代它们,那么您可以使用您想要的任何类型的循环。但是如果您要根据此 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
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()
所保证的一切都是实现接口Set
的。这可能是一些未记录的类,例如SecretHashSetKeys$foo
,因此只需对接口Set
进行编程即可。我试图获取
TreeSet
的视图时遇到了这个问题,经过仔细检查,返回类型最终为TreeSet$3
。All that's guaranteed from
keySet()
is something that implements the interfaceSet
. And that could possibly be some undocumented class likeSecretHashSetKeys$foo
, so just program to the interfaceSet
.I ran into this trying to get a view on a
TreeSet
, the return type ended up beingTreeSet$3
on close examination.它将映射中的键值放入集合中。
It puts the key values in the map into the set.
从 Javadocs 来看,
HashMap
有几种可用于操作 hasmap 和从 hasmap 中提取数据的方法。公共集keySet()
返回此映射中包含的键的集合视图。该集合由地图支持,因此对地图的更改会反映在集合中,反之亦然。如果在对集合进行迭代时修改映射(除非通过迭代器自己的删除操作),则迭代的结果是不确定的。该集合支持元素删除,即通过 Iterator.remove、Set.remove、removeAll、retainAll 和clear 操作从映射中删除相应的映射。它不支持add或addAll操作。
指定者:
接口Map中的keySet
覆盖:
AbstractMap 类中的 keySet
返回:
此映射中包含的键的集合视图
,因此如果您有任何数据类型的映射 myMap ,例如定义为
map
的映射,如果您按如下方式迭代它:例如,如果映射被定义为
Map
那么对于上面的例子我们将有:
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:e.g if the map was defined as
Map<Integer,Boolean>
Then for the above example we will have: