他提供了一种处理集合和数组的方式,可以用于过滤、映射、排序、聚合等操作,使代码更加简洁、易懂。结合了lambda表达式,简化集合、数组的操作。
使用步骤:
1)先得到一条Stream流,并把数据放上去。
2)使用中间方法,对流水线上的数据进行操作。
3)使用终结方法对流水线上的数据进行操作。
中间方法:方法调用完毕之后,还可以调用其他方法。
终结方法:最后一步,调用完毕后,不能调用其他方法
获取Stream流方式:
1.单列集合
public class test {
public static void main(String [] args) {
//创建单列集合
ArrayList<String> list=new ArrayList<>();
Collections.addAll(list, "a","b","c","d","e");
list.stream().forEach(s->System.out.println(s));//遍历
}
}
2.双列集合
public class test {
public static void main(String [] args) {
//创建双列集合
HashMap<String, Integer> hm=new HashMap<>();
hm.put("a", 1);
hm.put("f", 3);
hm.put("d",5);
hm.put("s", 9);
//一种方法
hm.keySet().stream().forEach(s->System.out.println(s));
System.out.println("--------------");
//第二种方法
hm.entrySet().stream().forEach(t->System.out.println(t));
}
}
3.数组
public class test {
public static void main(String [] args) {
//创建数组
String [] arr= {"a","b","c","d","t"};
Arrays.stream(arr).forEach(s->System.out.print(s));
}
}
4.一堆零散
public class test {
public static void main(String [] args) {
Stream.of(1,2,3,4,5,6).forEach(s->System.out.println(s));
}
}
Stream流中间方法
1.过滤(filter)
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三","李四","张无忌","李若","张开账","荣天真");
list.stream().filter(s->s.startsWith("张"))
.filter(s->s.length()==3)
.forEach(s->System.out.println(s));
}
}
2.获取前几个元素(limit)、跳过前几个元素(skip)
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三","李四","张无忌","李若","张开账","荣天真");
list.stream().limit(3)
.forEach(s->System.out.println(s));
}
}
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三","李四","张无忌","李若","张开账","荣天真");
list.stream().skip(3)
.forEach(s->System.out.println(s));
}
}
3.去重(distinct——依赖hashCode和equals方法,若为自定义对象,则要重写该方法)
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三","张三","张三","李若","张开账","荣天真");
list.stream().distinct()
.forEach(s->System.out.println(s));
}
}
4.合并a和b两个流为一个流(concat)
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三","李若","张开账","荣天真");
ArrayList<Integer>list1=new ArrayList<>();
Collections.addAll(list1, 1,2);
Stream.concat(list.stream(), list1.stream()).forEach(s->System.out.println(s));
}
}
Stream流的终结方法
1.统计(count)
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三","李若","张开账","荣天真");
long count=list.stream().count();
System.out.println(count);
}
}
2.收集流中的数据,放到集合中(toArray)
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三","李若","张开账","荣天真");
//第一种方法
Object[] arr1= list.stream().toArray();
System.out.println(Arrays.toString(arr1));
//第二种方法
String [] arr2=list.stream().toArray( value-> new String[value]);
System.out.println(Arrays.toString(arr2));
}
}
3.收集流中的数据,放到集合中(list,set,map)
List:
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三-男-20","李若-女-21","张开账-男-19","荣天真-男-21");
//收集List集合当中
//把所有的男性收集起来
List<String> newlist=list.stream()
.filter(s->"男".equals(s.split("-")[1]))
.collect(Collectors.toList());
System.out.println(newlist);
}
}
Set:
其中还包含set与list之间的区别:
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三-男-20","张三-男-20","李若-女-21","张开账-男-19","荣天真-男-21");
//把所有的男性收集起来
List<String> newlist=list.stream()
.filter(s->"男".equals(s.split("-")[1]))
.collect(Collectors.toList());
System.out.println(newlist);
Set<String> newSet=list.stream()
.filter(s->"男".equals(s.split("-")[1]))
.collect(Collectors.toSet());
System.out.println(newSet);
}
}
3.Map
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "张三-男-20","李若-女-21","张开账-男-19","荣天真-男-21");
//收集Map集合当中
//将姓名作为键,年纪作为值
Map<String, Integer> map= list.stream().collect(Collectors.toMap(
s->s.split("-")[0] //键
,
s->Integer.parseInt(s.split("-")[2]) //值
));
System.out.println(map);
}
}
综合练习
1.数据过滤:定义一个集合,并添加一些整数1,2,3,4,5,6,7,8,9,10过滤奇数,只留下偶数。并将结果保存起来。
public class test {
public static void main(String [] args) {
ArrayList<Integer>list=new ArrayList<>();
Collections.addAll(list, 1,2,3,4,5,6,7,8,9);
List<Integer> l=list.stream().filter(s->s%2==0)
.collect(Collectors.toList());
System.out.print(l);
}
}
2.创建一个ArrayList集合,并添加以下字符串,字符串中前面是姓名,后面是年龄”zhangsan, 23"
“lisi, 24"
"wangwu, 25”
保留年龄大于等于24岁的人,并将结果收集到Map集合中,姓名为键,年龄为值。
public class test {
public static void main(String [] args) {
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list, "zhangsan,23","lisi,24","wangwu,25");
Map<String, Integer> map=list.stream()
.filter(s->(Integer.parseInt(s.split(",")[1])>=24))
.collect(Collectors.toMap(
s->s.split(",")[0]
,
s->Integer.parseInt(s.split(",")[1])
));
System.out.println(map);
}
}
3.现在有两个ArrayList集合,第一个集合中:存储6名男演员的名字和年龄。第二个集合中:存储6名女演员的名字和年龄。姓名和年龄中间用逗号隔开。比如:张三,23
要求完成如下的操作:
1,男演员只要名字为3个字的前两人
2,女演员只要姓杨的,并且不要第一个
3,把过滤后的男演员姓名和女演员姓名合并到一起
4,将上一步的演员信息封装成Actor对象。(类型转换,用map方法)
5,将所有的演员对象都保存到List集合中。
备注:演员类Actor,属性只有一个: name, age
public class test {
public static void main(String [] args) {
ArrayList<String>list1=new ArrayList<>();
Collections.addAll(list1, "张小寒,23","伍仟,24","吴谦,25","王军科,21","张函,25","李现,22");
ArrayList<String>list2=new ArrayList<>();
Collections.addAll(list2, "王晓鸥,21","杨小密,22","赵小刀,25","谢娜,23","杨晨诗,20","吴雨桐,26");
Stream<String>s1=list1.stream()
.filter(s->(s.split(",")[0]).length()==3)
.limit(2);
Stream<String>s2=list2.stream()
.filter(s->s.split(",")[0].startsWith("杨"))
.skip(1);
List<Actor>list=Stream.concat(s1, s2)
.map(s-> new Actor(s.split(",")[0],Integer.parseInt(s.split(",")[1])))
.collect(Collectors.toList());
System.out.println(list);
}
}