Java8---Stream的基本用法(2)

下面列举了一些,Stream的一些基本的使用方法,比如去重,遍历,筛选,获取指定元素的等。

获取集合中制定元素

List<IndustryInfo> listRe = new ArrayList<>();//开发中是有元素的
IndustryInfo industryInfo = listRe.stream().filter(f -> f.getName().toString().equals("其他")).collect(Collectors.toList()).get(0);


public static void test2(){
       //创建Stream
       Stream<Integer> integerStream = Stream.of(1,2,3);

       // Individual values
       Stream stream = Stream.of("a", "b", "c");

       // Arrays
       String [] strArray = new String[] {"a", "b", "c"};
       Stream stream2 = Stream.of(strArray);
       Stream stream3 = Arrays.stream(strArray);

       // Collections
       List<String> list = Arrays.asList(strArray);
       Stream stream4 = list.stream();

       //创建一个集合
       List<Integer> list2 = Lists.newArrayList(1,99,2,343,2,3,22,null,45,34,null,34,7);

       //输出
       list2.stream().forEach(System.out::println);
       System.out.println("-------------");

       //去重,输出
       list2.stream().distinct().forEach(System.out::println);
       System.out.println("-------------");

       //去掉null值
       list2.stream().filter(li->li != null).forEach(System.out::println);
       System.out.println("-------------");

       //每个元素乘以2
       list2.stream().filter(li->li != null).mapToInt(li->li*2).forEach(System.out::println);
       System.out.println("-------------");

       //每个元素诚乘以3
       IntStream s2 = list2.stream().filter(li -> li != null).mapToInt(li -> li * 3);
       s2.forEach(System.out::println);
       System.out.println("-------------");

       //截取操作,获取前N位,不足N位,就全部获取
       Stream<Integer> list4 = list2.stream().limit(4);
       list4.forEach(System.out::println);
       System.out.println("-------------");

       //丢弃操作,舍弃前N位,不足N位,返回空Stream
       Stream<Integer> skipList = list2.stream().skip(8);
       skipList.forEach(System.out::println);
       System.out.println("-------------");

       //统计个数
       long count = list2.stream().filter(li -> li != null).count();
       System.out.println(count);
       System.out.println("-------------");

       //大于200的
       list2.stream().filter(li ->li != null && li >200).forEach(System.out::println);
       System.out.println("-------------");

       //求和
       int sum = list2.stream().filter(li -> li != null).mapToInt(li -> li).sum();
       System.out.println(sum);
       System.out.println("-------------");

       //输出1,2,3,4
       IntStream.range(1,5).forEach(System.out::println);

       //输出1,2,3,4,5
       IntStream.rangeClosed(1,5).forEach(System.out::println);

       //变大写
       List<String> collect = list.stream().map(String::toUpperCase).collect(Collectors.toList());
       collect.stream().forEach(System.out::println);

   }
   public static void test3(){
       Stream<Integer> integerStream = Stream.of(1,5,4,55,64,88);
       //求和
       int sum = integerStream.mapToInt(li -> li).sum();
       System.out.println(sum);

       //stream->Array
       Stream<Integer> integerStream2 = Stream.of(1,5,4,55,64,88);
       Integer[] integers = integerStream2.toArray(Integer[]::new);

       //stream->Collection
       Stream<Integer> integerStream3 = Stream.of(1,5,4,55,64,88);
       List<Integer> list = integerStream3.collect(Collectors.toList());
       //传统集合求和
       int sum2 =0;
       for(int i = 0;i < list.size();i++){
           sum2+= list.get(i);
       }
       System.out.println("和为:"+sum2);
       //steam留求和
       int sum3 = list.stream().mapToInt(li->li).sum();
       System.out.println(sum3);

       //最大值
       Stream<Integer> integerStream4 = Stream.of(1,5,4,55,64,88);
       OptionalInt max = integerStream4.mapToInt(li -> li).max();
       System.out.println(max);

   }
/**
     * 聚合  reduce()
     */
   public static void test7(){
       List<Integer> ints = Lists.newArrayList(22,1,2,3,4,5,6,7,8,9,10,45,78,67);
       //求和 这个0是循环计算的初始值
       Integer reduce = ints.stream().reduce(0,(sum,item)->sum+item);
       System.out.println(reduce);
       //统计个数
       long count = ints.stream().count();
       System.out.println(count);

       //allMatch():是不是所有的元素都满足这个条件
       boolean b = ints.stream().allMatch(num -> num > 10);
       System.out.println(b);

       //anyMatch():是不是任何一个元素都满足这个条件
       boolean b1 = ints.stream().anyMatch(integer -> integer < 1);
       System.out.println(b1);

       Optional<Integer> first = ints.stream().findFirst();
       System.out.println(first);
   }


下面简单的演示一下,当集合的元素为对象时,Stream做筛选过滤的用法,可以看到在用Stream处理这种多条件的筛选时,比集合简单很多

public static void test5(){
       List<Fruit> fruits = test4();
       //河南 苹果 大于8元
       Stream<Fruit> fruitStream = fruits.stream().
               filter(f -> f.getAddress().equals("河南"))
               .filter(f -> f.getName().equals("苹果"))
               .filter(f -> f.getPrice() > 8);
       fruitStream.forEach(System.out::println);

   }
   public static List<Fruit> test4(){
       Fruit f1 = new Fruit("苹果","red","河南",6,500,"好吃");
       Fruit f2 = new Fruit("苹果","yellow","山西",6,500,"好吃");
       Fruit f3 = new Fruit("苹果","green","山西",5,500,"好吃");
       Fruit f11 = new Fruit("苹果","yellow","河南",22,100,"不好吃");
       Fruit f9 = new Fruit("苹果","yellow","河南",12,100,"不好吃");
       Fruit f10 = new Fruit("苹果","yellow","河南",24,100,"不好吃");
       Fruit f6 = new Fruit("苹果","green","山西",6,300,"好吃");

       Fruit f4 = new Fruit("柑橘","red","山西",6,500,"不好吃");
       Fruit f5 = new Fruit("柑橘","red","河南",5,600,"好吃");
       Fruit f12 = new Fruit("柑橘","red","河南",5,800,"好吃");
       Fruit f7 = new Fruit("柑橘","red","山西",12,500,"好吃");

       Fruit f8 = new Fruit("香蕉","green","山西",6,500,"好吃");

       List<Fruit> list1 = new ArrayList<>();
       list1.add(f1);
       list1.add(f2);
       list1.add(f3);
       list1.add(f4);
       list1.add(f5);
       list1.add(f6);
       list1.add(f7);
       list1.add(f8);
       list1.add(f9);
       list1.add(f10);
       list1.add(f11);
       list1.add(f12);

       return list1;


   }
package test;

/**
 * Created by lightClouds917
 * Date 2017/11/3
 * Description:
 */
public class Fruit {
    private String name;
    private String color;
    private String address;
    private Integer price;
    private Integer stock;
    private String description;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Fruit{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", address='" + address + '\'' +
                ", price=" + price +
                ", stock=" + stock +
                ", description='" + description + '\'' +
                '}';
    }

    public Fruit(String name, String color, String address, Integer price, Integer stock, String description) {
        this.name = name;
        this.color = color;
        this.address = address;
        this.price = price;
        this.stock = stock;
        this.description = description;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getPrice() {
        return price;
    }

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

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

                
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值