Java 集合 —— Map 集合,Java 视频教程
for(String s : values){
System.out.println(s);
}
//Set<Map.Entry<K,V>> entrySet()返回一个 Set 集合,包含 map 集合中全部的映射关系
Set<Map.Entry<String,String>> entrySet = map.entrySet();
for(Map.Entry<String,String> s : entrySet){
String key = s.getKey();
String value = s.getValue();
System.out.println(key+"="+value);
}
}
}
map 集合的遍历
package review.MapDemo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class demo4 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("孙悟空","金箍棒");
map.put("唐僧","袈裟");
map.put("八戒","九齿钉耙");
map.put("沙僧","行李");
/*
借助 Set<K> keySet()遍历
1、获取 Map 集合中所有映射的键的 Set 集合
2、遍历键的集合,根据每一个键获取对应的值
*/
//方式一,用增强 for 循环
Set<String> keys = map.keySet();
for(String s : keys){
String key = s;
String value = map.get(s);
System.out.println(key+"="+value);
}
//方式二,用迭代器
//为 Set 集合创建一个迭代器 Iterator
Set<String> itkeys = map.keySet();
Iterator<String> it = itkeys.iterator();
while(it.hasNext()){
String key = it.next();
String value = map.get(key);
System.out.println(key+"="+value);
}
}
}
package review.MapDemo;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class demo5 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("孙悟空","金箍棒");
map.put("唐僧","袈裟");
map.put("八戒","九齿钉耙");
map.put("沙僧","行李");
/*
通过集合中每个键值对(Entry)对象,获取键值对(Entry)对象中的键与值。
1、获取所有的键值对的集合
2、遍历包含所有键值对的 Set 集合,得到每一个键值对对象
3、根据获取到的每一个键值对,来获取键和值
*/
//方式一、增强 for 循环
Set<Map.Entry<String,String>> entrySet = map.entrySet();
for(Map.Entry<String,String> s : entrySet){
String key = s.getKey();
String value = s.getValue();
System.out.println(key+"="+value);
}
//方式二、迭代器遍历
Set<Map.Entry<String,String>> entrySet1 = map.entrySet();
Iterator<Map.Entry<String,String>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<String,String> entry = it.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
}
结果同上面一样
当自定义类的对象传入 map 集合时,应该重写 equals 和 hashcode 方法
具体实现我在之前的博客写过了
集合List和Map——重写equals和hashCode方法(集合元素是类的对象)
这里举一个简单的例子
package review.MapDemo;
import java.util.Objects;
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);
}
}
package review.MapDemo;
import java.util.HashMap;
import java.util.Set;
public class demo6 {
public static void main(String[] args) {
HashMap<Student,Integer> map = new HashMap<>();
Student s1 = new Student("zhang",12);
Student s2 = new Student("zhou",14);
Student s3 = new Student("zhang",12);
map.put(s1,1);
map.put(s2,2);
map.put(s3,3);
Set<Student> keys = map.keySet();
for(Student s : keys){
String name = s.getName();
int age = s.getAge();
System.out.println(name+"---"+age);
}
}
}
LinkedHashMap
LinkedHashMap
实现了 Map 接口,底层是依赖于哈希表和链表的,具有可预知的遍历顺序
哈希表保证唯一性,保证的是 Key 的唯一性
链表保证有序,保证的是键的有序(存储和取出顺序一致)
package review.MapDemo;
import java.util.LinkedHashMap;
import java.util.Set;
public class LinkedHashMapDemp1 {
public static void main(String[] args) {
LinkedHashMap<String,String> map = new LinkedHashMap<>();
map.put("1","java");
map.put("3","hive");
map.put("2","hadoop");
Set<String> keys = map.keySet();
for(String s : keys){
String key = s;
String value = map.get(s);
System.out.println(key+"="+value);
}
}
}
TreeMap
HashMap 的 key 值是无序的,而实现了 SortedMap 接口的具体实现类 TreeMap 会对 key 值进行排序
TreeMap 底层基于红黑树
TreeMap 实现排序有两种方式
(一)传入的 key 值实现了 Comparable 接口(String,Integer 等都已经实现了 Comparable 接口,因此可以直接用)
(二)创建 TreeMap 集合的时候指定比较器 Comparator
例 1 key 值为 String 类型时
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class demo1 {
public static void main(String[] args) {
TreeMap<String,Integer> treeMap = new TreeMap<>();
treeMap.put("java",1);
treeMap.put("hive",2);
treeMap.put("flume",3);
Set<Map.Entry<String,Integer>> set = treeMap.entrySet();
for(Map.Entry<String,Integer> entry : set){
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
按照 key 值的首字母进行排序
例 2 key 值为自定义的类的时候
udent 实现 Comparable 接口的方式排序
package review.TreeMapDemo;
public class Student implements Comparable<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 +
'}';
}
public int compareTo(Student s){
//首先按照年龄进行排序
int i = this.age - s.age;
//然后判断是不是属性值一样,如果都一样则认为是一个对象,不会加入到集合中
int ii = i==0 ? this.name.compareTo(s.name) : i;
return ii;
}
}
package review.TreeMapDemo;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class demo2 {
public static void main(String[] args) {
TreeMap<Student,String> treeMap = new TreeMap<>();
Student s1 = new Student("zhang",16);
Student s2 = new Student("meng",30);
Student s3 = new Student("cao",16);
Student s4 = new Student("zhang",16);
treeMap.put(s1,"学生一");
treeMap.put(s2,"学生二");
treeMap.put(s3,"学生三");
treeMap.put(s4,"学生四");
Set<Map.Entry<Student,String>> set = treeMap.entrySet();
for(Map.Entry<Student,String> entry : set){
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
结果为
属性值相同的学生对象被去重成功,不同的学生对象按照年龄进行排序,相同年龄的学生继续按照姓名进行排序
package review.TreeMapDemo;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class demo3 {
public static void main(String[] args) {
TreeMap<Student,String> treeMap = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int i = o1.getAge() - o2.getAge();
int i2 = i==0 ? o1.getName().compareTo(o2.getName()) : i;
return i2;
}
});
Student s1 = new Student("zhang",16);
Student s2 = new Student("meng",30);
Student s3 = new Student("cao",16);
Student s4 = new Student("zhang",16);
treeMap.put(s1,"学生一");
treeMap.put(s2,"学生二");
treeMap.put(s3,"学生三");
treeMap.put(s4,"学生四");
Set<Map.Entry<Student,String>> set = treeMap.entrySet();
for(Map.Entry<Student,String> entry : set){
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
TreeMap 实例
需求:用键盘录入字符串,获取字符串中每一个字母出现的次数
输出格式为:a(1)b(2)c(3)
package review;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class StringDemo1 {
public static void main(String[] args) {
//接收键盘录入的字符串
Scanner sc = new Scanner(System.in);
String next = sc.next();
//将字符串转为字符数组
char[] c = next.toCharArray();
//创建一个 TreeMap,用这个 Treemap 的 key 值来存放字符,value 值来存放该字符出现的次数
评论