用的情况下能够简化那些操作
它是以个接口,它有一个泛型
集合和流,集合是用来管理和使用的
流是做什么,不管怎么做
package com.wx.day36.t1.stream;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class TestCreateStream {
public static void main(String[] args) {
//Stream--->集合、数组
List<String> list = new ArrayList<String>();
list.add("zhangsan");
list.add("lisi");
list.add("wangwu");
list.add("zhaoliu");
list.add("zhangsan");
//做什么,而不是怎么做
//Stream里存的是数据的操作,自身不保存数据
//1.创建Stream
Stream<String> stream = list.stream();
//2.中间操作
Stream<String> center = stream.filter((s)->s.length() >= 5);
//3.终止操作 最终操作
System.out.println("名字长度大于5的是:");
center.forEach(System.out::println);
//简化
list.stream().filter((s)->s.length()>=5).forEach(System.out::println);
List<String> newList = new ArrayList<String>();
for(String s:list){
if(s.length()>=5)
newList.add(s);
}
System.out.println("名字长度大于等于5的是:");
for(String s: newList)
System.out.println(s);
}
}
package com.wx.day36.t1.stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TestStreamMethod {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("zhangsan");
list.add("lisi");
list.add("wangwu");
list.add("zhaoliu");
list.add("zhangsan");
System.out.println("------------filter----------");
//filter--->过滤 指定规则
list.stream().filter((s)->s.startsWith("z")).filter((s)->s.contains("s")).forEach(System.out::println);
System.out.println("--------------limit---------------");
//limit--->截断,返回不超过指定数量
list.stream().limit(4).forEach(System.out::println);
System.out.println("-----------------------------");
//distinct--->筛选,利用hashcode和equals
list.stream().distinct().forEach(System.out::println);
System.out.println("-----------distinct------------");
List<Emp> elist = new ArrayList<>();
elist.add(new Emp("高强", 28, 10));
elist.add(new Emp("高蓝轩", 28, 10));
elist.add(new Emp("高f强", 78, 100));
elist.add(new Emp("高f强", 78, 100));
elist.add(new Emp("高强", 28, 10));
//不会影响元数据
elist.stream().distinct().forEach(System.out::println);
System.out.println("-----------------------------");
elist.forEach(System.out::println);
//map值得是 不是集合
System.out.println("--------------Map------------");
//emp-->T getName()-->R
elist.stream().map((emp)->emp.getName()).forEach(System.out::println);
elist.stream().map((emp)->emp.getScore()).forEach(System.out::println);
List<String> strlist = Arrays.asList("aaa","bbb","ccc","dddf");
strlist.stream().map((s)->s.toUpperCase()).forEach(System.out::println);
System.out.println("-----------------------------");
strlist.stream().map(String::toUpperCase).forEach(System.out::println);
//sorted排序,流中的元素实现Comparable接口
list.stream().sorted().forEach(System.out::println);
System.out.println("--------------sorted(Comparator)---------------");
elist.stream().sorted((x,y)->Double.compare(x.getScore(), y.getScore())).forEach(System.out::println);
}
}
package com.wx.day36.t1.stream;
public class Emp {
private String name;
private int age;
private double score;
public int hashCode(){
return (int)(this.name.hashCode() + this.age + this.score);
}
public boolean equals(Object obj){
//1.比较两个引用是否指向同一个对象
if(this==obj)
return true;
//2.obj是否为空
if(obj==null)
return false;
//3.判断两个引用的实际类型
if(this.getClass()!=obj.getClass())
return false;
//4.强转
Emp emp = (Emp)obj;
//5.比较
if(this.name.equals(emp.getName()) && this.age == emp.getAge() && this.score==emp.getScore())
return true;
return false;
}
public Emp(){
super();
}
public Emp(String name, int age, double score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Emp [name=" + name + ", age=" + age + ", score=" + score + "]";
}
}
package com.wx.day36.t2.end;
import java.util.ArrayList;
import java.util.List;
public class TestEndMethod {
public static void main(String[] args) {
List <String> list = new ArrayList<String>();
list.add("Blsit");
list.add("dlsit");
list.add("fglsit");
list.add("hsit");
list.add("sdfgit");
list.add("sdfgfdsit");
list.add("hhhsit");
int count = (int)list.stream().filter((s)->s.length()>=3).count();
System.out.println(count);
boolean b1 = list.stream().filter((s)->s.length()>=5).anyMatch((s)->s.startsWith("f"));
System.out.println(b1);
boolean b2 = list.stream().filter((s)->s.length()>=6).allMatch((s)->s.contains("t"));
System.out.println(b2);
boolean b3 = list.stream().filter((s)->s.length()>=7).noneMatch((s)->s.contains("t"));
System.out.println(b3);
String str = list.stream().filter((s)->s.length()>=5).findFirst().get();
System.out.println(str);
String str2 = list.stream().filter((s)->s.length()>=6).findAny().get();
System.out.println(str2);
}
}
package com.wx.day36.t3.threads;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class TestStream {
public static void main(String[] args) {
List <String> list = new ArrayList<String>();
for(int i = 0 ; i < 306600 ; i++){
UUID u = UUID.randomUUID();
list.add(u.toString());
}
// long start = System.currentTimeMillis();
// long count = list.stream().sorted().count();
// System.out.println(count);
// long end = System.currentTimeMillis();
// System.out.println(end-start);
//
long start = System.currentTimeMillis();
long count = list.parallelStream().sorted().count();
System.out.println(count);
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}