Java filter 移除 java的filter函数

发布时间:2023-05-18 09:16:51

Java中 filter()函数的使用

Java中 filter()函数的使用

Filter函数在Java中的用法
  • 介绍
  • 详细代码
  • Java8之前的写作方法
  • Java8写法1
  • Java8写法2
  • 造数和测试代码
  • 测试结果
  • 参考资料链接:
介绍

Java 8 Stream接口引入了filter()方法,可以用来根据特定条件从对象集中过滤某些元素。该条件应指定为filter()方法,以接受作为参数的谓词。

predicate接口定义了一种叫test()的抽象方法,它接受泛型T的对象,并返回布尔值。

让我们编写一些代码,以便更清楚地理解filter方法。看看下面的Dish类。

详细代码

package com.liu.filter;/** * @Decision TODO * @author liusx * @Date 2021年8月17日 */public class Dish {private String name;    private Boolean vegitarian;    private Integer calaries;    private Type type;    public Dish(String name, Boolean vegitarian, Integer calaries, Type type) {         super();         this.name = name;         this.vegitarian = vegitarian;         this.calaries = calaries;         this.type = type;    }    public Boolean getVegitarian() {        return vegitarian;    }    public void setVegitarian(Boolean vegitarian) {        this.vegitarian = vegitarian;    }    public Type getType() {        return type;    }    public void setType(Type type) {        this.type = type;    }    public enum Type { MEAT, FISH, OTHER };}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

让我们想想,我们只想从所有菜肴的列表中过滤素食菜肴。以下是Java 8之前的方法。

Java8之前的写作方法

public static List<Dish> getVegetarianDishes_old(List<Dish> menus) {List<Dish> vegetarianDishes = new ArrayList<Dish>();////找蔬菜菜单forr (Dish d : menus) {if (d.getVegitarian()) {vegetarianDishes.add(d);}}return vegetarianDishes;}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

上述方法称为外部迭代,我们显式地管理数据集中的迭代。

Java怎么用? 实现这一点只是一个单行问题,如下所示。

Java8写法1

public static List<Dish> getVegetarianDishes_new(List<Dish> menus) {List<Dish> vegitarianDishes = menus.stream()///找蔬菜菜单                .filter(d -> d.getVegitarian())                .collect(Collectors.toList());return vegitarianDishes;}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

我们以Lambda表达式的形式将Predicate实例传递给filter()。

我们也可以使用java 如下所示,引用将Predicate实例传递给filter()的方法。

Java8写法2

List<Dish> menu = ...List<Dish> vegitarianDishes = menu.stream()                                    .filter(Dish::getVegitarian)                                    .collect(Collectors.toList());

  • 1
  • 2
  • 3
  • 4

Dish::getvegitarianJava 引用8种方法的语法。它指的是Dish类getvegitarian()方法。

filter()方法返回一个盘流,collect()该方法将此流转换成列表。“收集”操作称为终端操作。

假设我们想要的前三道菜热量超过300卡路里。流量支持limitt(n)方法,它返回不超过给定大小的另一个流。请求的大小作为参数传递给limit。

List<Dish> menu = ...List<Dish> threeHighCalaricDish = menu.stream()                                         .filter(d -> d.getCalaries() > 300)                                         .limit(3)                                         .collect(Collectors.toList());

  • 1
  • 2
  • 3
  • 4
  • 5

类似地,如果我们想跳过前三个元素,流量支持skip(n)方法返回丢弃前n个元素的流动。如果流动元素小于n,则返回空流。注意limitt(n)和skip(n)是互补的!

用流过滤前两道肉菜,如下:

List<Dish> menu = ...List<Dish> meatDishes = menu.stream()                                  .filter(d -> d.getType() == Dish.Type.MEAT)                                  .limit(2)                                  .collect(Collectors.toList())

  • 1
  • 2
  • 3
  • 4
  • 5
造数和测试代码

package com.liu.filter;import java.util.ArrayList;import java.util.List;import java.util.stream.Collectors;import com.alibaba.fastjson.JSON;import com.liu.filter.Dish.Type;/** * @Decision Java测试 在8中使用filter()方法 不同于8之前的方法 * @author liusx * @Date 2021年8月17日 */public class TestFilter {public static void main(String[] args) {List<Dish> menus = getAllMenus();//java8之前的Listtt方法<Dish> vegetarianDishes = getVegetarianDishes_old(menus);    System.out.println(old方法::"+JSON.toJSONString(vegetarianDishes));   //java8后的方法    List<Dish> vegetariandishes2 = getVegetarianDishes_new(menus);    System.out.println(new方法::"+JSON.toJSONString(vegetariandishes2);}/** * @decision java8之前的方法 * @param menus * @return * @author liusx * @Date 2021年8月17日 */public static List<Dish> getVegetarianDishes_old(List<Dish> menus) {List<Dish> vegetarianDishes = new ArrayList<Dish>();////找蔬菜菜单forr (Dish d : menus) {if (d.getVegitarian()) {vegetarianDishes.add(d);}}return vegetarianDishes;}/** * @decision java8方法 * @param menus * @return * @author liusx * @Date 2021年8月17日 */public static List<Dish> getVegetarianDishes_new(List<Dish> menus) {List<Dish> vegitarianDishes = menus.stream()///找蔬菜菜单                .filter(d -> d.getVegitarian())                .collect(Collectors.toList());return vegitarianDishes;}/** * @decision 制作menu数据 * @return * @author liusx * @Date 2021年8月17日 */public static List<Dish> getAllMenus() {List<Dish> menus = new ArrayList<Dish>();////所有菜Dish d1 = new Dish(油麦菜”, true, 100, Type.OTHER);Dish d2 = new Dish(“青鱼”, false, 100, Type.FISH);Dish d3 = new Dish(“大鹅”, false, 100, Type.MEAT);Dish d4 = new Dish(菠菜”, true, 100, Type.OTHER);menus.add(d1);menus.add(d2);menus.add(d3);menus.add(d4);return menus;}}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
测试结果

Java filter 移除 java的filter函数_java

本文是转载内容,我们尊重原作者对文章的权利。如有内容错误或侵权行为,请联系我们更正或删除文章。

上一篇 Java SOC 算法 java算法是什么
下一篇 Java 读取XML文件内容 Map java读取xml文件的几种方式

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题