码迷,mamicode.com
首页 > 其他好文 > 详细

第八篇 集合与容器(八)

时间:2018-08-25 16:28:51      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:data   ble   pack   数据   set   for   demo   javabean   his   

表格数据的存储

package com.zzp.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 
 * 测试表格数据的存储
 * @author java
 *
 */
public class TestStoreData {
    public static void main(String[] args) {
        Map<String,Object> map1 = new HashMap<>();
        map1.put("id", 1001);
        map1.put("姓名", "张三");
        map1.put("收入", 20000);
        
        Map<String,Object> map2 = new HashMap<>();
        map2.put("id", 1002);
        map2.put("姓名", "李四");
        map2.put("收入", 2000);
        
        Map<String,Object> map3 = new HashMap<>();
        map3.put("id", 1003);
        map3.put("姓名", "王五");
        map3.put("收入", 20000);
        
        List<Map<String,Object>> list = new ArrayList<>();
        list.add(map1);
        list.add(map3);
        list.add(map2);
        
        for(Map<String,Object> ma : list){
            Set<String> keySet = ma.keySet();
            for(String key : keySet){
                System.out.print(ma.get(key)+"\t");
            }
            System.out.println();
        }
    }
}

 使用javabean存储数据

package com.zzp.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 
 * 测试表格数据的存储
 * 每一行使用Javabean对象进行存储,多行放在map或list中
 * @author java
 *
 */
public class TestStoreData02 {
    public static void main(String[] args) {
        User user1 = new User(001,"张三",2000);
        User user2 = new User(002,"李四",10000);
        User user3 = new User(003,"王五",12000);
        
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        
        for(User ls : list){
            System.out.println(ls.getId()+","+ls.getName()+","+ls.getSalary()+"\t");
        }
        
        Map<Integer,User> map = new HashMap<>();
        map.put(1001, user1);
        map.put(1002, user2);
        map.put(1003, user3);
        
        Set<Integer> keySet = map.keySet();
        for(Integer key : keySet){
            System.out.println(key +"---"+map.get(key).getId()+"--"+
                        map.get(key).getName()+"--"+map.get(key).getSalary());
        }
    }
}


class User{
    private int id;
    private String name;
    private double salary;
    
    public User() {
        super();
    }
    
    public User(int id, String name, double salary) {
        super();
        this.id = id;
        this.name = name;
        this.salary = salary;
    }



    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
}

 

第八篇 集合与容器(八)

标签:data   ble   pack   数据   set   for   demo   javabean   his   

原文地址:https://www.cnblogs.com/zhangzhipeng001/p/9534314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!