Set 子接口,无序、无下标、元素不可重复。
HashSet 实现类。用 HashCode、equals(==) 两个步骤判断对象是否相同。
存入不同地址,相同属性值的对象。
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "id " + id + " name " + name + " age " + age;
}
}
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu1 = new Student(123, "张三", 18);
Student stu2 = new Student(456, "李四", 19);
Student stu3 = new Student(789, "王五", 20);
Set<Student> set1= new HashSet<Student>();
set1.add(stu1);
set1.add(stu2);
set1.add(stu3);
Student stu4 = new Student(789, "王五", 30);
set1.add(stu4);
for (Student student : set1) {
//set1 有4个值
System.out.println(student);
}
}
}
重写 Student 类的 hashCode、equals 方法,实现对象的属性值相同就不能存入 Set 集合。
//Student 类方法重写
@Override
public int hashCode() {
int i1 = id.hashCode() + name.hashCode();
return i1;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (obj instanceof Student) {
Student w = (Student)obj;
if (w.id.equals(id)
&& w.name == name) {
return true;
}
}
return false;
}
LinkedHashSet ,链表实现的HashSet,按照链表进行存储,即可保留元素的插入顺序。
TreeSet ,实现了 SortedSet 接口,对集合元素自动排序。基于排列顺序实现元素不重复。自定义类必须实现 Comparable 接口,指定排序规则。通过 CompareTo 方法确定是否为重复元素。compareTo == 0 即代表是相同值不会存入集合。
public class Student implements Comparable<Student> {
private Integer id;
private String name;
private Integer age;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "id " + id + " name " + name + " age " + age;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int i1 = id.compareTo(o.id);
int i2 = name.compareTo(o.name);
return i1 != 0 ? i1 : i2;
}
}
public class Test {
public static void main(String[] args) {
Student stu1 = new Student(123, "张三", 18);
Student stu2 = new Student(456, "李四", 19);
Student stu3 = new Student(789, "王五", 20);
TreeSet<Student> set1= new TreeSet<Student>();
set1.add(stu1);
set1.add(stu2);
set1.add(stu3);
Student stu4 = new Student(789, "王五", 30);
set1.add(stu4);
for (Student student : set1) {
// 去重且排序
//id 123 name 张三 age 18
//id 456 name 李四 age 19
//id 789 name 王五 age 20
System.out.println(student);
}
}
}
匿名函数实现 Comparable 。
TreeSet<Student2> treeSet4 = new TreeSet<Student2>(new Comparator<Student2>() {
@Override
public int compare(Student2 o1, Student2 o2) {
int i1 = o1.getId().compareTo(o2.getId());
int i2 = o1.getName().compareTo(o2.getName());
return i1 != 0 ? i1 : i2;
}
});
Map 体系集合。称为“映射”存储一对数据(Key-Value),键不可重复,值可以重复。
Map<Interface> | ||
---|---|---|
HashMap | Hashtable | SortedMap<Interface> |
Properties | TreeMap |
HashMap ,JDK1.2 版本,线程不安全,运行效率快 。允许用 null 作为 key 或是 value 。HashMap 的 key 和 HashSet 的规则一样
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test1 {
public static void main(String[] args) {
Map<String, String> maps = new HashMap<String, String>();
maps.put("CN", "中国");
maps.put("USA", "美国");
maps.put("FN", "法国");
System.out.println(maps.get("CN"));
maps.remove("FN");
Set<String> keyset = maps.keySet();
for (String string : keyset) {
System.out.println(string);
}
Set<Entry<String, String>> entrySet = maps.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println("key " + entry.getKey() + " value " + entry.getValue());
}
// key可以为空,但只能有一个
maps.put(null, null);
}
}
Hashtable ,JDK1.0 版本,线程安全,运行效率慢;不允许 null 作为 key 或是 value,不常用。
Properties ,Hashtable 的子类,要求 key 和 value 都是 String。通常用于配置文件的读取。
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class test {
public static void main(String[] args) {
Properties pro = new Properties();
// 存值
pro.setProperty("username", "admin");
pro.setProperty("password", "123456");
// 取值
System.out.println(pro.getProperty("username"));
System.out.println(pro.getProperty("password"));
Properties pro2 = System.getProperties();
Set<Entry<Object, Object>> see = pro2.entrySet();
for (Entry<Object, Object> entry : see) {
System.out.println(entry.getKey() + "\t" + entry.getValue());
}
}
}
TreeMap ,key 和 TreeSet 的规则一样,自定义类作为 Key 时需实现 Comparable 接口。
public class Test {
public static void main(String[] args) {
TreeMap<Student, String> maps = new TreeMap<Student, String>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int i = o1.getId().compareTo(o2.getId());
int i2 = o1.getName().compareTo(o2.getName());
return i!=0?i:i2;
}
});
Student s1 = new Student(123, "张三", 18);
Student s2 = new Student(124, "李四", 19);
Student s3 = new Student(125, "王五", 20);
Student s4 = new Student(126, "周六", 21);
Student s5 = new Student(126, "周六", 21);
maps.put(s1, "3");
maps.put(s2, "4");
maps.put(s3, "5");
maps.put(s4, "6");
maps.put(s5, "6");
// key 去重且排序
System.out.println(maps);
TreeMap<String, String> maps2 = new TreeMap<String, String>();
maps2.put("e", "H");
maps2.put("c", "C");
maps2.put("b", "B");
maps2.put("d", "D");
maps2.put("a", "A");
System.out.println(maps2);
}
}