课后作业
已知数据手机号数组:
String[] phones = {“13804010040”,“13804010041”,“13804010042”,
“13804010043”,“13804010044”,“13804010045”,
“13804010046”,“13804010047”,“13804010044”,
“13804010054”,“13804010055”,“13804010056”,
“13804010058”,“13804010059”,“13804010060”,
“13804010061”,“13804010062”,“13804010063”,
“13804010064”,“13804010065”,“13804010066”,
“13804010067”,“13804010068”,“13804010069”,
“13804010070”,“13804010071”,“13804010072”,
“13804010073”,“13804010074”,“13804010075”,
“13804010045”,“13804010057”,“13804010063”,
“13804010058”,“13804010069”,“13804010066”}
1.使用集合,快速去除重复的手机号,将去重后手机号存
放的List集合中,输出去重后有多少个手机号,13804010099这个手机号是否存在
2.已知上面手机号数组,将数组转为集合,然后使用map集合存储每个手机号出现的次数,并遍历输出
public static void main(String[] args) {
String[] phones = {"13804010040", "13804010041", "13804010042",
"13804010043", "13804010044", "13804010045",
"13804010046", "13804010047", "13804010044",
"13804010054", "13804010055", "13804010056",
"13804010058", "13804010059", "13804010060",
"13804010061", "13804010062", "13804010063",
"13804010064", "13804010065", "13804010066",
"13804010067", "13804010068", "13804010069",
"13804010070", "13804010071", "13804010072",
"13804010073", "13804010074", "13804010075",
"13804010045", "13804010057", "13804010063",
"13804010058", "13804010069", "13804010066"};
// 去重,转变为list集合
List<String> listPhones = Arrays.stream(phones).distinct().collect(Collectors.toList());
// 遍历list集合
listPhones.forEach(one -> System.out.print(one + " "));
System.out.println();
// 手机号是否存在
if (listPhones.contains("13804010099")) {
System.out.println("13804010099存在!");
} else {
System.out.println("13804010099不存在!");
}
// 每个手机号出现的次数
System.out.println("第二题:");
Map<String, Integer> hashMap = new HashMap<>();
for (String phone : phones) {
if (hashMap.containsKey(phone)) {
hashMap.put(phone, hashMap.get(phone) + 1);
} else {
hashMap.put(phone, 1);
}
}
hashMap.forEach((key, value) -> System.out.println(key + "的次数是:" + value));
}