您的位置:首页 > 编程语言 > Java开发

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

2017-11-16 10:52 127 查看
下面列举了一些,Stream的一些基本的使用方法,比如去重,遍历,筛选等。

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;
}
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java8 Stream