Day23每日一考
题目
1.如何遍历Map的key集,value集,key-value集,使用上泛型
2.写出使用Iterator和增强for循环遍历List<String>的代码,使用上泛型
3.提供一个方法,用于遍历获取HashMap<String,String>中的所有value,并存放在List中返回。考虑上集合中泛型的使用。
4.创建一个与a.txt文件同目录下的另外一个文件b.txt
5.Map接口中的常用方法有哪些
答案
1
public void test01(){
HashMap<Integer, String> map = new HashMap<>();
map.put(1,"A");
map.put(2,"B");
map.put(3,"ABN");
map.put(4,"AC");
map.put(5,"ADF");
Set<Integer> integers = map.keySet();
integers.forEach(System.out::println);
Collection<String> values = map.values();
values.forEach(System.out::println);
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
System.out.println(entry.getKey()+"**********"+entry.getValue());
}
}
2
public void test02(){
ArrayList<String> strings = new ArrayList<>();
strings.add("A");
strings.add("A1");
strings.add("A2");
strings.add("A3");
strings.add("A4");
Iterator<String> iterator = strings.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
strings.forEach(System.out::println);
}
3
public void test03(){
HashMap<String, String> map = new HashMap<>();
map.put("QWE","A");
map.put("ASD","B");
map.put("ZXC","ABN");
map.put("UHB","AC");
map.put("YHB","ADF");
Collection<String> values = map.values();
ArrayList<String> strings = new ArrayList<>();
for (String value : values) {
strings.add(value);
}
System.out.println(strings);
}
4
@Test
public void test04() throws IOException {
File file = new File("D:\\a.txt");
File file1 = new File(file.getParent(), "b.txt");
file1.createNewFile();
}
5
增:put
删: remove
改: put
查 :get
遍历:迭代器、foreach()、for增强