Exercise: Collection element processing

Exercise: Set element processing (traditional method)
problem
. There are two ArrayList sets to store the names of multiple members in the team. The traditional for loop (or enhanced for loop) is required to perform the following steps in sequence:

  1. The first team only needs a 3-character member name; it is stored in a new set.
  2. Only the first 3 people are required after the first team is screened; they are stored in a new set.
  3. The second team only needs the name of the member whose surname is Zhang; it is stored in a new set.
  4. After the second team is screened, don't have the first 2 people; store them in a new set.
  5. Combine two teams into one team; store in a new set.
  6. Create a Person object based on the name; store it in a new collection.
  7. Print the Person object information of the entire team.

Code:

public static void main(String[] args) {
    
    
        //第一支队伍
        ArrayList<String> one = new ArrayList<>();
        one.add("迪丽热巴");
        one.add("宋远桥");
        one.add("苏星河");
        one.add("石破天");
        one.add("石中玉");
        one.add("老子");
        one.add("庄子");
        one.add("洪七公");

        //第二支队伍
        ArrayList<String> two = new ArrayList<>();
        two.add("古力娜扎");
        two.add("张无忌");
        two.add("赵丽颖");
        two.add("张三丰");
        two.add("尼古拉斯赵四");
        two.add("张天爱");
        two.add("张二狗");

        //第一个队伍只要名字为3个字的成员姓名
        List<String> one1 = new ArrayList<>();
        for (String name : one) {
    
    
            if (name.length()==3){
    
    
                one1.add(name);
            }
        }

        //第一个队伍筛选之后只要前3个人
        List<String> one2 = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
    
    
            one2.add(one1.get(i));
        }

         //第二个队伍只要姓张的成员姓名
        List<String> two1 = new ArrayList<>();
        for (String name : two) {
    
    
            if (name.startsWith("张")){
    
    
                two1.add(name);
            }
        }

        //第二个队伍筛选之后不要前2个人
        List<String> two2 = new ArrayList<>();
        for (int i = 1; i < two1.size(); i++) {
    
    
            two2.add(two1.get(i));
        }

        //将两个队伍合并为一个队伍;存储到一个新集合中
        List<String> all = new ArrayList<>();
        all.addAll(one2);
        all.addAll(two2);

        //根据姓名创建 Person 对象;存储到一个新集合中
        List<Person> person = new ArrayList<>();
        for (String name : all) {
    
    
            person.add(new Person(name));
        }

        //打印整个队伍的Person对象信息。
        for (Person person1 : person) {
    
    
            System.out.println(person1);
        }
    }
}

The code for the Person class is:

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

Program demonstration:
Insert picture description here

Stream stream optimization:

Code:

public class Demo02StreamTest {
    
    
    public static void main(String[] args) {
    
    
        //第一支队伍
        ArrayList<String> one = new ArrayList<>();
        one.add("迪丽热巴");
        one.add("宋远桥");
        one.add("苏星河");
        one.add("石破天");
        one.add("石中玉");
        one.add("老子");
        one.add("庄子");
        one.add("洪七公");

        //第二支队伍
        ArrayList<String> two = new ArrayList<>();
        two.add("古力娜扎");
        two.add("张无忌");
        two.add("赵丽颖");
        two.add("张三丰");
        two.add("尼古拉斯赵四");
        two.add("张天爱");
        two.add("张二狗");

        //第一个队伍只要名字为3个字的成员姓名
        //第一个队伍筛选之后只要前3人
        Stream<String> stream1 = one.stream().filter(s -> s.length() == 3).limit(3);

        //第二个队伍只要姓张的成员姓名
        //第二个队伍筛选之后不要前2个人
        Stream<String> stream2 = two.stream().filter(s -> s.startsWith("张")).skip(2);

        //将两个队伍合并为一个队伍
        //根据姓名创建Person对象
        //打印整个队伍的Person对象信息
        Stream.concat(stream1,stream2).map(name->new Person(name)).forEach(person-> System.out.println(person));
    }
}

Program demonstration:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/109156917