Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为hashmap中的每一个应用程序创建一个?_Java - Fatal编程技术网

Java 如何为hashmap中的每一个应用程序创建一个?

Java 如何为hashmap中的每一个应用程序创建一个?,java,Java,我有这个领域: HashMap<String, HashMap> selects = new HashMap<String, HashMap>(); 使用entrySet /** *Output: D: 99.22 A: 3434.34 C: 1378.0 B: 123.22 E: -19.08 B's new balance: 1123.22 */ import java.util.HashMap; import java.util.Map; import

我有这个领域:

HashMap<String, HashMap> selects = new HashMap<String, HashMap>();

使用
entrySet

/**
 *Output: 
D: 99.22
A: 3434.34
C: 1378.0
B: 123.22
E: -19.08

B's new balance: 1123.22
 */

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MainClass {
  public static void main(String args[]) {

    HashMap<String, Double> hm = new HashMap<String, Double>();

    hm.put("A", new Double(3434.34));
    hm.put("B", new Double(123.22));
    hm.put("C", new Double(1378.00));
    hm.put("D", new Double(99.22));
    hm.put("E", new Double(-19.08));

    Set<Map.Entry<String, Double>> set = hm.entrySet();

    for (Map.Entry<String, Double> me : set) {
      System.out.print(me.getKey() + ": ");
      System.out.println(me.getValue());
    }

    System.out.println();

    double balance = hm.get("B");
    hm.put("B", balance + 1000);

    System.out.println("B's new balance: " + hm.get("B"));
  }
}
/**
*输出:
D:99.22
A:3434.34
C:1378.0
B:123.22
E:-19.08
B的新余额:1123.22
*/
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Set;
公共类主类{
公共静态void main(字符串参数[]){
HashMap hm=新的HashMap();
hm.put(“A”,新双(3434.34));
hm.put(“B”,新双(123.22));
hm.put(“C”,新双(1378.00));
hm.put(“D”,新双(99.22));
hm.put(“E”,新双(-19.08));
Set=hm.entrySet();
用于(Map.Entry me:set){
System.out.print(me.getKey()+“:”);
System.out.println(me.getValue());
}
System.out.println();
双平衡=hm.get(“B”);
hm.put(“B”,余额+1000);
System.out.println(“B的新余额:+hm.get(“B”));
}
}
请参见此处的完整示例:

Map.values()

HashMap选择=
新的HashMap();
...
for(HashMap h:selects.values())
{
ComboBox cb=新ComboBox();
对于(字符串s:h.values())
{
cb.项目。添加;
}
}

您可以使用迭代器迭代
哈希映射(以及许多其他集合),例如:

HashMap<T,U> map = new HashMap<T,U>();

...

Iterator it = map.values().iterator();

while (it.hasNext()) {
    System.out.println(it.next());
}
HashMap map=newhashmap();
...
迭代器it=map.values().Iterator();
while(it.hasNext()){
System.out.println(it.next());
}

我知道我做这件事有点晚了,但我也会分享我所做的,以防对其他人有所帮助:

HashMap<String, HashMap> selects = new HashMap<String, HashMap>();

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

    // do what you have to do here
    // In your case, another loop.
}
HashMap selects=newhashmap();
for(Map.Entry:selects.entrySet()){
String key=entry.getKey();
HashMap value=entry.getValue();
//做你在这里必须做的事
//在您的情况下,另一个循环。
}

我通常与cx42net做相同的操作,但我没有显式创建条目

HashMap<String, HashMap> selects = new HashMap<String, HashMap>();
for (String key : selects.keySet())
{
    HashMap<innerKey, String> boxHolder = selects.get(key);
    ComboBox cb = new ComboBox();
    for (InnerKey innerKey : boxHolder.keySet())
    {
        cb.items.add(boxHolder.get(innerKey));
    }
}
HashMap selects=newhashmap();
for(字符串键:selects.keySet())
{
HashMap-boxHolder=selects.get(键);
ComboBox cb=新ComboBox();
对于(InnerKey:boxHolder.keySet())
{
cb.items.add(boxHolder.get(innerKey));
}
}
这对我来说似乎是最直观的,我认为我对迭代映射的值有偏见

Lambda表达式Java 8

在Java1.8(Java8)中,通过使用聚合操作(流操作)中的forEach方法,这变得容易多了,该方法看起来类似于Iterable接口中的迭代器

只需将下面的粘贴语句复制到代码中,并将HashMap变量从hm重命名为HashMap变量,以打印出键值对

HashMap<Integer,Integer> hm = new HashMap<Integer, Integer>();
/*
 *     Logic to put the Key,Value pair in your HashMap hm
 */

// Print the key value pair in one line.
hm.forEach((k,v) -> System.out.println("key: "+k+" value:"+v));
更新


包括指向Oracle文档的文档链接。 有关Lambda的详细信息,请转到此“必须阅读”,对于拆分器,请转到此。

Streams Java 8 除了接受a的
forEach
方法之外,我们还获得了Java 8中的API

迭代条目(使用forEach和Streams):

sample.forEach((k,v) -> System.out.println(k + "=" + v)); 
sample.entrySet().stream().forEachOrdered((entry) -> {
            Object currentKey = entry.getKey();
            Object currentValue = entry.getValue();
            System.out.println(currentKey + "=" + currentValue);
        });
sample.entrySet().parallelStream().forEach((entry) -> {
            Object currentKey = entry.getKey();
            Object currentValue = entry.getValue();
            System.out.println(currentKey + "=" + currentValue);
        });
流的优点是它们可以很容易地并行化,并且在我们有多个CPU可供使用时非常有用。我们只需要使用
parallelStream()
来代替上面的
stream()
。对于并行流,使用
forEach
更有意义,因为
forEachOrdered
不会对性能产生任何影响。如果我们想迭代键,我们可以使用
sample.keySet()
sample.values()

为什么
forEach是有序的
而不是
forEach
流式的?


流也提供了
forEach
方法,但是
forEach
的行为显然是不确定的,当
forEachOrdered
为流的每个元素执行操作时,如果流具有定义的遭遇顺序,则按照流的遭遇顺序执行。因此,
forEach
不能保证订单会被保留。还要检查更多信息。

+1:这是一种比我的答案(假设我们使用的是Java 5或更高版本)更简洁的方法。在外部HashMap上使用泛型参数似乎表示Java 5或更高版本。[[但我确实想知道这是什么Java,因为内部HashMap没有泛型参数,并且在HashMap上有一个数组访问……但它只是一些用于传达问题的“伪”代码。]]你想解释一下为什么它似乎不适合你吗?您使用的是Java5还是更高版本?我们遵循您的代码示例,它似乎在组合框创建后对其没有任何作用。或者这些值是否不符合您想要的顺序?或者您是否需要键而不是组合框中的值?我想大多数人都会同意,到目前为止,这三个答案中的任何一个都可以正常工作,因此,如果您提供更多信息,我们可能会在应用答案方面遇到问题,或者在其他地方遇到其他问题,我们可以提供帮助。哦……我很傻=)。只有一个问题:如何获取HashMapString,HashMap>@simply denis?恐怕我不明白你的问题?您是在问如何获得对
HashMap
的引用吗?如果您的循环在方法中,您应该能够使用
this.selects
或干脆
selects
?否则,应该将映射作为参数传递给包含循环的方法。或者您正在询问如何获取给定键(
此->字符串
?)的特定内部
哈希映射,您将使用
选择。获取(键串)
。如果我完全误解了,请澄清。我不能把这样的设计降低到这个水平below@Yes,你可以。您可以基于
it.next()
创建一个内部迭代器。它肯定能工作,我每天都在使用:)到底什么不起作用?由于您有
HashMap
,因此需要两个循环—一个用于外部,一个用于内部HashMap。顺便说一句,您应该明确地键入第二个HashMap——不知道您在其中存储了什么,但是类似于HashMap的东西——不过,从您的示例来看,它是
HashMap<Integer,Integer> hm = new HashMap<Integer, Integer>();
/*
 *     Logic to put the Key,Value pair in your HashMap hm
 */

// Print the key value pair in one line.
hm.forEach((k,v) -> System.out.println("key: "+k+" value:"+v));
    HashMap<Integer,Integer> hm = new HashMap<Integer, Integer>();
    Random rand = new Random(47);
    int i=0;
    while(i<5){
        i++;
        int key = rand.nextInt(20);
        int value = rand.nextInt(50);
        System.out.println("Inserting key: "+key+" Value: "+value);
        Integer imap =hm.put(key,value);
        if( imap == null){
            System.out.println("Inserted");
        }
        else{
            System.out.println("Replaced with "+imap);
        }               
    }

    hm.forEach((k,v) -> System.out.println("key: "+k+" value:"+v));

Output:

Inserting key: 18 Value: 5
Inserted
Inserting key: 13 Value: 11
Inserted
Inserting key: 1 Value: 29
Inserted
Inserting key: 8 Value: 0
Inserted
Inserting key: 2 Value: 7
Inserted
key: 1 value:29
key: 18 value:5
key: 2 value:7
key: 8 value:0
key: 13 value:11
Spliterator sit = hm.entrySet().spliterator();
sample.forEach((k,v) -> System.out.println(k + "=" + v)); 
sample.entrySet().stream().forEachOrdered((entry) -> {
            Object currentKey = entry.getKey();
            Object currentValue = entry.getValue();
            System.out.println(currentKey + "=" + currentValue);
        });
sample.entrySet().parallelStream().forEach((entry) -> {
            Object currentKey = entry.getKey();
            Object currentValue = entry.getValue();
            System.out.println(currentKey + "=" + currentValue);
        });