黑马程序员技术交流社区

标题: Collection总结 [打印本页]

作者: lgl48128244    时间: 2014-6-29 17:59
标题: Collection总结
1  1  3  6

一张图:一个类:Collections。三个知识点:foreach、generic(泛型)、Auto-boxing\unboxing(自动拆装箱)

六个接口:Collection、Set、List、Map、Iterator、Comparable



学习代码:

package com.pb.jbit.Collection;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//定义一个雇员类

public class Employee {// javaBean实体类

private int id;
private String name;
private double salary;
private String department;
private Date date;

public Employee(int id, String name, double salary, String department,
   String date) {
  super();
  this.id = id;
  this.name = name;
  this.salary = salary;
  this.department = department;
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
  try {
   this.date = format.parse(date);
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
}

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;
}

public String getDepartment() {
  return department;
}

public void setDepartment(String department) {
  this.department = department;
}

public Date getDate() {
  return date;
}

public void setDate(Date date) {
  this.date = date;
}

}

测试一:利用对象来储存数据

package com.pb.jbit.Collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 一个对象对应一条记录
* @author 李国梁
*
*/
public class test01 {
public static void main(String[] args) {
  Employee e = new Employee(1001, "高淇", 3000, "项目部", "2007-09");
  Employee e1 = new Employee(1001, "马士兵", 3000, "教学部", "2006-07");
  Employee e2 = new Employee(1001, "斐新", 3000, "开发部", "2008-03");
  List<Employee> list = new ArrayList<Employee>();
  list.add(e);
  list.add(e1);
  list.add(e2);
  // 方法一:
  Iterator<Employee> iterator = list.iterator();
  while (iterator.hasNext()) {
   Employee employee = (Employee) iterator.next();
   System.out.println(employee.getName() + "---" + employee.getDate());
  }
  System.out.println();
  // 方法二:
  for (int i = 0; i < list.size(); i++) {
   System.out.println(list.get(i).getName() + "---"
     + list.get(i).getDate());
  }
  System.out.println();
  // 方法三:
  for (Employee employee : list) {
   System.out.println(employee.getName() + "---" + employee.getDate());
  }
}
}

测试二:利用Map集合来储存数据

package com.pb.jbit.Collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* 利用Map容器封装数据, 一个Map对应一条记录
*
* @author 李国梁
*
*/
public class test02 {
public static void main(String[] args) {
  Map<Object, Object> map = new HashMap<Object, Object>();
  map.put("id", 1001);
  map.put("name", "高淇");
  map.put("salary", 3500);
  map.put("department", "项目部");
  map.put("date", "2007-09");
  Map<Object, Object> map1 = new HashMap<Object, Object>();
  map1.put("id", 1002);
  map1.put("name", "马士兵");
  map1.put("salary", 3550);
  map1.put("department", "教学部");
  map1.put("date", "2006-09");
  Map<Object, Object> map2 = new HashMap<Object, Object>();
  map2.put("id", 1003);
  map2.put("name", "斐新");
  map2.put("salary", 3555);
  map2.put("department", "开发部");
  map2.put("date", "2003-09");
  List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
  list.add(map);
  list.add(map1);
  list.add(map2);

  showslef(list);
}
public static void showslef(List<Map<Object, Object>> list) {
  // 方法一:
  for (int i = 0; i < list.size(); i++) {
   System.out.println(list.get(i).get("name") + "---"
     + list.get(i).get("date"));
  }
  System.out.println();
  // 方法二:
  for (Map<Object, Object> map : list) {
   System.out.print(map.get("name"));
   System.out.print("---");
   System.out.println(map.get("date"));
  }
  System.out.println();
  // 方法三:
  Iterator<Map<Object, Object>> iterator = list.iterator();
  while (iterator.hasNext()) {
   Map<java.lang.Object, java.lang.Object> map = (Map<java.lang.Object, java.lang.Object>) iterator
     .next();
   System.out.print(map.get("name"));
   System.out.print("---");
   System.out.println(map.get("date"));
  }
  System.out.println();
  // 方法四:
  for (Iterator<Map<Object, Object>> iterator1 = list.iterator(); iterator1
    .hasNext();) {
   Map<java.lang.Object, java.lang.Object> map = (Map<java.lang.Object, java.lang.Object>) iterator1
     .next();
   System.out.print(map.get("name"));
   System.out.print("---");
   System.out.println(map.get("date"));
  }
}
}








psb.jpg (114.53 KB, 下载次数: 13)

Collection

Collection

作者: 静丶    时间: 2014-6-29 19:01
学习一下~
作者: ╭月牙湾湾╮    时间: 2014-6-30 00:48
学习了      
作者: lvc    时间: 2014-6-30 06:18
自己弄的,分享给大家。不足的地方请指教。

java集合关系图.rar

9.75 KB, 下载次数: 122


作者: 会飞De石头    时间: 2014-6-30 09:14
代码应该价加注释,要不不懂的还是不懂
作者: alexsoon    时间: 2014-6-30 17:14
辛苦了~- -就是代码看着有点不太舒服




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2