映射项集的forEach循环Java 8

映射项集的forEach循环Java 8,java,lambda,java-8,Java,Lambda,Java 8,我试图将旧的传统for each循环转换为java7到java8 for each循环,用于映射条目集,但我得到了一个错误。 以下是我试图转换的代码: for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } for(Map.Entry:Ma

我试图将旧的传统for each循环转换为java7到java8 for each循环,用于映射条目集,但我得到了一个错误。 以下是我试图转换的代码:

for (Map.Entry<String, String> entry : map.entrySet()) {
        System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
    }
for(Map.Entry:Map.entrySet()){
System.out.println(“Key:+entry.getKey()+”值:+entry.getValue());
}
以下是我所做的更改:

map.forEach( Map.Entry<String, String> entry -> {
       System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());

   }); 
map.forEach(map.Entry->{
System.out.println(“Key:+entry.getKey()+”值:+entry.getValue());
}); 
我也尝试过这样做:

Map.Entry<String, String> entry;
   map.forEach(entry -> {
       System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());

   });
Map.Entry;
map.forEach(条目->{
System.out.println(“Key:+entry.getKey()+”值:+entry.getValue());
});
但仍然面临错误。我得到的错误是:
Lambda表达式的签名与函数接口方法的签名不匹配
accept(String,String)
Read:
Map.forEach()
需要一个
BiConsumer也许回答“哪个版本更快,我应该使用哪个版本?”这类问题的最佳方法是查看源代码:

map.forEach()-来自map.java

default void forEach(BiConsumer<? super K, ? super V> action) {
    Objects.requireNonNull(action);
    for (Map.Entry<K, V> entry : entrySet()) {
        K k;
        V v;
        try {
            k = entry.getKey();
            v = entry.getValue();
        } catch(IllegalStateException ise) {
            // this usually means the entry is no longer in the map.
            throw new ConcurrentModificationException(ise);
        }
        action.accept(k, v);
    }
}

defaultvoidforeach(BiConsumer您可以根据自己的需求使用以下代码

map.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));
HashMap hm=newhashmap();
hm.put(“A”,1);
hm.put(“B”,2);
hm.put(“C”,3);
hm.put(“D”,4);
hm.forEach((键,值)->{
System.out.println(“键:+Key+”值:+value”);
});
streamapi
public void iterateStreamAPI(映射){
map.entrySet().stream().forEach(e->System.out.println(e.getKey()+“:”e.getValue());
}
String ss=“Pawan kavita kiyans Patidar Patidar”;
StringBuilder ress=新的StringBuilder();
Map fre=ss.chars().boxed()
.collect(Collectors.toMap(k->Character.valueOf((char)k.intValue()),k->1,Integer::sum));
//fre.forEach((k,v)->System.out.println((k+“:“+v));
fre.entrySet().forEach(e->{
//System.out.println(e.getKey()+“:”+e.getValue());
//append(String.valueOf(e.getKey())+e.getValue());
}); 
每小时频率((k,v)->{
//系统输出打印项次(“项目:+k+”计数:+v);
press.append(String.valueOf(k)+String.valueOf(v));
});
System.out.println(ress.toString());

下面是最好的方法 1.使用条目集进行迭代

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + ":" + entry.getValue());
}
for(Map.Entry:Map.entrySet()){
System.out.println(entry.getKey()+“:”+entry.getValue());
}
  • 兰博达斯 map.forEach((k,v)->System.out.println((k+“:”+v))
  • 3流 map.entrySet().stream()
    .forEach(e->System.out.println(e.getKey()+“:”+e.getValue())

    很好。现在两者都可以使用。但是哪一种方法更好?在哪种情况下性能会更好?@jbnizet第二个版本强制为每个条目创建一个
    Map.Entry
    实例;第一个版本提供键和值,而不需要实例化。因此
    Map.Entry
    是一个中间人,您可以避免使用第一个版本。@Marko Topolnik:对于大多数
    Map
    实现而言,
    Map.Entry
    实例在迭代之前已经存在,并且不需要创建。不过,不必在操作中处理
    Map.Entry
    是一种可读性上的胜利,并且由于没有额外的方法调用,因此有可能获得更好的性能需要使用ons来检索键和值。@Holger我要强调您的隐含含义:对于大多数但不是所有的
    Map
    实现来说,
    ConcurrentHashMap
    是一个重要的反例。@Marko Topolnik:但是当迭代整个
    ConcurrentHashMap
    时,临时条目实例是最后一个尽管如此,我们还是同意使用
    map.forEach((key,value)->…)
    无论如何……此答案与JB Nizet 2年前提供的部分答案相同,没有提供任何其他有用的信息。请对您的答案进行解释。这将帮助其他人根据自己的需要调整您的答案。虽然此代码片段可能是解决方案,但确实有助于提高您文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }
    
    map.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));
    
    HashMap<String,Integer> hm = new HashMap();
    
     hm.put("A",1);
     hm.put("B",2);
     hm.put("C",3);
     hm.put("D",4);
    
     hm.forEach((key,value)->{
         System.out.println("Key: "+key + " value: "+value);
     });
    
    public void iterateStreamAPI(Map<String, Integer> map) {
        map.entrySet().stream().forEach(e -> System.out.println(e.getKey() + ":"e.getValue()));
    }
    
    String ss = "Pawan kavita kiyansh Patidar Patidar";
        StringBuilder ress = new StringBuilder();
        
        Map<Character, Integer> fre = ss.chars().boxed()
                .collect(Collectors.toMap(k->Character.valueOf((char) k.intValue()),k->1,Integer::sum));
        
          //fre.forEach((k, v) -> System.out.println((k + ":" + v)));
        
        fre.entrySet().forEach(e ->{
                //System.out.println(e.getKey() + ":" + e.getValue());
                //ress.append(String.valueOf(e.getKey())+e.getValue());
            }); 
    
        fre.forEach((k,v)->{
            //System.out.println("Item : " + k + " Count : " + v);
            ress.append(String.valueOf(k)+String.valueOf(v));
        });
        
        System.out.println(ress.toString());
    
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }