有一个Map集合里面存储的是学生的姓名和年龄,内容如下{赵四=21,王二=17,张三=18,小丫=25,李四=26,王五=38}(15分) * a.将里面的元素用两种遍历方式打印到控制台上 *
Posted 暴走灬青春
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有一个Map集合里面存储的是学生的姓名和年龄,内容如下{赵四=21,王二=17,张三=18,小丫=25,李四=26,王五=38}(15分) * a.将里面的元素用两种遍历方式打印到控制台上 *相关的知识,希望对你有一定的参考价值。
package com.heima.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyStore.Entry;
import java.util.HashMap;
import java.util.Map;
public class Test2
/**有一个Map集合里面存储的是学生的姓名和年龄,内容如下赵四=21,王二=17,张三=18,小丫=25,李四=26,王五=38(15分)
* a.将里面的元素用两种遍历方式打印到控制台上
* (5分)b.将小丫的年龄改成18(2分)
* c.将年龄大于24的学员姓名存入到当前工程目录student.txt中(8分)
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
HashMap<String, Integer> hm = new HashMap<String, Integer>();
hm.put("赵四", 21);
hm.put("王二", 17);
hm.put("张三", 18);
hm.put("小丫", 25);
hm.put("李四", 26);
hm.put("王五", 38);
System.out.println("第一种遍历:");
for (String key : hm.keySet())
System.out.println(key +"="+hm.get(key));
System.out.println("第二种遍历:");
for (Map.Entry<String, Integer> str : hm.entrySet())
int value = str.getValue();
String key = str.getKey();
System.out.println(key +"="+value);
for (String key : hm.keySet())
if("小丫".equals(key))
hm.put(key, 18);
FileOutputStream fos = new FileOutputStream("student.txt");
for (String key : hm.keySet())
if(hm.get(key) > 24)
fos.write(key.getBytes());
fos.write("\\n".getBytes());
以上是关于有一个Map集合里面存储的是学生的姓名和年龄,内容如下{赵四=21,王二=17,张三=18,小丫=25,李四=26,王五=38}(15分) * a.将里面的元素用两种遍历方式打印到控制台上 *的主要内容,如果未能解决你的问题,请参考以下文章