java8 forEach Map List

java8 forEach 在Map和List中的使用

  • 原始的使用

    Map<String, Integer> items = new HashMap<>();
    items.put(“A”, 10);
    items.put(“B”, 20);
    items.put(“C”, 30);
    items.put(“D”, 40);
    items.put(“E”, 50);
    items.put(“F”, 60);

    for (Map.Entry<String,Integer> entry : items.entrySet()){
    System.out.println(“key:”+entry.getKey()+“;value:”+entry.getValue());
    }
    //output
    A—10
    B—20
    C—30
    D—40
    E—50
    F—60

  • forEach 使用方式

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

    //output
    key : A value : 10
    key : B value : 20
    key : C value : 30
    key : D value : 40
    key : E value : 50
    key : F value : 60

    items.forEach((k,v)->{
    System.out.println("Item : " + k + " Count : " + v);
    if(“E”.equals(k)){
    System.out.println(“Hello E”);
    }
    });

    key : A; value : 10
    key : B; value : 20
    key : C; value : 30
    key : D; value : 40
    key : E; value : 50
    Hello E

  • java8 List 原先的使用方式

    List arrayList = new ArrayList<>();
    arrayList.add(“A”);
    arrayList.add(“B”);
    arrayList.add(“C”);
    arrayList.add(“D”);
    arrayList.add(“E”);

    for (String item:arrayList){
    System.out.println(item);
    }

  • java8 forEach 使用方式

    arrayList.forEach(item->System.out.println(item));

    arrayList.forEach(System.out::println);

    arrayList.forEach(item->{
    if(“C”.equals(item)){
    System.out.println(item);
    }
    });

    arrayList.stream()
    .filter(s-> s.contains(“B”)||s.contains(“C”))
    .forEach(System.out::println);

    arrayList.stream()
    .filter(s->s.contains(“E”))
    .findFirst().ifPresent(s -> System.out.println(s));

参考例子

java8-forEach-examples
IBM-java8-stream

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 8中,可以使用Stream对List进行分组,并将结果转换为Map。下面是一个示例代码: ```java import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class GroupingExample { public static void main(String[] args) { // 假设有一个Person类,包含name和age属性 List<Person> personList = List.of( new Person("Alice", 25), new Person("Bob", 30), new Person("Alice", 35), new Person("Charlie", 40) ); // 使用Stream的groupingBy方法进行分组,并将结果转换为Map Map<String, List<Person>> personMap = personList.stream() .collect(Collectors.groupingBy(Person::getName)); // 打印分组结果 personMap.forEach((name, group) -> { System.out.println(name + ": " + group); }); } } class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } } ``` 在上述示例中,我们首先定义了一个Person类,包含name和age属性。然后,我们创建了一个包含Person对象的List。 接下来,在使用Stream的`groupingBy`方法时,我们传递了一个函数`Person::getName`作为分组的依据,它将根据Person对象的name属性进行分组。最后,通过`collect(Collectors.groupingBy())`将分组结果转换为Map。 最后,我们使用`forEach`方法遍历Map,并打印分组结果。输出结果如下: ``` Alice: [Person{name='Alice', age=25}, Person{name='Alice', age=35}] Bob: [Person{name='Bob', age=30}] Charlie: [Person{name='Charlie', age=40}] ``` 以上是一个简单的示例,你可以根据自己的需求进行适当的修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值