Java 如何从HashMap获取值和键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16246821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:59:47  来源:igfitidea点击:

How to get values and keys from HashMap?

javahashmap

提问by

I'm writing a simple edit text in Java. When the user opens it, a file will be opened in JTabbedPane. I did the following to save the files opened:

我正在用 Java 编写一个简单的编辑文本。当用户打开它时,文件将以JTabbedPane. 我做了以下操作来保存打开的文件:

HashMap<String, Tab> hash = new HashMap<String, Tab>();

HashMap<String, Tab> hash = new HashMap<String, Tab>();

Where Tabwill receive the values, such as: File file, JTextArea container, JTabbedPane tab.

哪里Tab会收到值,例如:File file, JTextArea container, JTabbedPane tab

I have a class called Tab:

我有一个班级叫Tab

public Tab(File file, JTextArea container, JTabbedPane tab)
{
    this.file = file;
    this.container = container;
    this.tab = tab;
    tab.add(file.getName(), container);
    readFile();
}

Now, in this SaveFileclass, I need get the Tabvalues stored in the HashMap. How can I do that?

现在,在这个SaveFile类中,我需要获取Tab存储在HashMap. 我怎样才能做到这一点?

采纳答案by Bohemian

To get all the values from a map:

要从地图中获取所有值:

for (Tab tab : hash.values()) {
    // do something with tab
}

To get all the entries from a map:

要从地图中获取所有条目:

for ( Map.Entry<String, Tab> entry : hash.entrySet()) {
    String key = entry.getKey();
    Tab tab = entry.getValue();
    // do something with key and/or tab
}

Java 8 update:

Java 8 更新:

To process all values:

处理所有值:

hash.values().forEach(tab -> /* do something with tab */);

To process all entries:

要处理所有条目:

hash.forEach((key, tab) -> /* do something with key and tab */);

回答by vishakvkt

Use the 'string' key of the hashmap, to access its value which is your tab class.

使用哈希映射的“字符串”键,访问它的值,即您的选项卡类。

Tab mytab = hash.get("your_string_key_used_to_insert");

回答by user2245180

You give 1 Dollar, it gives you a cheese burger. You give the String and it gives you the Tab. Use the GET method of HashMap to get what you want.

你给 1 美元,它会给你一个芝士汉堡。你给字符串,它给你标签。使用HashMap的GET方法得到你想要的。

HashMap.get("String");

回答by bruhhhhh

You could use iterator to do that:

您可以使用迭代器来做到这一点:

For keys:

对于钥匙:

for (Iterator <tab> itr= hash.keySet().iterator(); itr.hasNext();) {
    // use itr.next() to get the key value
}

You can use iterator similarly with values.

您可以类似地将迭代器与值一起使用。

回答by Prabhu

Mapis internally made up of Map.Entryobjects. Each Entrycontains keyand value. To get key and value from the entry you use accessor and modifier methods.

Map内部由Map.Entry对象组成。每个Entry包含keyvalue。要从条目中获取键和值,您可以使用访问器和修饰符方法。

If you want to get valueswith given key, use get()method and to insert value, use put()method.

如果你想得到给values定的key,使用get()方法并插入值,使用put()方法。

#Define and initialize map;
Map map = new HashMap();
map.put("USA",1)
map.put("Japan",3)
map.put("China",2)
map.put("India",5)
map.put("Germany",4)

map.get("Germany") // returns 4

If you want to get the set of keys from map, you can use keySet()method

如果你想从地图中获取一组键,你可以使用keySet()方法

Set keys = map.keySet();
System.out.println("All keys are: " + keys);
// To get all key: value
for(String key: keys){
    System.out.println(key + ": " + map.get(key));
}

Generally, To get all keys and values from the map, you have to follow the sequence in the following order:

通常,要从地图中获取所有键和值,您必须按照以下顺序进行操作:

  • Convert Hashmapto MapSetto get set of entries in Mapwith entryset()method.:
    Set st = map.entrySet();
  • Get the iterator of this set:
    Iterator it = st.iterator();
  • Get Map.Entryfrom the iterator:
    Map.Entry entry = it.next();
  • use getKey()and getValue()methods of the Map.Entryto get keys and values.
  • 转换HashmapMapSetMapwithentryset()方法中获取条目集。:
    Set st = map.entrySet();
  • 获取这个集合的迭代器:
    Iterator it = st.iterator();
  • Map.Entry从迭代器中获取:
    Map.Entry entry = it.next();
  • 的使用getKey()getValue()方法Map.Entry来获取键和值。
// Now access it
Set st = (Set) map.entrySet();
Iterator it = st.iterator();
while(it.hasNext()){
    Map.Entry entry = mapIterator.next();
    System.out.print(entry.getKey() + " : " + entry.getValue());
}

In short, use iterator directly in for

总之,直接在for中使用迭代器

for(Map.Entry entry:map.entrySet()){
    System.out.print(entry.getKey() + " : " + entry.getValue());
}

回答by Hary Bakta

You have to follow the following sequence of opeartions:

您必须遵循以下操作顺序:

  • Convert Mapto MapSetwith map.entrySet();
  • Get the iterator with Mapset.iterator();
  • Get Map.Entrywith iterator.next();
  • use Entry.getKey()and Entry.getValue()
  • 转换MapMapSetmap.entrySet();
  • 获取迭代器 Mapset.iterator();
  • 获取Map.Entryiterator.next();
  • 使用Entry.getKey()Entry.getValue()
# define Map
for (Map.Entry entry: map.entrySet)
    System.out.println(entry.getKey() + entry.getValue);

回答by javabrew

With java8 streaming API:

使用 java8 流 API:

List values = map.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());

回答by Gama_aide

for (Map.Entry<String, Tab> entry : hash.entrySet()) {
    String key = entry.getKey();
    Tab tab = entry.getValue();
    // do something with key and/or tab
}

Works like a charm.

奇迹般有效。

回答by Narendra raghuwanshi

It will work with hash.get("key");Where key is your key for getting the value from Map

它将与hash.get("key"); 一起使用。其中 key 是您从 Map 获取值的关键

回答by Andriy

To get values and keysyou could just use the methods values() and keySet() of HashMap

要获取值和键,您可以使用 HashMap 的 values() 和 keySet() 方法

public static List getValues(Map map) {
    return new ArrayList(map.values());
}

public static List getKeys(Map map) {
    return new ArrayList(map.keySet());
}