目录
1.Map集合的特点
2.Map的遍历方式
3.HashMap的使用
4.泛型
5.集合框架工具类
首先上思维导图
一,Map的特点
1.每一个键对应一个值
2.每一个键都是唯一的
3.键可以为null
4.可以增删改查
如下代码:
package com.whw.map;
import java.util.HashMap;
import java.util.Map;
/**
* map集合的特点
*
* @author zjjt
*
*/
public class Dome01 {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
// 增加
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put(null, 4);// 键可以为null
System.out.println(map);// 查询
// 删除
System.out.println(map.remove("b"));// 输出删除的键对值
System.out.println(map);
// 修改
map.put("c", 5);
System.out.println(map);
}
}
输出结果:
二,Map的遍历方式
1.先获得map集合中所有的key(键),再使用foreach遍历
2.先获得map集合的映射关系,再使用foreach遍历
如下代码:
package com.whw.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Dome02 {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
// 先获得map集合中所有的key(键)
Set<String> keys = map.keySet();
// foreach遍历
for (String key : keys) {
System.out.println("key=" + key + ",value=" + map.get(key));
}
System.out.println("--------------------------------------");
// 先获得map集合的映射关系
Set<Entry<String, Object>> entrySet = map.entrySet();
// foreach遍历
for (Entry<String, Object> entry : entrySet) {
System.out.println("key=" + entry.getKey() + ",value=" + entry.getValue());
}
}
}
输出结果:
三,HashMap的使用
举个例子:求统计字符串中每个字符的出现次数
如下代码:
package com.whw.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* 统计字符串中每个字符的出现次数 1.字符串转换成字符数组 2.遍历数组,将字符放进map集合中
* 3.判断:如果该字符没有,那就给字符对应的值设置为1;如果有,就给字符对应的值+1
*
* @author zjjt
*
*/
public class Dome03 {
public static void main(String[] args) {
String s = "sslkfdgafkdaflfhdkgfjkdhadhdfhlaklfhahdfkgkdfalaksdkjdhfhdkjsfljfdafhfal";
char[] chars = s.toCharArray();// 转换成字符数组
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : chars) {
Integer num = map.get(c);// 获得该字符对应的值
if (num == null || num == 0) {// 判断值是否为空
map.put(c, 1);// 为空则设置为1
} else {
map.put(c, num + 1);// 不为空则给原来的值+1
}
}
// 遍历
Set<Entry<Character, Integer>> entrySet = map.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
System.out.println(entry.getKey() + "出现了:" + entry.getValue() + "次");
}
}
}
输出结果:
四,泛型
泛型的作用:
1.将运行时产生的异常转换为编译期的错误
2.提高代码的健壮性
废话不多说,我们来看代码展示:
package com.whw.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Dome04 {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 4);
map.put("e", "xxx");
// 筛选出value为偶数的key
Set<Entry<String, Object>> entrySet = map.entrySet();
for (Entry<String, Object> entry : entrySet) {
Object v1 = entry.getValue();
Integer v2 = (Integer) v1;
if (v2 % 2 == 0) {
System.out.println("偶数值对应的键为:"+entry.getKey());
}
}
}
}
没有使用泛型的运行结果:
使用泛型
将下面这三行的object修改成integer
修改后---->
则会在编译期就给出红色的错误提醒
五,集合框架工具类
1.使用Collections
首先上代码:
package com.whw.map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Dome05 {
public static void main(String[] args) {
List<Students> list = new ArrayList<>();
list.add(new Students("aa", 22));
list.add(new Students("bb", 33));
list.add(new Students("cc", 11));
list.add(new Students("dd", 44));
Collections.sort(list,new Comparator<Students>() {
@Override
public int compare(Students o1, Students o2) {
// TODO Auto-generated method stub
return o1.getAge()-o2.getAge();//根据年龄来排序
}
});
for (Object s : list) {
System.out.println(s);
}
}
}
class Students{
private String name;
private int 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 "Students [name=" + name + ", age=" + age + "]";
}
public Students(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
输出结果:
因为使用了比较器,所以输出结果是按年龄从小到大输出
ArrayList默认是添加进来的顺序输出
2.使用Arrays
输出结果:
到这里就结束了,有不对的地方希望指出,谢谢大家!