字符串转换到List对象

题目描述:

某班级要组织秘游活动,有四个景点的数据可以选择,依次是:“东湖”“黄鹤楼”“木兰文化区”“归元弹寺”,每名学生最多可以选择两个想去的景点,最少要选择1个想去的景点,现在系统收集到了班级多名学生选择的信息如下。
String info ="10001, 张无忌, 男, 2023-07-22 11:11:12, 东湖-黄鹤楼#10002, 赵敏,女, 2023-07-22 09:11:21, 黄鹤楼-归元禅寺#10003.周芷若,女,2023-07-22 04:11:21.木兰文化区-东湖#10004,小昭,女,2023-07-22 08:11:21,东湖#10005,灭绝,女,2023-
07-22 17:11:21,归元禅寺”;
新建测试类,在类中书写main方法,在方法中完成如下业务逻辑:
业务一:
”需要你解析上面的字符串,获取里面的用户数据,并封装到Sutdent对象中
■ 多个Student对象在添加到List<Student> 集合中
注意:
■字符串中的规则如下,多个用户用#拼接,用户的信息之间用,拼接,多个景点是用-拼接的。
其中用户的id和选择时间是需要进行类型转换的,其中id需要将String转成Long,选择时间需要将String转成LocalDateTime。
**业务二:**
遍历上面获取的List<Student>集合,统计里面每个景点选择的次数,并输出景点名称和选择的次数。
业务三:
请用程序计算出哪个景点想去的人数最多,以及哪些人没有选择这个最多人想去的景点。

分析:

//1.先定义student对象,并创建一个List<Student>集合(包装对象)

//2. 多个Student对象在添加到List<Student> 集合中

     //① 多个用户用#拼接 可以用split();

     //② String 转换为Long  用:Long.parseLong()

    //③String 转换为LocalDateTime  LocalDateTime.spare()   DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");一个工具要转换为这种格式

    //④list.add(new Student(...))

//3.统计里面每个景点选择的次数,并输出景点名称和选择的次数

键值对用Map集合,new HashMap(),记得"-"要分割  ,是否包含用的是map.containsKey(..)

    //map集合中有记录 map.put(address.get(address)+1)

    //map集合没有记录map.put(address.1)

//4.哪个景点想去的人数最多

用的 map.entrySet().stream().max((o1,o2)->(o1.getValue()-o2.getValue())).get().getKey()

//5.哪些人没有选择这个最多人想去的景点

用的if(!s.getAddress().contains(maxSelectAddress))

代码:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class 分割字符串 {
    public static void main(String[] args) {
        String info = "10001,张无忌,男,2023-07-22 11:11:12,东湖-黄鹤楼#10002,赵敏,女,2023-07-22 09:11:21,黄鹤楼-归元禅寺#10003,周芷若,女,2023-07-22 04:11:21,木兰文化区-东湖#10004,小昭,女,2023-07-22 08:11:21,东湖#10005,灭绝,女,2023-07-22 17:11:21,归元禅寺";
        List<Student> student=new ArrayList<>();
        student=getStudentList(info);
        Map<String,Integer> map=parseSelectAddressCount(student);

        //景点人去的最多,谁没有选择这个景点
        String maxSelectAddress=getMaxSelectAddress(map);
        System.out.println("最多选择景点的名称:"+maxSelectAddress);
        //打印那些人没有去国选择最多的景点
        printNoSelectAddress(student,maxSelectAddress);

    }

    private static void printNoSelectAddress(List<Student> student, String maxSelectAddress) {
        for(Student s:student){
            if(!s.getAddress().contains(maxSelectAddress))
                System.out.println(s.getName());
        }
    }

    private static String getMaxSelectAddress(Map<String, Integer> map) {//entrySet()获得键和值
        return map.entrySet().stream().max((o1,o2)->(o1.getValue()-o2.getValue())).get().getKey();

    }
//遍历上面获取的List<Student>集合,统计里面每个景点选择的次数,并输出景点名称和选择的次数。
    private static Map<String, Integer> parseSelectAddressCount(List<Student> student) {
        Map<String, Integer> map=new HashMap<>();
        for(Student s:student) {
            String [] selectAddress=s.getAddress().split("-");
            for(String select:selectAddress){
                if(map.containsKey(select)){
                    map.put(select,map.get(select)+1);
                }else{
                    map.put(select,1);
                }
            }
        }
        //景点名称和选择的次数
     for(Map.Entry<String,Integer> entry:map.entrySet()){
        System.out.println("景点的名称:"+entry.getKey()+"景点选择的次数: "+entry.getValue());
    }
     return map;
    }


    // 多个Student对象在添加到List<Student> 集合中
    public static List<Student> getStudentList(String info){
        List<Student> list = new ArrayList<>();
        String [] infoArray = info.split("#");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        for (String str : infoArray) {
            String [] strArray = str.split(",");
            LocalDateTime time=LocalDateTime.parse(strArray[3],df);//parse()方法进行转换
            Student student = new Student(Long.parseLong(strArray[0]),strArray[1],strArray[2],time,strArray[4]);
            list.add(student);
        }
       return list;
    }
}
import java.time.LocalDateTime;

public class Student {
    private Long id;
    private String name;
    private String sex;
    private LocalDateTime localTime;
    private String address;

    public Student(Long id, String name, String sex, LocalDateTime localTime, String address) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.localTime = localTime;
        this.address = address;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public LocalDateTime getLocalTime() {
        return localTime;
    }

    public void setLocalTime(LocalDateTime localTime) {
        this.localTime = localTime;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", localTime=" + localTime +
                ", address='" + address + '\'' +
                '}'+'\n';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就七天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值