某班级要组织秘游活动,有四个景点的数据可以选择,依次是:“东湖”“黄鹤楼”“木兰文化区”“归元弹寺”,每名学生最多可以选择两个想去的景点,最少要选择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>集合,统计里面每个景点选择的次数,并输出景点名称和选择的次数。
业务三:
请用程序计算出哪个景点想去的人数最多,以及哪些人没有选择这个最多人想去的景点。
//① 多个用户用#拼接 可以用split();
//② String 转换为Long 用:Long.parseLong()
//③String 转换为LocalDateTime LocalDateTime.spare() DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");一个工具要转换为这种格式
//④list.add(new Student(...))
键值对用Map集合,new HashMap(),记得"-"要分割 ,是否包含用的是map.containsKey(..)
//map集合中有记录 map.put(address.get(address)+1)
//map集合没有记录map.put(address.1)
用的 map.entrySet().stream().max((o1,o2)->(o1.getValue()-o2.getValue())).get().getKey()
用的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 + ''' + '}'+'‘;讯享网}}


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/158970.html