------- android培训、java培训、期待与您交流! ----------
(一)Map体系
Map
<K,V>
集合:相对于Colllection单列集合来说,Map集合称为双列集合,存储的每个元素均为键值对,对键进行约束。可以通过键找到对应的值,键(K)都是唯一的,多个键可以对应同一个值(V)。与数学中的函数类似。
Map集合的体系:
Map
|--HashMap
|--LinkedHashMap
|--Hashtable
|--TreeMap
•HashMap:底层为哈希表结构的Map集合。
•Hashtable:底层为哈希表结构的Map集合。
•TreeMap:对键进行排序,排序原理与TreeSet相同的Map集合。
•LinkedHashMap:可预知顺序的HashMap集合
•HashMap键无序,不可重复,不安全,速度快
-LinkedHashMap键有序,不可重复
•Hashtable键无序,不可重复,安全,速度慢
•TreeMap键排序,不可重复
常用功能:
A:添加功能
V put(K key,V value)
B:删除功能
remove(K key)
C:判断功能
containsKey(K key)
containsValue(V value)
D:获取功能
V get(K key)
Set<K> keySet()
Collection<V> values()
Set<Map.Entry<K,V>> entrySet()
Map.Entry:
getKey
getValue
E:长度功能
int size()
Map的遍历方式
假设有一个HashMap集合,存储的键和值都是String类型。名称叫hm。
A:根据键找值
a:获取所有键的集合
b:遍历键的集合,获取到每一个键
c:根据键找值
代码体现:
public class Demo03_KeySet {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
//向集合对象中添加元素
map.put("诺一","刘烨");
map.put("妮娜","刘烨");
map.put("夏天","夏克立");
map.put("轩轩","邹市明");
//遍历双列Map集合 》》》键=值 , 键=值
//返回所有键键的set集合
Set<String> keySet2 = map.keySet();
//迭代键的集合,依次获取到每一个键
for (String key : keySet2) {
//通过Map中的键,获取Map中的值,是Map调用方法
String value = map.get(key);
//输出键值对对应关系
System.out.print("key:"+key+" = value:"+value+",");
}
}
}
B:根据键值对对象找键和值
a:获取所有键值对对象的集合
b:遍历键值对对象的集合,获取到每一个键值对对象
c:根据键值对对象获取键和值
* HashMap: Map集合遍历的方式二
* public Set<Map.Entry<K,V>> entrySet() 返回此映射所包含的映射关系的 Set 视图。返回所有键值对
* Entry是Map的内部接口,可以操作map中的所有内容
*
* Entry: 映射项(键-值对)。
* 方法:
* K getKey() 根据键值对返回键
* V getValue() 根据键值对返回值
* V setValue(V value) 根据键值对修改值
*/
public class Demo04_entrySet {
public static void main(String[] args) {
//定义map集合对象(指定泛型)
HashMap<String, String> map = new HashMap<>();
//准备元素对象
String wife = "白娘子";
String hus = "许仙";
String wife2 = "二姨太";
String wife3 = "七姨太";
String hus2_3 = "何鸿燊";
String wife4 = "雪姨";
String hus4 = "JamesGosling";
//添加键值对
map.put(wife, hus);
map.put(wife2, hus2_3);
map.put(wife3, hus2_3);
map.put(wife4, hus4);
//调用map的方法,返回键值对对应关系的集合。
Set<Entry<String,String>> entrySet = map.entrySet();
//迭代键值对对应关系集合,依次获取到每一个键值对对应关系
for (Entry<String, String> entry : entrySet) {
//通过键值对对应关系获取键
String key = entry.getKey();
//通过键值对对应关系获取值
String value = entry.getValue();
//打印键值
System.out.println("key:"+key);
System.out.println("value:"+value);
//修改值
if("小青".equals(key)) {
entry.setValue("法海");
}
}
System.out.println(map);
}
}
(二)lHashMap
底层为哈希表的双列集合。对键进行无序唯一约束,放入相同键时,新的值会将旧的值覆盖。是最常用的Map集合。
•特点
-线程不安全
-速度快
-允许存放null键,null值
•主要方法
-public V put(K key, V value)
-public void putAll(Map<? extends K,? extends V> m)
-public boolean isEmpty()
-public int size()
-public boolean containsKey(Object key)
-public boolean containsValue(Object value)
-public V remove(Object key)
-public void clear()
-public V get(Object key)
-public Set<K> keySet()
-public Collection<V> values()
-public Set<Map.Entry<K,V>> entrySet()
public class Demo01_Map {
public static void main(String[] args) {
//创建集合对象
HashMap<String, String> map = new HashMap<String, String>();
//创建元素对象
String child = "康康";
String father = "胡军";
String child2 = "诺一";
String child4 = "妮娜";
String father2 = "刘烨";
String child3 = "夏天";
String father3 = "夏克立";
map.put(child, father);
map.put(child2, father2);
map.put(child3, father3);
map.put(child4, "隔壁老王");
//map集合添加元素时,如果键相同,值不同,则键对应的值会有新值覆盖掉老值
System.out.println(map.put(child4, father2));
//查看集合
System.out.println(map);
}
}
/*
* HashMap普通方法:
* public V put(K key, V value)在此映射中关联指定值与指定键 如果键相同,则旧值被替换 *****
* public V get(Object key) 根据键获取值 *****
*
* public boolean isEmpty() 判断map集合为空
* public boolean containsKey(Object key) 判断是否包含某个键
* public boolean containsValue(Object value) 判断是否包含某个值
* public V remove(Object key) 删除对应关系 根据键删除元素 返回值为删除掉的值
*/
public class Demo02_HashMap {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("诺一","刘烨");
map.put("妮娜","刘烨");
map.put("夏天","夏克立");
map.put("轩轩","邹市明");
map.put("", "");
map.put("", null);
map.put(null, "");
map.put(null, null);
System.out.println(map.isEmpty());
System.out.println(map.size());
System.out.println(map);
System.out.println("===============================");
System.out.println(map.containsKey("诺一"));
System.out.println(map.containsKey(""));
System.out.println(map.containsKey("刘烨"));
System.out.println("===============================");
System.out.println(map.containsValue("诺一"));
System.out.println(map.containsValue(""));
System.out.println(map.containsValue("刘烨"));
System.out.println("===============================");
String string = map.get("诺一");
System.out.println(string);
System.out.println(map.remove(""));
System.out.println(map);
map.clear();
System.out.println(map);
}
}
(三)Hashtable
(四)TreeMap
底层为哈希表的集合,已被HashMap替代。Hashtable的Properties用于配置文件的定义和操作,键和值都是字符串,是集合中可以和IO技术相结合的对象
•特点
-线程安全
-速度慢
-不允许存放null键,null值
-命名方式不符合标准大驼峰式
对键进行排序,排序原理与TreeSet相同的Map集合。
•排序方式同TreeSet
-使键实现Comparable接口,重写compareTo方法
-创建集合对象时,传入Comparator对象,重写compare方法
/*
* TreeMap: 对键进行排序的双列集合
*/
public class Demo07_TreeMap {
public static void main(String[] args) {
TreeMap<Person, String> tm = new TreeMap<Person, String>(new Comparator<Person>() {
//先比较姓名,再比较年龄
@Override
public int compare(Person o1, Person o2) {
//先比较年龄
int result = o1.getName().compareTo(o2.getName());
//如果年龄相同,再比较姓名
if(result==0) {
result = o1.getAge() - o2.getAge();
}
return result;
}
});
tm.put(new Person("a小雷",18), "芙蓉");
tm.put(new Person("a小燊",20), "赵丽颖");
tm.put(new Person("c小强",18), "柳岩");
tm.put(new Person("d姜哥",24), "凤姐");
System.out.println(tm);
}
}
public class Person implements Comparable<Person>{
private String name;
private int age;
public Person() {}
public Person(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 "Person [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
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 (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
//指定需求规则:先比较年龄,再比较姓名
@Override
public int compareTo(Person o) {
//先比较年龄
int result = this.age - o.age;
//如果年龄相同,再比较姓名
if(result==0) {
result = this.name.compareTo(o.name);
}
return result;
}
}
(五)练习:
练习:”aabcbdeeeeedbddcc”,获取字符串中每一个字母出现的次数。要求结果:a(2)b(3)c(3)d(4)e(5)
分析:
1,每个字母出现的次数和字母是一一对应的关系,所以我们应该想到的是Map的双列集合
2,因为从结果看打印字母的顺序都有序,所以可以使用map集合中的TreeMap集合
思路:
1,将字符串变成字符数组,因为要操作字符串中的每一个字母
2,遍历数组,将每一个字母都作为键去map集合中获取值
3,如果获取的值为null,说明该键不存在,就将该键和1存入到集合中,如果该值不为null,说明该键已经存在于
map集合,并有对应的值,那么就将该值取出,并自增后,再将该键和新的值存入map集合中,因为键相同,新值
会覆盖老值
4,遍历结束map集合中就已经具备了每一个字母对应的次数
5,将集合中的数据变成字符串打印
<pre name="code" class="java">public class Test2 {
public static void main(String[] args) {
String s = "aabcbdeeeeedbddcc";
String result = method(s);
System.out.println(result);
}
public static String method(String s) {
//创建TreeMap集合,用于存储字母与数字的对应关系
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
//1,将字符串变成字符数组,因为要操作字符串中的每一个字母
char[] chars = s.toCharArray();
//2,遍历数组,将每一个字母都作为键去map集合中获取值
for (char key : chars) {
//将每一个字母都作为键去map集合中获取值
Integer value = tm.get(key);
//3,如果获取的值为null,说明该键不存在,就将该键和1存入到集合中,
//如果该值不为null,说明该键已经存在于map集合,并有对应的值,那么就将该值取出,并自增后,
//再将该键和新的值存入map集合中,因为键相同,新值会覆盖老值
//如果结果map中没有该字母,就加入该字母1次
if(value == null) {
tm.put(key, 1);
}else {
//如果有该字母,就获取该字母之前出现的次数value,再加1次,重新覆盖之前的次数
tm.put(key, ++value);
}
}
//5,将集合中的数据变成字符串打印
//创建字符串缓冲区对象,用于使用map数据拼写结果
StringBuilder sb = new StringBuilder();
//返回所有键的集合
Set<Character> keySet = tm.keySet();
//迭代键的集合依次获取每个键
for (Character key : keySet) {
//使用Map通过键获取值
Integer value = tm.get(key);
sb.append(key)
.append("(")
.append(value)
.append(")");
}
return sb.toString();
}
}