Map集合
Map接口概述
将键映射到值的对象
一个映射不能包含重复的键
每个键最多只能映射到一个值
Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构针对键有效,跟值无关;
Collection集合的数据结构是针对元素有效
Map集合的功能概述
a:添加功能
V put(K key,V value):添加元素。这个其实还有另一个功能?替换
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
public class MyTest2 {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<>();
//存储键值对
/* V put (K key, V value)
在此映射中关联指定值与指定键。*/
String s = hm.put("文章", "马伊琍");
System.out.println(s);
//键相同,值覆盖,当你第一次存储这个键值对时,返回的是null
//当你第二次存储相同的键,但是值不同,就会发生键相同,值覆盖,返回的是这个键第一次的旧值
String s1 = hm.put("文章", "姚笛");
System.out.println(s1);
hm.put("贾乃亮", "李小璐");
hm.put("王宝强", "蓉儿");
hm.put("陈羽凡", "白百合");
hm.put("王大治", "董洁");
hm.put("大朗", "金莲");
System.out.println(hm);
}
}
b:删除功能
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回
c:判断功能
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
d:长度功能
int size():返回集合中的键值对的对数
public class MyTest {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<>();
//存储键值对
String s = hm.put("文章", "马伊琍");
hm.put("贾乃亮", "李小璐");
hm.put("王宝强", "蓉儿");
hm.put("陈羽凡", "白百合");
hm.put("王大治", "董洁");
hm.put("大朗", "金莲");
//移除一个键值对
String value = hm.remove("大朗");
System.out.println(value);
System.out.println(hm);
boolean empty = hm.isEmpty(); //判断集合是否为空
int size = hm.size();//获取集合的长度
//判断集合中是否包含这个键
System.out.println(hm.containsKey("贾乃亮"));
//判断集合中是否包含这个值
System.out.println(hm.containsValue("金莲"));
//清空集合中所有的键值对
hm.clear();
System.out.println(hm);
}
}
d:获取功能
Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
V get(Object key):根据键获取值
Set keySet():获取集合中所有键的集合
Collection values():获取集合中所有值的集合
public class MyTest {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<>();
//存储键值对
String s = hm.put("A", "a");
hm.put("B", "b");
hm.put("C", "c");
hm.put("D", "d");
hm.put("E", "e");
hm.put("F", "f");
//遍历map集合方式1 ,根据键找值
/* String value = hm.get("文章");
System.out.println(value);*/
Set<String> strings = hm.keySet(); //获取所有键的集合
//遍历键集,根据键找值
for (String key : strings) {
System.out.println(key+"==="+hm.get(key));
}
//获取值的集合
Collection<String> values = hm.values();
System.out.println(values);
}
}
public class MyTest2 {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<>();
//存储键值对
String s = hm.put("文章", "马伊琍");
hm.put("贾乃亮", "李小璐");// Node<K,V> getKey() getValue()
hm.put("王宝强", "蓉儿");
hm.put("陈羽凡", "白百合");
hm.put("王大治", "董洁");
hm.put("大朗", "金莲");
//map集合的遍历方式2:把键找对,统一看作一个整体取出来,放到一个集合中
//Map.Entry<String, String>
/*接口 Map.Entry<K, V>
K getKey ()
返回与此项对应的键。
V getValue ()
返回与此项对应的值。*/
Set<Map.Entry<String, String>> entries = hm.entrySet();
for (Map.Entry<String, String> entry : entries) {
// System.out.println(entry); Node
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"==="+value);
}
}
}
LinkedHashMap
概述:
Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序
特点:
底层的数据结构是链表和哈希表 元素有序 并且唯一
元素的有序性由链表数据结构保证 唯一性由 哈希表数据结构保证
Map集合的数据结构只和键有关
public class MyTest {
public static void main(String[] args) {
// LinkedHashSet
// LinkedHashMap 键唯一,且有序,键的数据结构是哈希表和链表,链表保证了有效,哈希表保证了唯一
LinkedHashMap<String,String> hm = new LinkedHashMap<>();
hm.put("贾乃亮", "李小璐");
hm.put("贾乃亮", "李小璐2");
hm.put("王宝强", "蓉儿");
hm.put("陈羽凡", "白百合");
hm.put("王大治", "董洁");
hm.put("大朗", "金莲");
hm.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String key, String value) {
System.out.println(key+"==="+value);
}
});
}
}
TreeMap
键的数据结构是红黑树,可保证键的排序和唯一性
排序分为自然排序和比较器排序
线程是不安全的
效率比较高
~~public class MyTest3 {
public static void main(String[] args) {
//Map 集合的数据结构根键有关
//键是二叉树
TreeMap<Student, String> hashMap = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num=s1.getName().length()-s2.getName().length();
int num2=num==0?s1.getName().compareTo(s2.getName()):num;
int num3=num2==0?s1.getAge()-s2.getAge():num2;
return num3;
}
});
hashMap.put(new Student("张三", 23), "s001");
hashMap.put(new Student("张三", 230), "s002");
hashMap.put(new Student("张三", 23), "哈哈哈哈");
hashMap.put(new Student("张三3333", 230), "s001");
hashMap.put(new Student("张三sdfsfsdfs", 23), "呵呵呵呵呵");
hashMap.put(new Student("lisi", 29), "s001");
hashMap.put(new Student("王五sdffffffffffffffffffffffffffffffffff", 28), "s001");
hashMap.put(new Student("赵六ssss", 27), "s001");
hashMap.put(new Student("田七", 27), "s001");
// System.out.println(hashMap);
hashMap.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.getName()+"---"+student.getAge()+"====="+s);
}
});
}
}
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
HashMap和Hashtable的区别
HashMap和Hashtable的区别: 查看API可以知道
HashMap: 线程不安全,效率高.允许null值和null
Hashtable: 线程安全 , 效率低.不允许null值和null键
Collections工具类的概述和常见方法讲解
A:Collections类概述: 针对集合操作 的工具类
B:Collections成员方法
public static void sort(List list): 排序,默认按照自然顺序
public static int binarySearch(List<?> list,T key): 二分查找
public static T max(Collection<?> coll): 获取最大值
public static void reverse(List<?> list): 反转
public static void shuffle(List<?> list): 随机置换