Map
LinkedHashMap<K,V>:HashMap下有个子类LinkedHashMap,存储数据采用的哈希表结构+链表结构。通过链表结构可以保证元素的存取顺序一致;通过哈希表结构可以保证的键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。
tips:Map接口中的集合都有两个泛型变量<K,V>,在使用时,要为两个泛型变量赋予数据类型。两个泛型变量<K,V>的数据类型可以相同,也可以不同。
-
-
public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。 -
public V get(Object key)根据指定的键,在Map集合中获取对应的值。 -
boolean containsKey(Object key)判断集合中是否包含指定的键。 -
public Set<K> keySet(): 获取Map集合中所有的键,存储到Set集合中。 -
public Set<Map.Entry<K,V>> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。
使用put方法时,若指定的键(key)在集合中没有,则没有这个键对应的值,返回null,并把指定的键值添加到集合中;
若指定的键(key)在集合中存在,则返回值为集合中键对应的值(该值为替换前的值),并把指定键所对应的值,替换成指定的新值。
1 public class MapDemo { 2 public static void main(String[] args) { 3 //创建 map对象 4 HashMap<String, String> map = new HashMap<String, String>(); 5 6 //添加元素到集合 7 map.put("黄晓明", "杨颖"); 8 map.put("文章", "马伊琍"); 9 map.put("邓超", "孙俪"); 10 System.out.println(map); 11 12 //String remove(String key) 13 System.out.println(map.remove("邓超")); 14 System.out.println(map); 15 16 // 想要查看 黄晓明对应的 是谁 17 System.out.println(map.get("黄晓明")); 18 System.out.println(map.get("邓超")); 19 } 20 }
分析步骤:
-
获取Map中所有的键,由于键是唯一的,所以返回一个Set集合存储所有的键。方法提示:
keyset() -
遍历键的Set集合,得到每一个键。
根据键,获取键所对应的值。方法提示:
-
public K getKey():获取Entry对象中的键。 -
public V getValue():获取Entry对象中的值。
在Map集合中也提供了获取所有Entry对象的方法:
-
public Set<Map.Entry<K,V>> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。
1 public class MapDemo01 { 2 public static void main(String[] args) { 3 //创建Map集合对象 4 HashMap<String, String> map = new HashMap<String,String>(); 5 //添加元素到集合 6 map.put("胡歌", "霍建华"); 7 map.put("郭德纲", "于谦"); 8 map.put("薛之谦", "大张伟"); 9 10 //获取所有的键 获取键集 11 Set<String> keys = map.keySet(); 12 // 遍历键集 得到 每一个键 13 for (String key : keys) { 14 //key 就是键 15 //获取对应值 16 String value = map.get(key); 17 System.out.println(key+"的CP是:"+value); 18 } 19 } 20 }
操作步骤与图解:
-
获取Map集合中,所有的键值对(Entry)对象,以Set集合形式返回。方法提示:
entrySet()。 -
遍历包含键值对(Entry)对象的Set集合,得到每一个键值对(Entry)对象。
-
通过键值对(Entry)对象,获取Entry对象中的键与值。 方法提示:
getkey() getValue()
1 public class MapDemo02 { 2 public static void main(String[] args) { 3 // 创建Map集合对象 4 HashMap<String, String> map = new HashMap<String,String>(); 5 // 添加元素到集合 6 map.put("胡歌", "霍建华"); 7 map.put("郭德纲", "于谦"); 8 map.put("薛之谦", "大张伟"); 9 10 // 获取 所有的 entry对象 entrySet 11 Set<Entry<String,String>> entrySet = map.entrySet(); 12 13 // 遍历得到每一个entry对象 14 for (Entry<String, String> entry : entrySet) { 15 // 解析 16 String key = entry.getKey(); 17 String value = entry.getValue(); 18 System.out.println(key+"的CP是:"+value); 19 } 20 } 21 }
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); } }
编写测试类:
1 public class HashMapTest { 2 public static void main(String[] args) { 3 //1,创建Hashmap集合对象。 4 Map<Student,String>map = new HashMap<Student,String>(); 5 //2,添加元素。 6 map.put(newStudent("lisi",28), "上海"); 7 map.put(newStudent("wangwu",22), "北京"); 8 map.put(newStudent("zhaoliu",24), "成都"); 9 map.put(newStudent("zhouqi",25), "广州"); 10 map.put(newStudent("wangwu",22), "南京"); 11 12 //3,取出元素。键找值方式 13 Set<Student>keySet = map.keySet(); 14 for(Student key: keySet){ 15 Stringvalue = map.get(key); 16 System.out.println(key.toString()+"....."+value); 17 } 18 } 19 }
-
-
如果要保证map中存放的key和取出的顺序一致,可以使用
java.util.LinkedHashMap集合来存放。
public class LinkedHashMapDemo { public static void main(String[] args) { LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); map.put("张三", "zhangsan"); map.put("李四", "lisi"); map.put("王五", "wangwu"); Set<Entry<String, String>> entrySet = map.entrySet(); for (Entry<String, String> entry : entrySet) { System.out.println(entry.getKey() + " " + entry.getValue()); } } }
结果:
张三 zhangsan
李四 lisi
王五 wangwu
计算一个字符串中每个字符出现次数。
分析:
-
获取一个字符串对象
-
创建一个Map集合,键代表字符,值代表次数。
-
遍历字符串得到每个字符。
-
判断Map中是否有该键。
-
如果没有,第一次出现,存储次数为1;如果有,则说明已经出现过,获取到对应的值进行++,再次存储。
-
打印最终结果
public class MapTest { public static void main(String[] args) { //友情提示 System.out.println("请录入一个字符串:"); String line = new Scanner(System.in).nextLine(); // 定义 每个字符出现次数的方法 findChar(line); } private static void findChar(String line) { //1:创建一个集合 存储 字符 以及其出现的次数 HashMap<Character, Integer> map = new HashMap<Character, Integer>(); //2:遍历字符串 for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); //判断 该字符 是否在键集中 if (!map.containsKey(c)) {//说明这个字符没有出现过 //那就是第一次 map.put(c, 1); } else { //先获取之前的次数 Integer count = map.get(c); //count++; //再次存入 更新 map.put(c, ++count); } } System.out.println(map); } }
浙公网安备 33010602011771号