温故知新 集合

/*
* 对集合归纳 (主要对Vector List ,Map回顾)-----------目前只写项目中常见的情况(由硬件半路入门java才疏学浅,知识体系不完善)
*/

//集合的核心点是遍历(添加/获取)

对于List集合掺杂泛型,(项目中常用的方法)最好的方法是---------加强For循环

假设ArrayList里已经封装了很多数据:要遍历一以获取 

 List<Entity> list=new ArrayList<Entity>();

for(Entity entity:list){

System.out.println("获得数据为"+entity);

}

对于Map集合掺杂泛型: Map数据遍历方式(项目中常用的方法)

  Map<String,Object> map = new HashMap<String, Object>();

//第一种 System.out.println("通过map.keySet遍历key和value"); for(String key :map.keySet()){ System.out.println("key= "+key+" value= "+map.get(key)); } } //第二者:推荐尤其是容量大的时候 for(Map.Entry<String, Object> entry: map.entrySet()){ System.out.println("key= "+entry.getKey()+" value= "+entry.getValue()); } //第三种:使用map.values()遍历所有的value但不能遍历key for(String v:map.values()){ System.out.println("value= "+v); } }

  

项目中:

class Demo{
private int id;
private String name;
private Date birthday;
private String agender;

/**
* @param id
* @param name
* @param birthday
* @param agender
*/
public Demo(int id, String name, Date birthday, String agender) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.agender = agender;
}
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAgender() {
return agender;
}
public void setAgender(String agender) {
this.agender = agender;
}

 

public class test{

public void main(String[] args){

//密码常用 ----设置初始密码选用集合
Vector<String> v = new Vector<String>(1);
v.add(0, "123");
v.add(1,"234");
v.add("345");
System.out.println(v);

Vector<String> ve =new Vector<String>();
ve.add("1234");
ve.add("2345");
ve.add("3456");
System.out.println(ve);

List<String> list=new ArrayList();
list.add(0, "13");
list.add(1,"14");
System.out.println(list);
Map<String,Object> map=new HashMap();
map.put("1", "15");
map.put("2","16");
System.out.println(map);

List<Map<String, Demo>> listDemo=new ArrayList<Map<String,Demo>>();
Map<String,Demo> mapDemo=new HashMap<String,Demo>();
mapDemo.put("0001",new Demo(1,"张三",new Date(),"男"));
mapDemo.put("0002",new Demo(2,"李四",new Date(),"女"));
Map<String,Demo> mapDemo01=new HashMap<String,Demo>();
mapDemo01.put("0003",new Demo(3,"王五",new Date(),"男"));
mapDemo01.put("0004",new Demo(4,"赵六",new Date(),"女"));
listDemo.add(mapDemo);
listDemo.add(mapDemo01);
System.out.println(listDemo);

}

}

posted @ 2020-03-24 14:30  唯恐不及  阅读(158)  评论(0编辑  收藏  举报