18-集合(二)Set,Map
- set接口: 无序的,不重复的(唯一)
- 特点: set中所有的方法都是Collection接口中的
import java.util.HashSet;
import java.util.Set;
public class SetDemo {
public static void main(String[] args) {
Set s= new HashSet();
System.out.println(s.add("清子"));
System.out.println(s.add("灼灼"));
System.out.println(s.add("清子")); //覆盖还是没添加进去
System.out.println(s);
}
}
- TreeSet: 有序,唯一
- 特点: 底层采用红黑树(二叉树),要求有序(升序) ,自然顺序
- TreeSet是通过比较器去去掉重复的元素(定义比较规则)
- 比较器:
- 内部比较器: Comparable
- 用当前对象this和传入的对象o进行比较,只能定义一次
- 外部比较器: Comparator
- 与当前对象无关,只完成比较的功能,可以定义多个比较规则
- 匿名比较器:
- 在匿名内部类中定义
- 内部比较器: Comparable
public class Person implements Comparable<Person> {
private String name;
private int age;
private String id; //身份证号
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, String id) {
super();
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", id=" + id + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
//内部比较器
@Override
public int compareTo(Person o) {
// TODO Auto-generated method stub
Integer i1 = this.getAge();
Integer i2 = o.getAge();
if(i1 == i2){
return this.getName().compareTo(o.getName());
}else{
return i1.compareTo(i2);
}
}
}
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
//创建对象
TreeSet<String> ts = new TreeSet<String>();
ts.add("z");
ts.add("a"); //有序(自然顺序)
ts.add("z");
System.out.println(ts);
//创建比较器的对象
//ImplCom ic = new ImplCom();
TreeSet<Person> p = new TreeSet<>(
new Comparator<Person>() { //创建内部类比较器
@Override
public int compare(Person o1, Person o2) {
// TODO Auto-generated method stub
return o1.getId().compareTo(o2.getId());
}
}
);
//创建Person对象
Person p1 = new Person("aa", 12, "123");
Person p2 = new Person("bb", 2, "231");
Person p3 = new Person("cc", 11, "132");
Person p4 = new Person("a", 12, "123");
//把Person类型的对象放进集合hs中
p.add(p1);
p.add(p2);
p.add(p3);
p.add(new Person("kele", 15, "2222")); //匿名对象
p.add(p4);
System.out.println(p);
}
}
class ImplCom implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return o1.getId().compareTo(o2.getId());
}
}
- HashSet: 底层
- hashSet的存储原理:
- 计算HashCode值,通过HashCode值得到存储的位置(y=x%7), y代表存储位置 x:代表hash码值
- 如果HashCode值相同,再比较内容是否相等,1. 如果内容相同,不再存储; 2. 如果内容不同,则在同一位置上下连参数
- hashSet的存储原理:
import java.util.HashSet;
public class HashSetDemo {
public static void main(String[] args) {
//创建一个String类型的HashSet集合对象h1
HashSet<String> h1 = new HashSet<>();
//创建一个Person类型的hashSet集合对象hs
HashSet<Person> hs = new HashSet<>();
//创建Person对象
Person p1 = new Person("aa", 12, "123");
Person p2 = new Person("bb", 2, "231");
Person p3 = new Person("cc", 11, "132");
Person p4 = new Person("aa", 12, "123");
//把Person类型的对象放进集合hs中
hs.add(p1);
hs.add(p2);
hs.add(p3);
hs.add(new Person("kele", 15, "2222")); //匿名对象
hs.add(p4);
System.out.println(hs.size());
}
}
- LinkedHashSet: 既可以去重, 又可以唯一的实现类
- 是HashSet的自雷,底层是哈希表+链表,哈希表可以去重,链表维护次序,有序(添加顺序)
import java.util.LinkedHashSet;
public class LinkedHashSetDemo {
public static void main(String[] args) {
//
LinkedHashSet<Person> hs = new LinkedHashSet<>();
//创建Person对象
Person p1 = new Person("aa", 12, "123");
Person p2 = new Person("bb", 12, "231");
Person p3 = new Person("cc", 11, "132");
Person p4 = new Person("a", 12, "123");
//把Person类型的对象放进集合hs中
hs.add(p1);
hs.add(p2);
hs.add(p3);
hs.add(new Person("kele", 15, "2222")); //匿名对象
hs.add(p4);
System.out.println(hs);
}
}
-
Map:
Collection: 单个元素
Map: 两个元素(键值对)
key: (无序,唯一) Set类型
value: (有序,不唯一) List Collection
增: put(k key, v ) putAll(Map<? extends K, ? extends V> m)
删: clear() replace(K key. V value)
改:
查:entrySet() get(Object key) containsValue(Object value)
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
//创建map集合对象,设置key为Integer类型,设置values设置为String类型
Map<Integer, String> map = new HashMap<Integer, String>();
Map<Integer, String> map1 = new HashMap<>();
//[1]添加
map.put(101, "乌龟");
map.put(106, "国栋");
System.out.println("我是aa"+map.put(101, "aaa")); //覆盖
System.out.println(map);
//[2]判断
System.out.println("判断key是否存在: "+map.containsKey(101));
System.out.println("判断values是否存在: "+map.containsValue("国栋"));
//[3]删除
//System.out.println("根据key值删除元素: "+map.remove(101));
System.out.println("根据key删除元素: "+map.remove(11)); //没有则返回false
//System.out.println(map);
//[4]查找
//1.get:根据key值查找
System.out.println(map.get(101));
//2.keySet : 得到key的集合
Set<Integer> set = map.keySet();
for(Integer i:set){
System.out.println(i);
}
System.out.println("===========================");
//3.values
Collection<String> coll = map.values();
for(String c:coll){
System.out.println(c);
}
System.out.println("===========================");
Set<Entry<Integer, String>> s = map.entrySet();
for(Entry<Integer, String> e:s){
System.out.println(e);
}
}
}
- HashMap: 1.2版本 不安全 -->允许null键 和 null值 .但是null只能有一个
- HashTable: 1.0 安全 -->不允许null键和null值
import java.util.HashMap;
import java.util.Hashtable;
public class MapDemo2 {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(11, "小奋青");
hm.put(12, "扎克伯格");
hm.put(13, "脸书");
hm.put(null, "妖灵");
hm.put(null, "戎 装");
hm.put(1, null);
hm.put(1, null);
hm.put(null, null);
hm.put(null, null);
System.out.println(hm);
System.out.println(hm.size());
/* Hashtable<Integer, String> hm = new Hashtable<Integer, String>();
hm.put(11, "小奋青");
hm.put(12, "扎克伯格");
hm.put(13, "脸书");
hm.put(null, "妖灵");
hm.put(null, "戎 装");
hm.put(1, null);
hm.put(1, null);
hm.put(null, null);
hm.put(null, null);
System.out.println(hm);
}*/
}
}