題目
現在有兩個 ArrayList 集合存儲隊伍當中的多個成員姓名,要求使用傳統的for循環(或增強for循環)依次進行以下若干操作步驟:
- 第一個隊伍只要名字為3個字的成員姓名;存儲到一個新集合中。
- 第一個隊伍篩選之后只要前3個人;存儲到一個新集合中。
- 第二個隊伍只要姓張的成員姓名;存儲到一個新集合中。
- 第二個隊伍篩選之后不要前2個人;存儲到一個新集合中。
- 將兩個隊伍合並為一個隊伍;存儲到一個新集合中。
- 根據姓名創建 Person 對象;存儲到一個新集合中。
- 打印整個隊伍的Person對象信息。
兩個隊伍(集合)的代碼如下:
import java.util.ArrayList;
import java.util.Collections;
public class DemoArrayListNames {
public static void main(String[] args) {
//第一支隊伍
ArrayList<String> one = new ArrayList<>();
Collections.addAll(one, "迪麗熱巴", "宋遠橋", "蘇星河", "石破天", "石中玉", "老子", "庄子", "洪七公");
//第二支隊伍
ArrayList<String> two = new ArrayList<>();
Collections.addAll(two, "古力娜扎", "張無忌", "趙麗穎", "張三豐", "尼古拉斯趙四", "張天愛", "張二狗");
}
}
Person 類的代碼為:
public class Person {
private String name;
public Person() { }
public Person(String name) { this.name = name; }
@Override
public String toString() { return "Person{" + "name='" + name + '\'' + '}'; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
傳統方式解答
既然使用傳統的for循環寫法,那么:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DemoArrayListNamesTradition {
public static void main(String[] args) {
//第一支隊伍
ArrayList<String> one = new ArrayList<>();
Collections.addAll(one, "迪麗熱巴", "宋遠橋", "蘇星河", "石破天", "石中玉", "老子", "庄子", "洪七公");
//第二支隊伍
ArrayList<String> two = new ArrayList<>();
Collections.addAll(two, "古力娜扎", "張無忌", "趙麗穎", "張三豐", "尼古拉斯趙四", "張天愛", "張二狗");
// 第一個隊伍只要名字為3個字的成員姓名;
List<String> oneA = new ArrayList<>();
for (String name : one) {
if (name.length() == 3) {
oneA.add(name);
}
}
// 第一個隊伍篩選之后只要前3個人;
List<String> oneB = new ArrayList<>();
for (int i = 0; i < 3; i++) {
oneB.add(oneA.get(i));
}
// 第二個隊伍只要姓張的成員姓名;
List<String> twoA = new ArrayList<>();
for (String name : two) {
if (name.startsWith("張")) {
twoA.add(name);
}
}
// 第二個隊伍篩選之后不要前2個人;
List<String> twoB = new ArrayList<>();
for (int i = 2; i < twoA.size(); i++) {
twoB.add(twoA.get(i));
}
// 將兩個隊伍合並為一個隊伍;
List<String> totalNames = new ArrayList<>();
totalNames.addAll(oneB);
totalNames.addAll(twoB);
// 根據姓名創建Person對象;
List<Person> totalPersonList = new ArrayList<>();
for (String name : totalNames) {
totalPersonList.add(new Person(name));
}
// 打印整個隊伍的Person對象信息。
for (Person person : totalPersonList) {
System.out.println(person);
}
}
}
運行程序,控制台輸出:
Person{name='宋遠橋'}
Person{name='蘇星河'}
Person{name='石破天'}
Person{name='張天愛'}
Person{name='張二狗'}
Stream方式解答
import java.util.ArrayList;
import java.util.Collections;
import java.util.stream.Stream;
public class DemoArrayListNamesStream {
public static void main(String[] args) {
//第一支隊伍
ArrayList<String> one = new ArrayList<>();
Collections.addAll(one, "迪麗熱巴", "宋遠橋", "蘇星河", "石破天", "石中玉", "老子", "庄子", "洪七公");
//第二支隊伍
ArrayList<String> two = new ArrayList<>();
Collections.addAll(two, "古力娜扎", "張無忌", "趙麗穎", "張三豐", "尼古拉斯趙四", "張天愛", "張二狗");
// 第一個隊伍只要名字為3個字的成員姓名;
Stream<String> streamOneA = one.stream().filter(name -> name.length() == 3);
// 第一個隊伍篩選之后只要前3個人;
Stream<String> streamOneB = streamOneA.limit(3);
// 第二個隊伍只要姓張的成員姓名;
Stream<String> streamTwoA = two.stream().filter(name -> name.startsWith("張"));
// 第二個隊伍篩選之后不要前2個人;
Stream<String> streamTwoB = streamTwoA.skip(2);
// 將兩個隊伍合並為一個隊伍;
Stream<String> totalNames = Stream.concat(streamOneB, streamTwoB);
// 根據姓名創建Person對象;
Stream<Person> personStream = totalNames.map(personName -> new Person(personName));
// 打印整個隊伍的Person對象信息。
personStream.forEach(System.out::println);
}
}
運行程序,控制台輸出:
Person{name='宋遠橋'}
Person{name='蘇星河'}
Person{name='石破天'}
Person{name='張天愛'}
Person{name='張二狗'}
該程序中,使用的Stream接口中的方法:java.util.stream.Stream 接口中的常用方法。