java map5种遍历方法和java 8 stream Api使用

package com.demo.springlearn.entity;

import java.util.*;

public class MapIterator {
    public static void main(String[] args) {
        Map<Integer, String> coursesMap = new HashMap<Integer, String>();
        coursesMap.put(1, "C");
        coursesMap.put(2, "C++");
        coursesMap.put(3, "Java");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "Hibernate ORM framework");
        Iterator<Map.Entry<Integer, String>> iterator = coursesMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, String> next = iterator.next();
            Integer key = next.getKey();
            String value = next.getValue();
            System.out.println(key + value);
        }
        Iterator<Integer> iterator1 = coursesMap.keySet().iterator();
        while (iterator.hasNext()) {
            Integer key = iterator1.next();

            String value = coursesMap.get(key);
            System.out.println(key + value);
        }
        for (Map.Entry<Integer, String> entry : coursesMap.entrySet()) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + value);
        }
        coursesMap.forEach((key, value) -> {
                    System.out.println(key);
                    System.out.println(value);
                }
        );
        coursesMap.entrySet().stream().forEach(integerStringEntry -> {
            System.out.println(integerStringEntry.getKey()+integerStringEntry.getValue());
        });
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("欧阳雪",18,"中国",'F'));
        personList.add(new Person("Tom",24,"美国",'M'));
        personList.add(new Person("Harley",22,"英国",'F'));
        personList.add(new Person("向天笑",20,"中国",'M'));
        personList.add(new Person("李康",22,"中国",'M'));
        personList.add(new Person("小梅",20,"中国",'F'));
        personList.add(new Person("何雪",21,"中国",'F'));
        personList.add(new Person("李康",22,"中国",'M'));
        // 1)找到年龄大于18岁的人并输出;
        personList.stream().filter(person ->
            person.getAge()>20).forEach(System.out::println);
        long countChina = personList.stream().filter(person -> person.getCountry().equals("中国")).count();
        System.out.println(countChina);
        personList.stream().filter(person -> person.getSex()=='F').limit(2).forEach(System.out::println);
    }
}

package com.fc.qa.pointservice.model;

import lombok.Data;
import org.apache.ibatis.logging.stdout.StdOutImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
class Person {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    public Person(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }


    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("欧阳雪", 18, "中国", 'F'));
        personList.add(new Person("Tom", 24, "美国", 'M'));
        personList.add(new Person("Harley", 22, "英国", 'F'));
        personList.add(new Person("向天笑", 20, "中国", 'M'));
        personList.add(new Person("李康", 22, "中国", 'M'));
        personList.add(new Person("小梅", 20, "中国", 'F'));
        personList.add(new Person("何雪", 21, "中国", 'F'));
        personList.add(new Person("李康", 22, "中国", 'M'));
        List<String> country = new ArrayList<>();
        personList.stream().sorted((p1, p2) -> {
            return p1.getAge().compareTo(p2.getAge());
        }).collect(Collectors.groupingBy(Person::getCountry)).forEach((k,v)-> System.out.println(k+v));
        Map<String,List<Person>> pp = personList.stream().sorted((p1, p2) -> {
            return p1.getAge().compareTo(p2.getAge());
        }).collect(Collectors.groupingBy(Person::getCountry));
       for(Map.Entry<String,List<Person>> p:pp.entrySet()){
           System.out.print(p.getKey());
           System.out.println(p.getValue());
       }

    }
}


浅谈对Java双冒号::的理解

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值