list 안에 있는 대상의 어떤 속성을 통해 그룹을 나누고 그룹을 나누어 화합을 구하고 무게를 제거합니다

1783 단어
1. 실체 클래스와list 집합
// 
public class User {
    private int id;
    private String name;
    private int age;
    
    public User(int id,String name,int age){
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

//list 만들기
List users = new ArrayList<>();
users.add(new User(1,"A",8));
users.add(new User(2,"A",7));
users.add(new User(3,"A",6));
users.add(new User(4,"B",7));
users.add(new User(5,"B",6));
users.add(new User(6,"C",6));
users.add(new User(6,"C",6));

2, list 작업
  /** * 1, (name) **/ Map> collectName = users.stream().collect(Collectors.groupingBy(User::getName)); System.out.println(collectName); /** * 2, (name) , age **/ Map collectNameSumAge = users.stream().collect(Collectors.groupingBy(User::getName,Collectors.summingInt(User::getAge))); System.out.println(collectNameSumAge); /** * 3, (name) */ List names = users.stream().map(User::getName).collect(Collectors.toList()); System.out.println(names); /** * 4,list */ names = names.stream().distinct().collect(Collectors.toList()); System.out.println(names); users = users.stream().distinct().collect(Collectors.toList()); System.out.println(users); /** * 5, (name) **/ users = users.stream().filter(user -> user.getName().equals("A")).collect(Collectors.toList()); System.out.println(users); /** * 6, */ boolean checkName = names.contains("A"); System.out.println(checkName); boolean bool = users.stream().anyMatch(user -> "A".equals(user.getName())); System.out.println(bool);

좋은 웹페이지 즐겨찾기