今天学习的是

创建用户对象,属性有年级,姓名,年龄,分数,生日

键盘录入五个学生信息格式如下 :一年级,张三,20岁,60分,2020-10-10

按年级保存用户对象到list1集合中

按照如下格式保存数据:一年级|张三|20|60|2020-10-10数据保存在user.txt文件中

读取user.txt文件中的数据,存Map集合,键是String编号   值是学生对象

第一个编号是A001  第二个编号是A002.......遍历输出map集合。

然后获得所有年级的学生名字,存list2集合  并能够打乱顺序,按照如下格式输出:(张三)(李四)。如果遇到姓名是小黑的学生就停止输出。

遍历list1集合使用Consumer格式化输出人名和成绩(格式:姓名--成绩),使用Supplier输出每个年级最高的分数

创建两个线程  一个是一年级     一个是二年级   遍历

实体类

package Day08;

import Day07.Str;

import java.util.List;

public class Student  {
    private String nj;
    private String name;
    private int age;
    private int score;
    private String date;

    public Student() {
    }

    public Student(String nj, String name, int age, int score, String date) {
        this.nj = nj;
        this.name = name;
        this.age = age;
        this.score = score;
        this.date = date;
    }

    public String getNj() {
        return nj;
    }

    public void setNj(String nj) {
        this.nj = nj;
    }

    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 int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Student{" +
                "nj='" + nj + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                ", date='" + date + '\'' +
                '}';
    }
}

测试类

package Day08;

import java.io.*;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class Test01 {
    public static void main(String[] args) throws IOException {
        Scanner sc=new Scanner(System.in);//创建Scanner
        List list1=new ArrayList<>();//创建集合
        for (int i = 1; i <= 2; i++) {//循环2遍
            System.out.println("请输入"+i+"信息");//提示语句
            String nj=sc.next();//键盘录入
            String[] split = nj.split(",");//分割,
            Student aa=new Student(split[0],split[1],Integer.parseInt(split[2]),Integer.parseInt(split[3]),split[4]);//存储在实体类
            list1.add(aa);//存储到list1里面
        }

        BufferedWriter bw=new BufferedWriter(new FileWriter(new File("user.txt")));//字符输出流
        for (Student student : list1) {
            bw.write(student.getNj()+"|"+student.getName()+"|"+student.getAge()+"|"+student.getScore()+"|"+student.getDate());
            bw.newLine();//换行
        }
        bw.close();//关流
        BufferedReader br=new BufferedReader(new FileReader(new File("user.txt")));//字符输入流
        String a;
        Map map=new HashMap<>();//定义一个map集合
        int num=0;//定义变量
        while ((a=br.readLine())!=null){
            String[] split1 = a.split("[|]");//分割user.txt里的文件使用|
            Student aa=new Student(split1[0],split1[1],Integer.parseInt(split1[2]),Integer.parseInt(split1[3]),split1[4]);
            num++;
            map.put("A00"+num,aa);//存储
        }
        br.close();//关流
        for (String s : map.keySet()) {
            System.out.println(s+" "+map.get(s));
        }//输出
      List list2=new ArrayList<>();//创建lisi2
        for (Student student : list1) {//存储list1里面的姓名
            list2.add(student.getName());
        }
        Collections.shuffle(list2);//打乱数据
        for (String s : list2) {//list2里面都是姓名
            if (s.equals("小黑")){//是小黑
                break;//跳出循环
            }
            System.out.println("("+s+")");//给姓名价格括号
        }
        Consumer consumer=new Consumer() {//使用list1集合Consumer
            @Override
            public void accept(Student student) {
                System.out.println(student.getName()+"--"+student.getScore());//输出姓名-成绩
            }
        };
        for (Student student : list1) {//循环
            consumer.accept(student);//lm有几个就输出几个
        }
        Supplier aa= () -> {//使用Supplier 输出年级最高的分数
           int max=list1.get(0).getScore();
            for (Student student : list1) {
                if (student.getScore()>=max){
                    max=student.getScore();
                }
            }
            return max;
        };
        Integer integer = aa.get();//提取出来最大值
        System.out.println(integer);//输出

        t1 name=new t1(list1);//创建线程
        t2 name1=new t2(list1);

        Thread n=new Thread(name,"自定义线程1");//创建子线程
        Thread n1=new Thread(name1,"自定义线程2");
        n.start();//开启线程
        n1.start();
    }
}
package Day08;

import java.util.List;

public class t1 implements Runnable {
    private List list;

    public t1(List list) {
        this.list = list;
    }

    @Override
    public void run() {
        for (Student student : list) {
            if (student.getName().equals("1")){
                System.out.println(Thread.currentThread().getName()+student);
            }
        }
    }
}
package Day08;

import java.util.List;

public class t2 implements Runnable {
    private List list;

    public t2(List list) {
        this.list = list;
    }

    @Override
    public void run() {
        for (Student student : list) {
            if (student.getName().equals("2")){
                System.out.println(Thread.currentThread().getName()+student);
            }
        }
    }
}

你可能感兴趣的:(java,开发语言)