第八篇 集合与容器
Posted zhangzhipeng001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第八篇 集合与容器相关的知识,希望对你有一定的参考价值。
表格数据的存储
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)+" "); } 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()+" "); } 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; } }
以上是关于第八篇 集合与容器的主要内容,如果未能解决你的问题,请参考以下文章