javaseday18补充(Map entry 内部接口实现获取元素)

/*
 * Map一次添加一对元素 Collection 一次添加一个元素
 * Map也称为双列集合 Collection集合称为单列集合
 * 其实Map集合中存储的就是键值对
 * map集合必须保证键的唯一性
 *
 * 常用方法
 * 1、添加
 *  value put (key,value) 返回前一个和key关联的值 如果没有返回null
 * 2、删除
 * void clear() 清空集合
 * value remove (key) 根据指定的key删除这个键值对
 *
 * 3、判断
 * boolean containsKey(key);
 * boolean containsValue(value);
 * boolean isEmpty();
 * 4、获取
 *  value get(key) 通过键获取值 如果没有该键返回null
 *   当然可以通过返回null 来判断是否包含指定键
 * int size() 获取键值对的个数
 *
 *
 *
 */
public class MapDemo01 {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<Integer, String>();
method(map);
}


public static void method(Map<Integer,String> map){


//添加元素
System.out.println(map.put(8, "wangcai"));//null 之前没有
//存相同键 值会覆盖
System.out.println(map.put(8, "dabai"));//返回之前拥有这个主键的人
map.put(2, "das");
map.put(5, "waha"); //hash表 无序


//删除
// System.out.println("remove"+map.remove(2));


//判断
System.out.println("contains:"+map.containsKey(8));


//获取
System.out.println("get:"+map.get(8));
System.out.println(map);




}

}


/*
 * 视图 可视化的映射关系 存储了一种结构
 *  返回(拿到)一种结构
 *
 */
public class MapDemo02 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
method(map);
}


public static void method(Map<Integer,String> map){


map.put(8, "wangcai");
map.put(2, "dabai");
map.put(3, "xiaoqiang");
map.put(4, "xiaohua");
map.put(6, "hehe");


//取出map中的所有元素
//原理 通过keySet方法获取map中所有的键所在的Set集合 再通过Set的迭代器获取到每一个键
//再对每一个键通过map集合的get方法获取其对应的值即可


Set<Integer> keySet = map.keySet();
Iterator<Integer> it = keySet.iterator();


while(it.hasNext()){
Integer key = it.next();
String value = map.get(key);
System.out.println(key+"..."+value);
}
}
}

javaseday18补充(Map entry 内部接口实现获取元素)

另外一中迭代方式

/*
* 通过Map转成set就可以迭代
* 找到了另一个方法 entrySet
* 该方法将键和值的映射关系存储到了Set集合中而这个映射关系的类型就是Map.Entry类型(结婚证)
*
*/


Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
Iterator<Map.Entry<Integer, String>> it = entrySet.iterator();


while(it.hasNext()){
Map.Entry<Integer, String> me = it.next();
Integer key = me.getKey();
String value = me.getValue();


System.out.println(key+"..."+value);
}

javaseday18补充(Map entry 内部接口实现获取元素)

interface MyMap{
public static interface MyEntry{//接口内部接口 是个静态的成员
//MyEntry 是 包含键和值内容 是键和值的映射关系对象 只有Map 的映射关系先存在了 entry 才能访问Map的映射关系 所以是内部
}
}

elipse 右边是提示缩略条

还有一种获取属性值的方法

Collection<String> values = map.values();
Iterator<String> it  = values.iterator();
while(it.hasNext()){
System.out.println(it.next());
}




/*
 * Map常用的子类
 * Hashtable 内部结构是哈希表 同步的 不允许null作为键 null作为值前期出现的 后期出现的后缀都他爹的名字
 * properties 用来存储键值对型的配置文件的信息 可以和IO技术相结合
 *
 * HashMap 内部结构是哈希表 不同步 允许null作为键 null作为值
 * TreeMap 内部结构是二叉树不同步 可以对Map集合中的键进行排序
 *
 */


public class HashMapDemo03 {
public static void main(String[] args) {
/*
* 将学生对象和学生的归属地通过键与值存储到map集合中
* 一旦事物变复杂就封装对象  比如 市县 村具体信息就封装
*
*/
HashMap <Student,String> hm = new HashMap<Student,String>();


hm.put(new Student("lisi",12),"北京" );
hm.put(new Student("zhangsan",16),"上海" );
hm.put(new Student("xiaoqiang",15),"大连" );
hm.put(new Student("xiaohua",18),"北大" );
hm.put(new Student("zhangsan",16),"如果" );//Hash 需要有equals 和Hashcoede 方法来进行判断重复




Set<Student> keySet = hm.keySet();


Iterator<Student> it = keySet.iterator();
// Iterator<Student> it2 = hm.keySet().iterator(); //上面两句的合并
while(it.hasNext()){
Student key = it.next();
String value = hm.get(key);
System.out.println(key.getName()+"..."+key.getAge()+value);
}


}
}

public static void main(String[] args) {
TreeMap <Student,String> tm = new TreeMap<Student,String>(new ComparatorByName());


tm.put(new Student("lisi",12),"北京" );
tm.put(new Student("zhangsan",16),"上海" );
tm.put(new Student("xiaoqiang",15),"大连" );
tm.put(new Student("xiaohua",18),"北大" );
tm.put(new Student("zhangsan",16),"如果" );//Hash 需要有equals 和Hashcoede 方法来进行判断重复




Iterator<Map.Entry<Student, String>> it = tm.entrySet().iterator(); 
while(it.hasNext()){
Map.Entry<Student, String> me = it.next();
Student key = me.getKey();
String value = me.getValue();
System.out.println(key.getName()+"..."+key.getAge()+value);
}
}