标签:算法 amp blog copyto turn 方式 目的 cas zha
1.Map概述
Map<K,V> Map集合类用于存储元素对(称作键和值),其中每个键映射到一个值,该接口描述了从不重复的键到值的映射。
2.Map子类
1).HashTable,底层是哈希表数据结构,不可以存入null键和null值,线程同步,JDK1.0效率低;
2).HashMap,底层是哈希表数据结构,可以存入null键和null值,不同步,JDK1.2效率高;
3).TreeMap,底层是二叉树数据结构,线程不同步,可以用于Map键排序.
3.Map集合转为Set集合
1).Set<K>keySet(),将所有的键存入Set集合,再使用迭代器获取value值
2).Set<Map.Entry<K,V>>entrySet(), entrySet()方法返回一个实现Map.Entry接口的Set集合,集合中存放的是键/值对应关系,该关系是Map.Entry型。其中Entry是Map接口的内部接口。
Map.Entry提供的方法:
A)getKey(): 返回条目的关键字
B)getValue(): 返回条目的值
4.Map常见操作
1).添加操作:
V put(K key, V value):如果key已存在,在关联后,返回替换前该key对应的value值,如果不存在,则返回null;
void putAll(Map t):将来自特定映像的所有元素添加给该映像.
2).删除操作:
V remove(Object key):从此映射中移除指定键的映射关系(如果存在),不存在则返回null;
void clear() :从此映射中移除所有映射关系.
3).查询操作:
V get(key): 获得与关键字key相关的值,并且返回与关键字key相关的对象,如果没有该关键字,则返回null;判断key是否存在,可以通过返回值是否等于null
boolean containsKey(key): 判断映像中是否存在关键字key;
boolean containsValue(Object value): 判断映像中是否存在值value;
int size(): 返回当前映像中映射的数量;
boolean isEmpty(): 判断映像中是否有任何映射.
Collection values():返回映像中所有value值的集,由于值多个,用Collection集合,对其操作可以使用Collection基本方法.
5.Map简单应用
- import java.util.*;
- class MapDemo
- {
- public static void main(String[] args)
- {
- Map<String, String> mp = new HashMap<String, String>();
- mp.put("02", "zhangsan");
- mp.put("01", "lisi");
- mp.put("04", "wangwu");
-
-
-
- Set<String> setmap = mp.keySet();
- Iterator<String> it = setmap.iterator();
- while(it.hasNext())
- {
- System.out.println(mp.get(it.next()));
- }
-
-
- Set<Map.Entry<String, String>> entrySet = mp.entrySet();
- Iterator<Map.Entry<String, String>> it2 = entrySet.iterator();
- while(it2.hasNext())
- {
- Map.Entry<String, String> e = it2.next();
- System.out.println(e.getKey() +":"+ e.getValue());
- }
- }
- }
- import java.util.*;
-
- class Student
- {
- private int age;
- private String name;
- Student(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- public int hashCode()
- {
- return name.hashCode()+age*17;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Student))
- throw new ClassCastException("error!");
- Student s = (Student)obj;
- return this.age==s.age && this.name.equals(s.name);
- }
- }
- class MapDemo2
- {
- public static void main(String[] args)
- {
- HashMap<Student, String> hm = new HashMap<Student, String>();
- hm.put(new Student("java02", 10), "Beijing");
- hm.put(new Student("java04", 40), "Wuhan");
- hm.put(new Student("java01", 4), "Zhengzhou");
-
- Set<Student> s1 = hm.keySet();
- Iterator<Student> it1 = s1.iterator();
- while(it1.hasNext())
- {
- Student t1 = it1.next();
- System.out.println(t1.getName()+" "+ t1.getAge() +" "+ hm.get(t1));
- }
-
- Set<Map.Entry<Student, String>> s2 = hm.entrySet();
- Iterator<Map.Entry<Student, String>> it2 = s2.iterator();
- while(it2.hasNext())
- {
- Map.Entry<Student, String> mp = it2.next();
- System.out.println(mp.getKey().getName()+".."+ mp.getKey().getAge() +".."+ mp.getValue());
- }
- }
- }
- import java.util.*;
-
- class MapDemo2
- {
- public static void main(String[] args)
- {
- String str = "abddkdsadjljasdak";
- char[] ch = str.toCharArray();
-
- TreeMap<Character, Integer> hm = new TreeMap<Character, Integer>(new Comp());
-
- for(int i=0; i<str.length(); i++)
- {
- if(hm.get(ch[i]) == null)
- hm.put(ch[i], 1);
- else
- hm.put(ch[i], hm.get(ch[i])+1);
- }
-
- Set<Character> s = hm.keySet();
- Iterator<Character> it = s.iterator();
- while(it.hasNext())
- {
- char c = it.next();
- System.out.print(c +"("+ hm.get(c) +") ");
- }
- }
- }
- class Comp implements Comparator<Character>
- {
- public int compare(Character c1, Character c2)
- {
- return c2.compareTo(c1);
- }
- }
- import java.util.*;
-
- class MapDemo2
- {
- public static void main(String[] args)
- {
- Comp comp = new Comp();
- TreeMap<String, TreeMap<String, String>> school = new TreeMap<String, TreeMap<String, String>>();
-
- TreeMap<String, String> room1 = new TreeMap<String, String>(comp);
- TreeMap<String, String> room2 = new TreeMap<String, String>(comp);
- TreeMap<String, String> room3 = new TreeMap<String, String>(comp);
-
- school.put("class01", room1);
- school.put("class02", room2);
- school.put("class03", room3);
-
- room1.put("04","zhang");
- room1.put("02","li");
-
- room2.put("01","liu");
- room2.put("02","zhang");
-
- room3.put("07","wang");
- room3.put("03","zhao");
-
- Iterator<String> it1 = school.keySet().iterator();
- while(it1.hasNext())
- {
- String className = it1.next();
- TreeMap<String, String> c = school.get(className);
-
- System.out.println(className +"....");
- Iterator<String> it2 = c.keySet().iterator();
- while(it2.hasNext())
- {
- String studentId = it2.next();
- System.out.println(studentId +","+ c.get(studentId));
- }
- }
- }
- }
- class Comp implements Comparator<String>
- {
- public int compare(String s1, String s2)
- {
- return s2.compareTo(s1);
- }
- }
Java_Map_Map详解
标签:算法 amp blog copyto turn 方式 目的 cas zha
原文地址:http://www.cnblogs.com/hanfeihanfei/p/6123344.html