1、Map
- Map集合概述和使用
- Map集合概述
- Interface Map
K:键的类型; V:值的类型 - 将键映射到值得对象;不能包含重复的键;每个键可以映射到最多一个值
- 例如:学生的学号和姓名
- 键 值
- 001 王小恒
- 002 王中恒
- 003 王大恒
- Interface Map
- Map集合概述
- 创建Map集合的对象
- 多态的方式
- 具体的实现类HashMap
Map集合的基本功能 | 方法名 | 说明 | | —- | —- | | V put(K key, V value) | 添加元素(键值对) | | V remove(Object key) | 根据键删除键值对元素 | | void clear() | 移除所有的键值对元素 | | boolean containsKey(Object key) | 判断集合是否包含指定的键 | | boolean containsValue(Object value) | 判断集合是否包含指定的值 | | boolean isEmpty() | 判断集合是否为空 | | int size() | 集合的长度,也就是集合中键值对的个数 |
Map集合的获取功能 | 方法名 | 说明 | | —- | —- | | V get(Object key) | 根据键获取值 | | Set
keySet() | 获取所有键的集合 | | Collection values() | 获取所有值的集合 | | Set > entrySet() | 获取所有键值对对象的集合 | Map集合的遍历
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("001", "王小恒");
map.put("002", "王中恒");
map.put("003", "王大恒");
//先获取键的集合
Set<String> keys = map.keySet();
//通过增强for遍历键的同时通过键 get()方法获取值
for(String s : keys) {
System.out.println(s + ", " + map.get(s));
}
}
}
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("001", "王小恒");
map.put("002", "王中恒");
map.put("003", "王大恒");
//通过entrySet方法先获得键值对对象
Set<Map.Entry<String, String>> entrySet = map.entrySet();
//通过键值对对象的getKey和getValue方法获得键和值
for(Map.Entry<String, String> me : entrySet) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key + ", " + value);
}
}
}
/*
HashMap集合存储学生对象并遍历
键是学号(String),值是学生对象(Student)
*/
public class MapDemo {
public static void main(String[] args) {
//创建Map对象
Map<String,Student> map = new HashMap<>();
//创建学生对象
Student s1 = new Student("王小恒",18);
Student s2 = new Student("王中恒",19);
Student s3 = new Student("王大恒",20);
map.put("001",s1);
map.put("002",s2);
map.put("003",s3);
//遍历方式1
Set<String> keySet = map.keySet();
for(String key : keySet) {
Student sValue = map.get(key);
System.out.println(key + ", " + sValue.getName() + ", " + sValue.getAge());
}
System.out.println("----------");
//遍历方式2
Set<Map.Entry<String, Student>> entrySet = map.entrySet();
for(Map.Entry<String, Student> me : entrySet) {
String key = me.getKey();
Student s = me.getValue();
System.out.println(key + ", " + s.getName() + ", " + s.getAge());
}
}
}
/*
HashMap集合存储学生对象并遍历
键是学生(Student),值是学生家庭住址(String)
*/
public class MapDemo {
public static void main(String[] args) {
//创建Map对象
Map<Student,String> map = new HashMap<>();
//创建学生对象
Student s1 = new Student("王小恒",18);
Student s2 = new Student("王中恒",19);
Student s3 = new Student("王大恒",20);
map.put(s1,"山东聊城");
map.put(s2,"贵州贵阳");
map.put(s3,"北京海淀");
//遍历方式1
Set<Student> keySet = map.keySet();
for(Student key : keySet) {
String value = map.get(key);
System.out.println(key.getName() + ", " + key.getAge() + value);
}
System.out.println("----------");
//遍历方式2
Set<Map.Entry<Student, String>> entrySet = map.entrySet();
for(Map.Entry<Student, String> me : entrySet) {
Student key = me.getKey();
String addValue = me.getValue();
System.out.println(key.getName() + ", " + key.getAge() + ", " + addValue);
}
}
}
```java / 创建一个ArrayList集合,存储三个元素,每一个元素都是HashMap,每一个HashMap的键和值都是String,并遍历 / public class HashMapList { public static void main(String[] args) {
ArrayList<HashMap<String, String>> arr = new ArrayList<>();
HashMap<String, String> hm1 = new HashMap<>();
hm1.put("001", "王小恒");
hm1.put("002", "王中恒");
hm1.put("003", "王大恒");
HashMap<String, String> hm2 = new HashMap<>();
hm2.put("004", "王小恒");
hm2.put("005", "王中恒");
hm2.put("006", "王大恒");
HashMap<String, String> hm3 = new HashMap<>();
hm3.put("007", "王小恒");
hm3.put("008", "王中恒");
hm3.put("009", "王大恒");
arr.add(hm1);
arr.add(hm2);
arr.add(hm3);
Iterator<HashMap<String, String>> it = arr.iterator();
while (it.hasNext()) {
HashMap<String, String> hm = it.next();
Set<String> keySet = hm.keySet();
for(String key : keySet) {
String value = hm.get(key);
System.out.println(key + ", " + value);
}
}
} }
```java
/*
创建一个HashMap集合,存储三个键值对元素,每一个键值对元素的键是String,值是ArrayList,每一个ArrayList的元素是String,并遍历
*/
public class ArrayListMap {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> hm = new HashMap<>();
ArrayList<String> arrayList1 = new ArrayList<>();
arrayList1.add("王小恒");
arrayList1.add("王中恒");
arrayList1.add("王大恒");
ArrayList<String> arrayList2 = new ArrayList<>();
arrayList2.add("王小恒");
arrayList2.add("王中恒");
arrayList2.add("王大恒");
ArrayList<String> arrayList3 = new ArrayList<>();
arrayList3.add("王小恒");
arrayList3.add("王中恒");
arrayList3.add("王大恒");
hm.put("001", arrayList1);
hm.put("002", arrayList2);
hm.put("003", arrayList3);
Set<String> keySet = hm.keySet();
for(String key : keySet) {
ArrayList<String> arr = hm.get(key);
for(String s : arr) {
System.out.println(key + ", " + s);
}
}
}
}
统计字符串中每个字符出现的次数
-
/*
统计字符串中每个字符出现的次数
键盘录入一个字符串,要求统计字符串中每个字符串出现的次数
*/
public class CountChar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String input = sc.nextLine();
HashMap<Character, Integer> hm = new HashMap<>();
for(int i=0; i<input.length(); i++) {
char c = input.charAt(i);
if(hm.get(c) == null) {
hm.put(c, 1);
} else {
int value = hm.get(c);
hm.put(c, value+1);
}
}
//遍历并输出指定格式
Set<Character> keySet = hm.keySet();
for(Character c : keySet) {
int count = hm.get(c);
System.out.print(c + "(" + count + ")");
}
}
}
-