第110集:容器SetHashSet基本用法源码分析
HashSet是最常用的set接口的实现类,它的底层实现是HashMap.
HashSet的特点是:无序,不可重复,添加重复元素会覆盖。【所以HashSet类没有get(int index)方法】
-
常用方法:
add
remove
size
contains
package com.collection;import java.util.HashSet;import java.util.Set;public class TestSet { public static void main(String[] args){ Set a = new HashSet(); a.add("aaa"); a.add("bbb"); a.add(new String("aaa"));//相同内容的元素是加不进set的 System.out.println(a.size()); a.remove("bbb"); System.out.println(a.size()); System.out.println(a.contains("aaa")); }}
第111集:容器自定义实现HashSet
HashSet能够实现不重复,是因为利用了map的key不可重复的特性。
package com.collection;import java.util.HashMap;/** * @author wangtao * 自定义自己的HashSet */public class SxtHashSet { HashMap map; final Object PRESENT = new Object(); public SxtHashSet(){ map = new HashMap(); } public void add(Object o){ map.put(o, PRESENT); } public int size(){ return map.size(); } public static void main(String[] args){ SxtHashSet a = new SxtHashSet(); a.add("aaa"); a.add("bbb"); a.add("aaa"); System.out.println(a.size()); } }
第112集:容器数据存储综合练习javabean的介绍
Javabean是指里面只有属性、set方法与get方法的类。
代码所示方法是一种用对象封装数据的方法
package test112;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;public class Employee { private int Id; private String name; private int salary; private String company; private Date date; public Employee(int Id,String name,int salary,String company,String date){ this.Id = Id; this.name = name; this.salary = salary; this.company = company; DateFormat format = new SimpleDateFormat("yyyy-MM"); try { this.date = format.parse(date); } catch (ParseException e) { e.printStackTrace(); } } public void setId(int Id){ this.Id = Id; } public int getId(){ return Id; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setSalary(int Salary){ this.salary = Salary; } public int getSalary(){ return salary; } public void setCompany(){ this.company = company; } public String getCompany(){ return company; } public void setDate(Date date){ this.date = date; } public Date getDate(){ return date; } public static void main(String[] args){ Employee a = new Employee(0301,"高琪",3000,"项目部","2007-10"); Employee b = new Employee(0302,"马士兵",3200,"项目部","2005-10"); Employee c = new Employee(0303,"裴新",3300,"项目部","2006-10"); Listlist = new ArrayList (); list.add(a); list.add(b); list.add(c); for(int i=0;i
第113集:容器数据存储综合练习map保存表记录
用Map接口保存数据也是一种常用的方法。【对比112集来看本集】
package test112_113;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class Employee_1 { public static void main(String[] args){ Map map = new HashMap(); map.put("id", "0301"); map.put("name", "高琪"); map.put("salary", "3050"); map.put("company", "项目部"); map.put("Date", "2007-10"); Map map2 = new HashMap(); map2.put("id", "0302"); map2.put("name", "马士兵"); map2.put("salary", "3050"); map2.put("company", "项目部"); map2.put("Date", "2007-10"); Map map3 = new HashMap(); map3.put("id", "0303"); map3.put("name", "裴新"); map3.put("salary", "3050"); map3.put("company", "项目部"); map3.put("Date", "2007-10"); List