Map ArrayList với stream api

Giả sử chúng ta có một ArrayList Student, có name và address gồm các thuộc tính city, district, number. 

Ví dụ: Liệt kê tất cả các địa chỉ của toàn bộ sinh viên

// File Address.java
public class Address {
    private String city;
    private String district;
    private int number;

    public Address(String city, String district, int number) {
        this.city = city;
        this.district = district;
        this.number = number;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", district='" + district + '\'' +
                ", number=" + number +
                '}';
    }
}
// File Student.java

public class Student {
    private String name;
    private Address address;

    public Student(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    public Address getAddress() {
        return address;
    }

    public String getName() {
        return name;
    }
}
// File Main.java

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

public class Main {
    public static void main(String[] args) {
        List<Student> arr = new ArrayList<>();
        arr.add(new Student("A", new Address("HCM", "Q5", 282)));
        arr.add(new Student("B", new Address("HCM", "Q7", 110)));
        arr.add(new Student("F", new Address("HCM", "Q8", 328)));
        arr.add(new Student("D", new Address("HCM", "Q4", 443)));
        arr.add(new Student("A", new Address("HCM", "Q5", 560)));

        List<Address> addresses = arr.stream().map(t -> t.getAddress()).collect(Collectors.toList());
        addresses.forEach(address -> System.out.println(address.toString()));
    }
}

Output:

Address{city=’HCM’, district=’Q5′, number=282}
Address{city=’HCM’, district=’Q7′, number=110}
Address{city=’HCM’, district=’Q8′, number=328}
Address{city=’HCM’, district=’Q4′, number=443}
Address{city=’HCM’, district=’Q5′, number=560}

Ví dụ 2: Liệt kê tên của toàn bộ học sinh

// File Main.java

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

public class Main {
    public static void main(String[] args) {
        List<Student> arr = new ArrayList<>();
        arr.add(new Student("A", new Address("HCM", "Q5", 282)));
        arr.add(new Student("B", new Address("HCM", "Q7", 110)));
        arr.add(new Student("F", new Address("HCM", "Q8", 328)));
        arr.add(new Student("D", new Address("HCM", "Q4", 443)));
        arr.add(new Student("A", new Address("HCM", "Q5", 560)));

        List<String> names = arr.stream().map(t -> t.getName()).collect(Collectors.toList());
        names.forEach(name -> System.out.println(name));
    }
}

Output:

A
B
F
D
A

Map object null

// File Main.java

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

public class Main {
    public static void main(String[] args) {
        List<Student> arr = new ArrayList<>();
        arr.add(new Student("A", null));
        arr.add(new Student("B", new Address("HCM", "Q5", 282)));

        List<Address> addresses = arr.stream().map(t -> t.getAddress()).collect(Collectors.toList());
        addresses.forEach(address -> System.out.println(address.toString()));
    }
}

Với đoạn code trên mình sẽ bị NullPointerException, vì address của Student A có address null. Để tránh vấn đề trên, chúng ta sử dụng Map kết hợp với filter để lọc đi những phần tử null trước.

// File Main.java

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

public class Main {
    public static void main(String[] args) {
        List<Student> arr = new ArrayList<>();
        arr.add(new Student("A", null));
        arr.add(new Student("B", new Address("HCM", "Q5", 282)));

        List<Address> addresses = arr.stream()
                .filter(t -> t.getAddress() != null)
                .map(t -> t.getAddress()).collect(Collectors.toList());
        addresses.forEach(address -> System.out.println(address.toString()));
    }
}

Output: Address{city=’HCM’, district=’Q5′, number=282}

‹Previous Next›

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x