s-day07- Stream综合案例
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.stream07;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
String name;
|
||||||
|
|
||||||
|
public Person(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Person{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package com.inmind.stream07;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
现在有两个ArrayList集合存储队伍当中的多个成员姓名,要求使用传统的for循环(或增强for循环)依次进行以下若干操作步骤:
|
||||||
|
|
||||||
|
1. 第一个队伍只要名字为3个字的成员姓名;
|
||||||
|
2. 第一个队伍筛选之后只要前3个人;
|
||||||
|
3. 第二个队伍只要姓张的成员姓名;
|
||||||
|
4. 第二个队伍筛选之后不要前2个人;
|
||||||
|
5. 将两个队伍合并为一个队伍;
|
||||||
|
6.根据姓名创建Person对象
|
||||||
|
7. 打印整个队伍的Person信息。
|
||||||
|
*/
|
||||||
|
public class Test21 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<String> one = new ArrayList<>();
|
||||||
|
one.add("迪丽热巴");
|
||||||
|
one.add("宋远桥");
|
||||||
|
one.add("苏星河");
|
||||||
|
one.add("老子");
|
||||||
|
one.add("庄子");
|
||||||
|
one.add("孙子");
|
||||||
|
one.add("洪七公");
|
||||||
|
one.add("洪八公");
|
||||||
|
|
||||||
|
List<String> two = new ArrayList<>();
|
||||||
|
two.add("古力娜扎");
|
||||||
|
two.add("张无忌");
|
||||||
|
two.add("张三丰");
|
||||||
|
two.add("赵丽颖");
|
||||||
|
two.add("张二狗");
|
||||||
|
two.add("张天爱");
|
||||||
|
two.add("张三");
|
||||||
|
|
||||||
|
/*//1. 第一个队伍只要名字为3个字的成员姓名;
|
||||||
|
ArrayList<String> newOne = new ArrayList<>();
|
||||||
|
for (String s : one) {
|
||||||
|
if (s.length() == 3) {
|
||||||
|
newOne.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//2. 第一个队伍筛选之后只要前3个人;
|
||||||
|
ArrayList<String> newOne1 = new ArrayList<>();
|
||||||
|
for (int i = 0; i < newOne.size(); i++) {
|
||||||
|
if (i < 3) {
|
||||||
|
newOne1.add(newOne.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(newOne1);
|
||||||
|
|
||||||
|
//3. 第二个队伍只要姓张的成员姓名;
|
||||||
|
ArrayList<String> newTwo = new ArrayList<>();
|
||||||
|
for (String s : two) {
|
||||||
|
if (s.startsWith("张")) {
|
||||||
|
newTwo.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//4. 第二个队伍筛选之后不要前2个人;
|
||||||
|
ArrayList<String> newTwo1 = new ArrayList<>();
|
||||||
|
for (int i = 0; i < newTwo.size(); i++) {
|
||||||
|
if (i > 1) {
|
||||||
|
newTwo1.add(newTwo.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(newTwo1);
|
||||||
|
//5. 将两个队伍合并为一个队伍;
|
||||||
|
newOne1.addAll(newTwo1);
|
||||||
|
System.out.println(newOne1);
|
||||||
|
//6.根据姓名创建Person对象
|
||||||
|
ArrayList<Person> personList = new ArrayList<>();
|
||||||
|
for (String s : newOne1) {
|
||||||
|
personList.add(new Person(s));
|
||||||
|
}
|
||||||
|
System.out.println(personList);
|
||||||
|
|
||||||
|
//7. 打印整个队伍的Person信息。
|
||||||
|
for (Person p : personList) {
|
||||||
|
System.out.println(p);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
System.out.println("--------------------------Stream流------------------------");
|
||||||
|
/*//1. 第一个队伍只要名字为3个字的成员姓名;
|
||||||
|
Stream<String> stream1 = one.stream().filter(s -> s.length() == 3);
|
||||||
|
//2. 第一个队伍筛选之后只要前3个人;
|
||||||
|
Stream<String> stream2 = stream1.limit(3);
|
||||||
|
//3. 第二个队伍只要姓张的成员姓名;
|
||||||
|
Stream<String> stream3 = two.stream().filter(s -> s.startsWith("张"));
|
||||||
|
//4. 第二个队伍筛选之后不要前2个人;
|
||||||
|
Stream<String> stream4 = stream3.skip(2);
|
||||||
|
//5. 将两个队伍合并为一个队伍;
|
||||||
|
Stream<String> newStream = Stream.concat(stream2, stream4);
|
||||||
|
//6.根据姓名创建Person对象
|
||||||
|
Stream<Person> personStream = newStream.map(s -> new Person(s));
|
||||||
|
//7. 打印整个队伍的Person信息。
|
||||||
|
personStream.forEach(p-> System.out.println(p));*/
|
||||||
|
|
||||||
|
System.out.println("--------------------------一句话写完------------------------");
|
||||||
|
Stream.concat(
|
||||||
|
one.stream().filter(s -> s.length() == 3).limit(3),
|
||||||
|
two.stream().filter(s -> s.startsWith("张")).skip(2)
|
||||||
|
).map(s -> new Person(s)).forEach(p-> System.out.println(p));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user