La demostración de la nueva característica de JDK1.8 reduce en gran medida la cantidad de código, puede echarle un vistazo si está interesado.

1. Forma tradicional de escribir Colección de listas

List<String> list = new ArrayList<String>();
list.add("张三");
list.add("王五");
list.add("尼古拉赵四");
list.add("海贼王");

2. Nueva forma de escribir la colección de listas.

 List<String> strList = Arrays.asList("YangHang", "AnXiaoHei", "LiuPengFei");

3. Filtrar objetos según condiciones Escritura tradicional

 // 筛选颜色为红色
    public  List<TestPojo> filterProductByColor(List<TestPojo> list){
    
    
        List<TestPojo> prods = new ArrayList<>();
        for (TestPojo product : list){
    
    
            if ("红色".equals(product.getName())){
    
    
                prods.add(product);
            }
        }
        return prods;
    }

    /**
     * 传统写法
     * @param list
     * @return
     */

    // 筛选价格小于8千的
    public  List<TestPojo> filterProductByPrice(List<TestPojo> list){
    
    
        List<TestPojo> prods = new ArrayList<>();
        for (TestPojo product : list){
    
    
            if (product.getPrice() < 8000){
    
    
                prods.add(product);
            }
        }
        return prods;
    }
public static void main(String[] args){
    
    
      List<TestPojo> pojoList =
              Arrays.asList(new TestPojo("紫色", 1000),
                      new TestPojo("蓝色", 8006),new TestPojo("红色", 1005));
        TestOne testOne = new TestOne();

        List<TestPojo> filterProductByColor = testOne.filterProductByColor(pojoList);
        //JDK1.8新特性,进行循环,一句代码就可以实现。
        filterProductByColor.stream().distinct().forEach(System.out::println);

Aquí hago referencia a un objeto de clase de entidad y anulo el método .equals.

public class TestPojo {
    
    

    private String name;
    private String age;
    private String sex;
    private Integer price;
    public TestPojo(String name, String age) {
    
    
        this.name = name;
        this.age = age;
    }

    public TestPojo(String name, Integer price) {
    
    
        this.name = name;
        this.price = price;
    }
    @Override
    public String toString() {
    
    
        return "TestPojo{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", price=" + price +
                '}';
    }
    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TestPojo baseBean = (TestPojo) o;
        return age == baseBean.age &&
                name.equals(baseBean.name);
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(name, age);
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public String getAge() {
    
    
        return age;
    }

    public Integer getPrice() {
    
    
        return price;
    }

    public void setPrice(Integer price) {
    
    
        this.price = price;
    }

    public void setAge(String age) {
    
    
        this.age = age;
    }

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }
}

4. Filtra los objetos según el nuevo estilo de escritura

 public static void main(String[] args){
    
    
      List<TestPojo> pojoList =
              Arrays.asList(new TestPojo("紫色", 1000),
                      new TestPojo("蓝色", 8006),new TestPojo("红色", 1005));
        TestOne testOne = new TestOne();
        /*利用新特性进行条件筛选*/
        //筛选价格小于8000的
        pojoList.stream().filter((p) -> p.getPrice() <8000).limit(2).forEach(System.out::println);
        // 筛选颜色为红色
        pojoList.stream().filter((p) -> "红色".equals(p.getName())).forEach(System.out::println);
        // 遍历输出名称
        pojoList.stream().map(TestPojo::getName).forEach(System.out::println);
        

Guess you like

Origin blog.csdn.net/Acompanys/article/details/105286788