首页 > 其他分享 >stream流之distinct

stream流之distinct

时间:2022-09-22 09:48:17浏览次数:33  
标签:stream Person distinct 流之 private personList add new public

1、对于string的去重

直接使用distinct()

public void test() {
    List<String> strList = new ArrayList<>();
    strList.add("A");
    strList.add("A");
    strList.add("B");
    collect = strList.stream().distinct().collect(Collectors.toList());
}

 

2、对对象的去重

① 使用@Data,重写了equals和hscode,是对所有元素的一个校验去重,直接使用distinct()

@Data
public class request {
        @ApiModelProperty(value = "商品id")
        @JsonProperty("product_id")
        private String productId;

        @ApiModelProperty(value = "商品条码")
        @JsonProperty("product_bar_code")
        private String productBarCode;

        @ApiModelProperty(value = "商品名称")
        @JsonProperty("product_name")
        private String productName;
}

② 对于指定字段,自定义重写equals和hscode

@Data
public class request {
        @ApiModelProperty(value = "商品id")
        @JsonProperty("product_id")
        private String productId;

        @ApiModelProperty(value = "商品条码")
        @JsonProperty("product_bar_code")
        private String productBarCode;

        @ApiModelProperty(value = "商品名称")
        @JsonProperty("product_name")
        private String productName;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            productDetail that = (productDetail) o;
            return Objects.equals(productId, that.productId) &&
                    Objects.equals(productBarCode, that.productBarCode) 
        }

        @Override
        public int hashCode() {
            return Objects.hash(productId, productBarCode);
        }
}

是对商品id和条码的校验去重

③ 引用Collectors两个静态方法collectingAndThen()和toCollection(),以及TreeSet<>来去重

@Test
public void test()  {
    List<Person> personList = new ArrayList<>();
    personList.add(new Person("zhangsan", 18));
    personList.add(new Person("zhangsan", 19));
    personList.add(new Person("lisi", 18));

personList = personList.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new ));
}

 

④ filter() + 自定义函数

private static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
    Map<Object, Boolean> map = new ConcurrentHashMap<>();
    return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

输入元素的类型是T及其父类,keyExtracctor是映射函数,返回Object,整个传入的函数的功能应该是提取key的。distinctByKey函数返回的是Predicate函数,类型为T
传入一个函数,将传入对象key放入ConcurrentHashMap,数据不重复,返回true,不重复的数据通过filter。

@Test
public void test()  {
    List<Person> personList = new ArrayList<>();
    personList.add(new Person("zhangsan", 18));
    personList.add(new Person("zhangsan", 19));
    personList.add(new Person("lisi", 18));
    personList = personList.stream().filter(distinctByKey(Person::getName)).collect(Collectors.toList());
}

 

标签:stream,Person,distinct,流之,private,personList,add,new,public
From: https://www.cnblogs.com/shirleyxueli/p/16718070.html

相关文章

  • Java Stream流
    Java8Stream流编程Stream使用一种类似于SQL语句从数据库查询数据的直观方式来提供对Java集合运算和表达的高阶抽象。得益于Lambda所带来的函数式编程,StreamAPI可......
  • OtherStream
    OtherStream一,标准的输入,输出流1.标准的输入、输出流1.1System.in:标准的输入流,默认从键盘输入System.out:标准的输出流,默认从控制台输出1.2System类的setIn(InputS......
  • 使用FileStream来实现复制功能
    staticvoidMain(string[]args){stringsource=@"D:\桌面\demo.txt";stringtarget=@"D:\桌面\demo2.txt";CopyF......
  • c#中的FileStream文件流
    staticvoidMain(string[]args){//FileStream(操作字节的)//1.创建FileStream对象FileStreamfs=newFileStream(@......
  • Java IO流(Stream)
    1.Stream流一个流可以理解为一个数据的序列。输入流表示从一个源读取数据,输出流表示向一个目标写数据。Stream可以定义为数据序列。有两种流-InPutStream   -......
  • Java8之list.stream的常见使用
    List<Integer>list=newArrayList<Integer>();从小到大方法:Collections.sort(list);从大到小方法:Collections.sort(list,Collections.reverseOrder());stream获取l......
  • Java8 Stream使用汇总总结
    前言:近期在coding过程中经常使用到java8的Stream,故在此做个汇总总结,积累沉淀下常用方法,希望对读的人也有所帮助,下一篇总结下lambda。文章目录Java8Stream1Stream概......
  • java-stream-函数式接口
    一、概述java是面向对象的,对象往往映射现实生活中的某个具体的东西,绝大多数保存在java内存的堆中;java的函数式编程,则可以将对象指向一个函数(行为),而非具体的东西;函数式接......
  • java流之装饰者模式
    扩展类在于继承FilterInputStream,从而增强read和writer方法的能力输出流同样如此。   推荐文章:https://blog.csdn.net/hustzw07/article/details/80795855......
  • java FileInputStream的相对路径
    javaFileInputStream的相对路径是相对于src目录的publicvoidbufferTest(){try(FileChannelchannel=newFileInputStream("src/main/resources/data.txt").get......