Map集合的第一种遍历方式
public class Map的遍历方式1 {
public static void main(String[] args) {
//创建集合
Map<String,String> map = new HashMap<>();
//添加对象
map.put("11c","neverstop");
map.put("hpy","keptgoing");
map.put("lzh","selfcontrol");
//将键放入单列集合
Set<String> set = map.keySet();
//增强for循环遍历
for (String s : set) {
String value = map.get(s);
System.out.println(s + " = " + value);
}
//迭代器遍历
//创建迭代器对象
Iterator<String> it = set.iterator();
while(it.hasNext()){
String key = it.next();
String value = map.get(key);
System.out.println(key + " = " + value);
}
//foreach遍历
set.forEach(new Consumer<String>() {
@Override
public void accept(String key) {
String value = map.get(key);
System.out.println(key + " = " + value);
}
});
}
}
第一种遍历方式其实是将键放入单列集合中,遍历,利用map.get方法返回value的值。
foreach循环可以再写的简便一点的,偷懒了哈哈。
Map集合的第二种遍历方式
public class Map的遍历方式2 {
public static void main(String[] args) {
//创建集合
Map<String, String> map = new HashMap<>();
//添加对象
map.put("11c", "neverstop");
map.put("hpy", "keptgoing");
map.put("lzh", "selfcontrol");
//获得全部的entry对象
Set<Map.Entry<String, String>> entries = map.entrySet();
//增强for遍历
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " = " + value);
}
//迭代器遍历
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
System.out.println("迭代器遍历");
while (iterator.hasNext()){
Map.Entry<String, String> next = iterator.next();
System.out.println( next.getKey() + " = " +next.getValue());
}
//foreach
entries.forEach(new Consumer<Map.Entry<String, String>>() {
@Override
public void accept(Map.Entry<String, String> stringStringEntry) {
String key = stringStringEntry.getKey();
String value = stringStringEntry.getValue();
System.out.println(key + " = " + value);
}
});
}
}
第二种遍历方式获得entry的对象,遍历可以得到key和value的值,输出就好。
Map集合的第三种遍历方式
public class MapLambda表达式遍历 {
public static void main(String[] args) {
//创建集合
Map<String,String> map = new HashMap<>();
//添加对象
map.put("11c","neverstop");
map.put("hpy","keptgoing");
map.put("lzh","selfcontrol");
//简化Lambda表达式遍历
//底层是增强for循环
map.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String key, String value) {
System.out.println(key + " =" + value );
}
});
//简化版
System.out.println("------------------------");
map.forEach((key , value) -> System.out.println(key + " =" + value ));
}
}
这种方式遍历的底层原理是就是加强for循环
//获得全部的entry对象
//Set<Map.Entry<String, String>> entries = map.entrySet();
//增强for遍历
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " = " + value);
}
与第二种遍历方式是一样的,但是不同的是这个将map.entrySet()对象直接放入for循环。
小练习
统计投票数,得出投票数最多的景点。
public class HashMap统计投票 {
public static void main(String[] args) {
//创建数组
String[] arr = {"a","b","c","d"};
//创建集合,得到投票次数
ArrayList<String> list = new ArrayList<>();
//创建Random对象
Random random = new Random();
for (int i = 0; i < 80; i++) {
int index = random.nextInt(arr.length);
list.add(arr[index]);
}
//创建HashMap集合
HashMap<String,Integer> hm = new HashMap<>();
for (String name : list) {
//如果map集合中存有该key,则count++表示投票数加一,重新添加覆盖原来的键值对。
if (hm.containsKey(name)){
Integer count = hm.get(name);
count++;
hm.put(name,count);
}else {
//不存在,说明是第一次出现,就添加到map集合中,初始值为1
hm.put(name,1);
}
}
System.out.println(hm);
//找出最大值
//因为随机数,有小概率某个景点的次数为0。所以0作为参照
int max = 0 ;
//遍历找出最大值
for (Map.Entry<String, Integer> entry : hm.entrySet()) {
if (entry.getValue() >= max)
max = entry.getValue();
}
System.out.println(max);
//遍历打印出投票数最多的景点
for (Map.Entry<String, Integer> entry : hm.entrySet()) {
if (entry.getValue() == max)
System.out.println(entry.getKey());
}
}
}
运行结果
{a=23, b=13, c=23, d=21}
23
a
c
先创建一个数组,存放景点信息,因为不确定景点数量或者甚至是自定义的,所以我用数组存放,创建ArrayList集合放同学们的投票数据,(之前我没做这道题的时候,还以为要创建很多个学生对象)将投票数据放入双列集合中,key为景点,value为次数。这就是大体思路
创建random对象生成随机数,比如说80个学生,那就遍历80次,制造索引数字,利用nextInt方法里面以arr数组的长度(也就是4)为条件随机生成索引数字,并且添加到list集合中。
双列集合做一个判断是否包含键key,如果包含就把count++,在put到集合中,之前的值会被覆盖,如果没有这个键,会被put到集合中,初始值value被设置为1。
大家如果有疑问的话,在评论留言。如果看到的话我会解答。