package MapTest;
import java.util.*;
/*
* java.util.Map接口中常用的方法:
* 1、Map和Collection没有继承关系
* 2、Map集合以key和value的方式存储数据:键值对
* key和value都是引用数据类型
* Key和value都是存储对象的内存地址
* Key起到主导的地位,value是Key的一个附属品
* 3、Map中常用的方法:
* void clear() 清空Map集合
* boolean containsKey(Object key) 判断Map中是否包含某个Key
* boolean containsValue(Object value) 判断Map中是否包含某个value
* boolean isEmpty() 判断Map集合中元素个数是否为0
*
* V put(K key, V value) 添加键值对
* V get(Object key) 通过key获取value
* V remove(Object key) 通过key删除键值对
* int size() 获取Map集合中键值对的个数
* Collection<V> values() 获取Map集合中所有的value,返回一个collection
*
* Set<K> keySet() 获取Map中所有的key(所有的键是Set集合)
* Set<Map.Entry<K,V>> entrySet() 将Map集合转换成Set集合
* 将Map集合转换成Set集合
* 假设现在有一个Map集合,如下:
* map1集合对象:
* key value
* ------------------
* 1 zhangsan
* 2 lisi
* 3 wangwu
* 4 zhaoliu
*
* Set set = map1.entrySet();
* set集合对象
* 1=zhangsan 注意:Map集合通过entrySet()方法转换成的这个set集合,Set集合中的元素类型是Map.Entry<k,V>
* 2=lisi Map.Entry和String一样,都是一种类型的名字,只不过Map.Entry是静态内部类,是Map中静态内部类
* 3=wangwu
* 4=zhaoliu
* 这个东西是什么?Map.Entry
* */
public class MapTest01 {
public static void main(String[] args) {
//给一个set集合,该集合存储的对象:Myclass.InnerClass类型
Set<Myclass.InnerClass> mi = new HashSet<>();
//存储的字符串对象
Set<String> ss = new HashSet<>();
//对比一下
Set<MyMap.MyEntry<Integer,String>> bb = new HashSet<>();
//Set<Map.Entry<K,V>> entrySet()
// V put(K key, V value) 添加键值对
Map<Integer,String> is = new HashMap<>();
System.out.println("添加键值对");
is.put(1,"zhangsan"); //这里进行了自动装箱
is.put(2,"lisi");
is.put(3,"xiaoming");
is.put(4,"xiaohong");
//int size() 获取Map集合中键值对的个数
System.out.println("\n获取Map集合中键值对的个数:" + is.size());
//V get(Object key) 通过key获取value
System.out.println("\n通过key获取value:" + is.get(3));
//V remove(Object key) 通过key删除键值对
System.out.println("\n通过key删除键值对:" + is.remove(2));
System.out.println("\n获取Map集合中键值对的个数:" + is.size());
//注意:contains方法都是调用equals方法比较的,所以自定义的类型需要重写equals方法
//boolean containsKey(Object key) 判断Map中是否包含某个Key
System.out.println("\n判断Map中是否包含某个Key:" + is.containsValue(2));
//boolean containsValue(Object value) 判断Map中是否包含某个value
System.out.println("\n判断Map中是否包含某个value:" + is.containsValue("xiaohong"));
System.out.println(is.containsValue("xiaohong"));//String类重写了equals方法,只比较内容
//boolean isEmpty() 判断Map集合中元素个数是否为0
System.out.println("\n判断Map集合中元素个数是否为0:" + is.isEmpty());
//Collection<V> values() 获取Map集合中所有的value,返回一个collection
Collection<String> vaules = is.values();
System.out.println("\n获取Map集合中所有的value,返回一个collection:");
for(String aa : vaules){
System.out.print(aa + " ");
}
// void clear() 清空Map集合
is.clear();
System.out.println("\n\n清空Map集合");
System.out.println("获取集合的键值对个数: " + is.size());
}
}
class MyMap{
public static class MyEntry<K,V>{
}
}
package MapTest;
import java.util.*;
public class MapTest02 {
public static void main(String[] args) {
Map<Integer,String> it = new HashMap<>();
it.put(1,"hadf");
it.put(2,"asfd");
it.put(3,"afd");
it.put(4,"dsfg");
Set<Integer> key = it.keySet();
System.out.println("这个key直接输出的类型:" + key);
Iterator<Integer> aa = key.iterator();
System.out.println("迭代器方式");
while(aa.hasNext()){
Integer key01 = aa.next();
String value = it.get(key01);
System.out.println("key :" + key01 + " value :" +value);
}
System.out.println("foreach增强for循环遍历:");
Collection<String> data = it.values();
for(Integer key01 : key){
System.out.println("key :" + key01 + " value : " + it.get(key01));
}
Set<Map.Entry<Integer,String>> it2 = it.entrySet();
Iterator<Map.Entry<Integer,String>> ii = it2.iterator();
System.out.println("第二种方法之迭代器:");
while(ii.hasNext()){
Map.Entry<Integer,String> node = ii.next();
Integer aaa = node.getKey();
String bbb = node.getValue();
System.out.println("Key :" + aaa + " Value :" + bbb);
}
System.out.println("第二种方法之foreach:这种效率较高,适合大数据下");
for(Map.Entry<Integer,String> aaa : it2){
System.out.println("key :" + aaa.getKey() + " value: " + aaa.getValue());
}
}
}
package MapTest;
public class Myclass {
public class InnerClass{
public void doSome(){
System.out.println("哈哈哈哈");
}
}
}