1. 如何遍历 Map 的 Key 集,value 集, key-value 集,使用上泛型
Map<String,Integer> map = new HashMap<String,Integer>();
map.put(); ....
//遍历key
Set<String> keySet = map.keySet();
for(String key : keySet){
System.out.println(key);
}
//遍历value
Collection<Integer> values = map.values();
Iterator<Integer> iterator = values.iterator():
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//遍历key-value
Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
Iterator<Map.Entry<String,Integer>> iterator = entrySet.iterator();
while(iterator.hasNext()){
Map.Entry<String,Integer> entry = iterator.next();
String key = entry.getkey();
Integer value = entry.getValue();
System.out.println(key + "--->" + value);
}
2. 写出使用Iterator 和 增强for循环遍历List<String>的代码,使用上泛型
3. 提供一个方法,用于遍历获取HashMap<String,String>中的所有value,并存放在List中返回。考虑上集合中泛型的使用。
public List<String> getValueList(HashMap<String,String> map){
ArrayList<String> valueList = new ArrayList<>();
Collcetion<String> values = map.values();
for(String value : values){
valueList.add(value);
}
return valueList;
}
4. 创建一个与a.txt文件同目录下的另外一个文件b.txt
File file1 = new File("d:\\a\\a.txt");
File file2 = new File(file1.getParent(),"b.txt");
5. Map接口中的常用方法有哪些
增:put(K k, V v)
删:V remove(K k)
改:put(K k, V v)
查:V get(K k)
长度:int size()
遍历:keySet(),values(),entrySet()
package top.qaqaq.java.P581;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* @author RichieZhang
* @create 2022-10-29 下午 4:43
*/
public class MapTest {
@Test
public void test1(){
Map<String,Integer> map = new HashMap<>();
map.put("AA",123);
map.put("BB",111);
map.put("CC",222);
Set<String> keySet = map.keySet();
for (String s : keySet) {
System.out.println(s);
}
Collection<Integer> values = map.values();
for (Integer value : values) {
System.out.println(value);
}
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + "===" + value);
}
}
@Test
public void test2(){
List<String> list = new ArrayList<>();
list.add("AA");
list.add("BB");
list.add("CC");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void test3(){
HashMap<String,String> map = new HashMap<>();
map.put("AA","123");
map.put("BB","111");
map.put("CC","222");
System.out.println(getValueList(map));
}
public List<String> getValueList(HashMap<String,String> map){
ArrayList<String> valueList = new ArrayList<>();
Collection<String> values = map.values();
for (String value : values){
valueList.add(value);
}
return valueList;
}
@Test
public void test4() throws IOException {
File file = new File("hello.txt");
File destFile = new File(file.getParent(),"he.txt");
boolean newFile = destFile.createNewFile();
if (newFile){
System.out.println("创建成功!");
}
}
}