黑马程序员技术交流社区
标题:
Map集合小总结及两种取值方法
[打印本页]
作者:
z13561291
时间:
2015-11-17 12:51
标题:
Map集合小总结及两种取值方法
Map集合:该集合存储键值对,一对一对往里存,而且保证键的唯一性
1.添加
put(key,value)
putAll(Map<? extends Key,? extends Value>n)
2.删除
remove(Object key)
claer()
3.判断
containsValue(Object value)
containsKey(Object value)
isEmpty*(
4.获取
get(Object key)
size()
values()
Set<Map.Entry<Key,Value>> entrySet()
Map.Entry 其实Entry也是一个接口,它是Map接口中的一个内部接口
interface Map
{
public static interface Entry{
public abstract Object getKey();
public abstract Objext getValue();
}
}
class HashMap implements Map.Entry
{
public abstract Object getKey(){}
public abstract Objext getValue(){}
}
Set<T> = 集合.keySet() 将Map中所有的键存入到Set集合,因为Set具备迭代器,
所以可以迭代方式取出所有的键,在根据get方法,获取每个键所对应的值
Map集合的取出原理:将map集合转成set集合,通过迭代器取出
Map
|--Hashtable:底层是哈希表数据结构,不可以存入null键为null值,是线程同步的
|--HashMap:底层是哈希表数据结构,可以存入null键和null值,是线程不同步的
|--TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键进行排序
和Set很像
其实Set底层就是使用了Map集合
/*
每个学生都有对应的归属地
学生Student,地址String
学生属性:姓名,年龄
注意:姓名和年龄相同的视为同一学生
保证学生的唯一性。
*/
import java.util.*;
class Student implements Comparable<Student> //让学生类具备可比较性
{
private String name;
private int age;
private String dizhi;
Student(String name,int age){
this.name = name;
this.age = age;
}
public int compareTo(Student s){
int num = new Integer(this.age).compareTo(new Integer(s.age)); //用Integer类型比较年龄大小,返回正,负,0
if(num ==0)
return this.name.compareTo(s.name); //比较name
return num;
}
public int hashCode(){ //重写Code方法比较哈希值
return name.hashCode()+age*34;
}
public boolean equals(Object obj){ //重写equals方法,按需求来比较
if(!(obj instanceof Student))
throw new ClassCastException("不是学生类型"); //当传入不是学生类时抛出数据类型不匹配异常
Student s = (Student)obj; //是学生类就强转
return this.name.equals(s.name) && this.age == s.age; //返回比较后的值
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public String toString(){ //重写toString方法
return name+"--"+age;
}
}
class MapTest
{
public static void main(String[] args)
{
Map<Student,String> map = new HashMap<Student,String>(); //定义泛型 接收对象为Student类和String
map.put(new Student("z3",20),"bj");
map.put(new Student("z2",21),"bj1");
map.put(new Student("z6",23),"bj3");
map.put(new Student("z4",21),"bj6");
map.put(new Student("z7",11),"bj1");
Set<Student> keyset = map.keySet(); //第一种获取方法 用key值获取value值
Iterator<Student> it = keyset.iterator();
while(it.hasNext())
{
Student s = it.next();
String addr = map.get(s);
System.out.println(s+"------"+addr);
}
Set<Map.Entry<Student,String>> entryset = map.entrySet(); //第二种获取方法 通过集合中的映射关系取出,Map.Entry类型
Iterator<Map.Entry<Student,String>> ite = entryset.iterator();
while(ite.hasNext()){
Map.Entry<Student,String> me = ite.next();
Student stu = me.getKey();
String value = me.getValue();
System.out.println(stu+"---"+value);
}
}
}
复制代码
作者:
z13561291
时间:
2015-11-17 12:54
第二种获取方法示意图[attach]93506[/attach]
作者:
z13561291
时间:
2015-11-17 13:03
第二种获取方法示意图。。。图片不会插入 抱歉
QQ截图20151117125405.png
(78.37 KB, 下载次数: 56)
下载附件
2015-11-17 12:54 上传
作者:
唯我独赞mo
时间:
2015-11-17 15:59
又复习了一遍,感觉不错谢谢楼主分享
作者:
切比雪夫
时间:
2015-11-17 21:45
谢谢分享!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2