Map集合的使用与详解
例如,如果我们声明一个Map类型的变量,那么这个Map集合只能存储键为String类型、值为Integer类型的元素,任何类型不匹配的操作都将在编译时被检测到,避免在运行时出现类型转换错误。1.Java中的集合都支持泛型,泛型的作用是指定集合中要存储的元素类型,通过使用泛型可以保证集合中只能存储指定类型的元素,避免了类型转换带来的风险和错误,增加了代码的可读性、可维护性和代码的安全性。总的来说,J
一.Map集合的使用与详解

1.Map集合的特点以及遍历方式
1.map集合的特点
1.Map中的数据是以键值对(key-value)的形式存储的,其中每个键都是2.唯一的,对应一个值。
3.Map中的键和值可以为任意对象,包括基本类型和自定义类型。
4.Map是一个无序的集合,值之间没有任何顺序关系,但是可以根据键来进行排序。
5.Map中的元素数量是可以动态变化的,可以进行添加、删除、修改操作。
2.map集合的遍历方式
1.使用for-each循环遍历Map集合
可以使用Map接口的entrySet()方法获取Map集合中所有的键值对,然后使用for-each循环遍历这些键值对。例如:
package com.niyin.map;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author 匿隐
*使用map集合遍历
*/
public class niyinmap {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
for(Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " ----" + entry.getValue());
}
}
}
运行结果
2.使用Iterator迭代器遍历Map集合
可以使用Map接口的entrySet()方法获取Map集合中所有的键值对,然后使用Iterator迭代器遍历这些键值对。例如:
package com.niyin.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
*
* @author 匿隐
*使用map集合遍历
*/
public class niyinmap {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
// for(Map.Entry<String, Integer> entry : map.entrySet()) {
//// System.out.println(entry.getKey() + " ----" + entry.getValue());
// }
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
System.out.println(entry.getKey() + " ---- " + entry.getValue());
}
}
}
运行结果
3.遍历Map集合中的所有键或所有值
可以使用Map接口的keySet()方法获取Map集合中所有的键,或使用values()方法获取Map集合中所有的值。例如:
package com.niyin.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
*
* @author 匿隐
*使用map集合遍历
*/
public class niyinmap {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
// for(Map.Entry<String, Integer> entry : map.entrySet()) {
//// System.out.println(entry.getKey() + " ----" + entry.getValue());
// }
//
// Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
// while(iterator.hasNext()) {
// Map.Entry<String, Integer> entry = iterator.next();
// System.out.println(entry.getKey() + " -> " + entry.getValue());
// }
for(String key : map.keySet()) {
System.out.println(key + "-----" + map.get(key));
}
for(Integer value : map.values()) {
System.out.println(value);
}
}
}
运行结果
2.Map集合的综合应用
1.用Map集合判断字符串里的字符出现了几次
package com.niyin.map;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author 匿隐
*使用map集合判断,字符串里的字符出现了几次
*/
public class niyinmap1 {
public static void main(String[] args) {
String str="ytjghjghgfjghjdgtnzhjkbjas";
char[] charArray = str.toCharArray();
Map<Character, Integer>map=new HashMap<>();
// System.out.println(charArray);
for (char c : charArray) {
// System.out.println(c);
Integer integer = map.get(c);
if (integer!=null) {
map.put(c, integer+1);
}else {
map.put(c, 1);
}
}
for (Map.Entry<Character,Integer> entry: map.entrySet() ) {
System.out.println(entry.getKey()+"出现了"+entry.getValue()+"次");
}
}
}
运行结果
3.泛型的应用
1.为什么要使用泛型?
1.Java中的集合都支持泛型,泛型的作用是指定集合中要存储的元素类型,通过使用泛型可以保证集合中只能存储指定类型的元素,避免了类型转换带来的风险和错误,增加了代码的可读性、可维护性和代码的安全性。
2.对于Map集合来说,通过泛型可以指定键和值的类型,这样可以在编译的时候发现类型不匹配的错误。例如,如果我们声明一个Map<String, Integer>类型的变量,那么这个Map集合只能存储键为String类型、值为Integer类型的元素,任何类型不匹配的操作都将在编译时被检测到,避免在运行时出现类型转换错误。
使用泛型还可以使代码更加简洁和可读,因为在代码中不需要进行显式的类型转换操作,而是直接使用指定的类型即可。
3.
总的来说,Java中的泛型为集合的类型安全性提供了支持,并提高了代码的可读性和可维护性。
4.集合工具的使用
1.用集合工具排序集合
package com.niyin.map;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
*
* @author 匿隐
*集合工具
*/
public class niyin2map2 {
public static void main(String[] args) {
int[] intArr= {3,5,6,7,4};
Arrays.sort(intArr);
System.out.println(intArr);
String string = Arrays.toString(intArr);
System.out.println(string);
List<int[]> asList = Arrays.asList(intArr);
}
}
运行结果
2.用集合工具排序之Collections
package com.niyin.map;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
*
* @author 匿隐
*集合工具
*/
public class niyin2map2 {
//public static void main(String[] args) {
//int[] intArr= {3,5,6,7,4};
//Arrays.sort(intArr);
//
// System.out.println(intArr);
//
// String string = Arrays.toString(intArr);
// System.out.println(string);
// List<int[]> asList = Arrays.asList(intArr);
//
//}
public static void main(String[] args) {
List<String>list=new ArrayList<String>();
list.add("c");
list.add("a");
list.add("b");
list.add("d");
list.add("e");
System.out.println("排序前");
System.out.println(list);
Collections.sort(list);
System.out.println("排序后");
System.out.println(list);
}
}
运行结果
制作不易,望多鼓励!
更多推荐
所有评论(0)