美文网首页
java 动态表格列

java 动态表格列

作者: aaagu1234 | 来源:发表于2021-07-20 16:50 被阅读0次
     package com.company;
    
    import java.util.*;
    
    class User{
        private String channelName;
        private String channelId;
        private String passenger;
        private String startDate;
        private String endDate;
    
        public String getPassenger() {
            return passenger;
        }
    
        public void setPassenger(String passenger) {
            this.passenger = passenger;
        }
    
        public String getChannelName() {
            return channelName;
        }
    
        public void setChannelName(String channelName) {
            this.channelName = channelName;
        }
    
        public String getStartDate() {
            return startDate;
        }
    
        public String getEndDate() {
            return endDate;
        }
    
        public void setEndDate(String endDate) {
            this.endDate = endDate;
        }
    
        public void setStartDate(String startDate) {
            this.startDate = startDate;
        }
    
        public String getChannelId() {
            return channelId;
        }
    
        public void setChannelId(String channelId) {
            this.channelId = channelId;
        }
    }
    public class Main {
    
        public static void main(String[] args) {
            // write your code here
            User u1 = new User();
            u1.setChannelId("1001");
            u1.setChannelName("通道1");
            u1.setPassenger("0.2");
            u1.setStartDate("7:30");
            u1.setEndDate("8:00");
            User u2 = new User();
            u2.setChannelId("1002");
            u2.setChannelName("通道2");
            u2.setPassenger("0.4");
            u2.setStartDate("7:30");
            u2.setEndDate("8:00");
            User u3 = new User();
            u3.setChannelId("1001");
            u3.setChannelName("通道1");
            u3.setPassenger("0.3");
            u3.setStartDate("8:00");
            u3.setEndDate("8:30");
            User u4 = new User();
            u4.setChannelId("1002");
            u4.setChannelName("通道2");
            u4.setPassenger("0.5");
            u4.setStartDate("8:00");
            u4.setEndDate("8:30");
            List<User> list = new ArrayList<User>();
            list.add(u1);
            list.add(u2);
            list.add(u3);
            list.add(u4);
            Map<String, List<User>> map = new HashMap<String, List<User>>();
    
            Iterator it = list.iterator();
            // 按照开始时间分组
            for(int i=0;i<list.size();i++){
                User u = (User) list.get(i);
                if (map.get(u.getStartDate()) != null) {
                    List<User> list2 =  map.get(u.getStartDate());
                    list2.add(u);
                } else {
                    List<User> list3 = new ArrayList<User>();
                    map.put(u.getStartDate(), list3);
                    list3.add(u);
                }
            }
            for (String key : map.keySet()) {
                System.out.println(key);
                System.out.println(map.get(key));
    
            }
            // 动态列key和value的生成
            List<Map<String, String>> allList = new ArrayList<Map<String, String>>();
            for (String key : map.keySet()) {
                List<User> list4 = (List) map.get(key);
                Map<String, String> m = new HashMap<String, String>();
                for(int i = 0; i < list4.size(); i++){
                    if(i == 0){
                        User us = list4.get(i);
                        m.put("startDate", us.getStartDate());
                        m.put("endDate", us.getEndDate());
                    }
                    User u = list4.get(i);
                    m.put(u.getChannelId(), u.getPassenger());
                }
                allList.add(m);
            }
    
            for(int i=0;i<allList.size();i++){
                System.out.println(allList.get(i));
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:java 动态表格列

          本文链接:https://www.haomeiwen.com/subject/cievbktx.html