----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------
Blog6_1 Map集合基本概念
Map集合:存储键值对类型元素的集合,是键值类型集合的顶层接口。
Map集合和Collection集合的区别:
(1)Map集合存储的是成对存在的元素,而Collection集合存储的是单列元素;
(2)Map集合键唯一,值可以重复,当存储的元素键一样,值进行覆盖并返回原来的值,而Collection集合中List存储元素可以重复,Set集合存储元素无序唯一。
Map集合的常用子类:HashMap、TreeMap、Properties。
Blog6_2 Map集合功能
A:添加功能
V put(K key,V value)----注意和Collection集合中的add区分。
B:删除功能
V remove(Kkey)----删除指定键的键值对元素。
C:判断功能
booleancontainsKey(K key)----判断是否包含指定键的键值对元素。
booleancontainsValue(V value)----判断是否包含指定值的键值对元素。
D:获取功能
V get(K key)----获取指定键的值。
Set<K> keySet()----获取所有键的集合
Collection<V>values()----获取所有值的集合。
Set<Map.Entry<K,V>> entrySet()----返回此映射中包含的映射关系的Set 视图。
E:长度功能
int size()----集合中所有键值对的对数。
Blog6_3 HashMap集合存储字符串对象并遍历
Blog6_3_1 HashMap集合存储字符串对象
Map<String,String> map = new HashMap<String, String>();
//注意:要存储的键和值分别是什么类型,在泛型中要表示出来,如上。
map.put("周瑜","小乔");
map.put("诸葛亮","黄月英");
map.put("吕布","貂蝉");
map.put("孙策","大乔")
Blog6_3_2遍历字符串HashMap集合中的元素
遍历Map集合主要使用到了Map集合中的:Set<K>keySet()和get(K key)功能,实现代码如下:
Set<String> set = map.keySet();//获取Map集合的所有键值并存储//到一个Set集合中。
//使用增强for遍历Map集合:
for (String key : set) {
String value = map.get(key);//使用get方法获取值
System.out.println(key + "---" + value);
}
Blog6_3_3键值对对象找键和值方式遍历
此种方式的思路是获取键值对的对象,再根据键值对获取键或者值,如下代码:
Set<Map.Entry<String,String>> set = map.entrySet();
for (Map.Entry<String,String> me : set) {
System.out.println(me.getKey() +"---" +me.getValue());
}
Blog6_4 HashMap集合存储自定义对象并遍历
Blog6_4_1 HashMap集合存储自定义对象代码如下:
public staticvoidmain(String[] args) {
// 创建集合对象
HashMap<String, Student> hm = new HashMap<String,Student>();
// 创建学生对象
Student s1 = new Student("林青霞", 27,"女");
Student s2 = new Student("王祖贤", 57,"女");
Student s3 = new Student("马德华", 37,"男");
// 添加元素
hm.put("it001",s1);
hm.put("it002",s2);
hm.put("it003",s3);<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
值得注意的一点是,当键为Student类时,Student类中必须重写hashCode()和equals()方法,才能进行键唯一的存储。
Blog6_4_2遍历自定义对象HashMap集合中的元素
// 遍历
Set<String>set = hm.keySet();
for (String key : set) {
Student value = hm.get(key);
// System.out.println(key + "---" + value);
System.out.println(key +"---" +value.getName() + "---"
+value.getAge() + "---" + value.getSex());
}
}
Blog6_5 TreeMap集合存储字符串对象并遍历
Blog6_5_1 TreeMap集合存储字符串对象
TreeMap<String, String> tm =new TreeMap<String, String>();
tm.put("yn004","王一勇");
tm.put("yn003","谢世成");
tm.put("yn001","罗志祥");
tm.put("yn002","小布");
Blog6_5_2遍历字符串TreehMap集合中的元素
Set<String> set = tm.keySet();
for (String key : set) {
String value = tm.get(key);
System.out.println(key +"---" + value);
}
注意,上面代码打印出来的结果是:
yn001---罗志祥
yn002---小布
yn003---谢世成
yn004---王一勇
保证键的唯一性,实质是String中重写了hashCode()和equals()方法
根据键进行了排序,实质是String类中实现了Comparable<String>接口。
Blog6_5_2键值对对象找键和值方式遍历
//获取键值对对象:
Set<Map.Entry<String,String>>set=map.entrySet();
//使用增强for对键值对对象集合进行遍历
for(Map.Entry<String,String> me:set){
System.out.println(me.getKey()+"--"+me.getValue());
}
Blog6_6 TreeMap集合存储键自定义对象并遍历
Blog6_6_1TreeMap集合存储键自定义对象并遍历
TreeMap<Student, String> tm = new TreeMap<Student,String>();
// 创建元素对象
Student s1 = new Student("索额图", 50);
Student s2 = new Student("明珠", 40);
Student s3 = new Student("鳌拜", 47);
Student s4 = new Student("陈廷敬", 43);
Student s5 = new Student("李光地", 44);
//存储键自定义对象
tm.put(s1, "湖北");
tm.put(s2,"河南");
tm.put(s3,"辽宁");
tm.put(s4,"新疆");
tm.put(s5, "山西");
//遍历
Set<Student> set =tm.keySet();
for (Student key : set) {
String value = tm.get(key);
System.out.println(key.getName() +"---"+ key.getAge() +"---"
+value);
}
运行上面程序,发现控制台显示:treemap2.Student cannot be cast to java.lang.Comparable,是因为TreeMap要进行排序,而键是自定义对象,我们并没有告诉虚拟机按照什么规则来排序,所有出错了。解决方法有两种
A. 让Student类实现comparable接口
B. 使用TreeMap(Comparator<? superK>comparator)构造方法配合匿名内部类的方法告诉虚拟机怎样排序。下面我们提供此种方法的代码:
TreeMap<Student, String> tm = new TreeMap<Student,String>( new Comparator<Student>() {
public int compare(Student s1,Student s2) {
int num1 =s1.getName().length()
-s2.getName().length();
int num2 = (num1 == 0) ?(s1.getAge() - s2.getAge())
:num1;
int num3 = (num2 == 0) ?(s1.getName().compareTo(s2
.getName())): num2;
return num3;
};
});
----------------------- android培训、java培训、java学习型技术博客、期待与您交流! ----------------------
详情请查看: