Java-容器-存取二维表格信息

文章介绍了如何在Java中使用Map存储数据,以及通过Javabean对象以列表形式组织和打印数据,包括时间格式化的处理。
摘要由CSDN通过智能技术生成
import java.util.*;

public class StoreTable {
    public static void main(String[] args) {
        test02();
    }
//    用Map表示一行数据
    public static void test01(){
        Map<String, Object> m1 = new HashMap();
        m1.put("id",1001);
        m1.put("title","我爱Java");
        m1.put("createTime","2021-10-01");
        m1.put("length",300);

        Map<String, Object> m2 = new HashMap();
        m2.put("id",1002);
        m2.put("title","我爱编程");
        m2.put("createTime","2022-10-01");
        m2.put("length",400);

        Map<String, Object> m3 = new HashMap();
        m3.put("id",1003);
        m3.put("title","我爱代码");
        m3.put("createTime","2023-10-01");
        m3.put("length",500);

        List<Map<String,Object>> list = new ArrayList<>();
        list.add(m1);
        list.add(m2);
        list.add(m3);
//        System.out.println(list);
        for (Map temp:list){
            System.out.println(temp.get("id")+"\t"
                    +temp.get("title")+"\t"
                    +temp.get("createTime")+"\t"
                    +temp.get("length")+"\t");
        }



    }
//    List+Javabean的方式
    public static void test02(){
        VideoInfo v1 = new VideoInfo(1001,"我爱Java","2021-10-01",300);
        VideoInfo v2 = new VideoInfo(1002,"我爱编程","2022-10-01",400);
        VideoInfo v3 = new VideoInfo(1003,"我爱代码","2023-10-01",5000);
        List<VideoInfo> list = new ArrayList<>();
        list.add(v1);
        list.add(v2);
        list.add(v3);
//        System.out.println(list);
        for (VideoInfo temp:list){
            System.out.println(temp);
        }
    }
}
class VideoInfo {
    private int id;
    private String title;
    private String createTime;
    private int length;

    public VideoInfo(int id, String title, String createTime, int length) {
        this.id = id;
        this.title = title;
        this.createTime = createTime;
        this.length = length;
    }

    @Override
    public String toString() {
        return getId() + "\t"
                + getTitle() + '\t'
                + getCreateTime() + '\t'
                + getLength() + '\t';
    }

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getLength() {
//        如果小于60秒,显示XX秒,如果小于3600秒,显示XX分XX秒,如果大于3600秒,显示XX小时XX分XX秒
        if (length<60){
            return this.length+"秒";
        }else if(length<3600){
            return this.length/60+"分"+this.length%60+"秒";
        }else {
            return this.length/3600+"时"+(this.length%3600)/60+"分"+(this.length%3600)%60+"秒";
        }
    }

    public void setLength(int length) {
        this.length = length;
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值