本文主要是【java】——java中list,map习题的文章,如果有什么需要改进的地方还请大佬指出⛺️
作者简介:大家好,我是听风与他
☁️博客首页:CSDN主页听风与他
每日一句:狠狠沉淀,顶峰相见
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, Integer> map = new HashMap<>();
map.put("zhangsan", 3);
map.put("lisi", 1);
map.put("wangwu", 2);
map.put("zhaoliu", 4);
map.put("xiha", 5);
Map<String, Integer> ans = sortMap(map);
int i=3;
Iterator<Entry<String, Integer>> entries = ans.entrySet().iterator();
while(i-->0) {
Entry<String, Integer> entry = entries.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
public static Map<String, Integer> sortMap(Map<String, Integer> map){
//利用Map的entrySet方法,转化为list进行排序
List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
// TODO Auto-generated method stub
return o2.getValue()-o1.getValue();
}
});
//排序好的list,一定要放进LinkedHashMap,因为只有LinkedHashMap是根据插入顺序进行存储
LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
for(Map.Entry<String, Integer> e:entries) {
linkedHashMap.put(e.getKey(), e.getValue());
}
return linkedHashMap;
}
}
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("apple");
list.add("orange");
list.add("banana");
list.add("grape");
list.add("grape");
Map<String, Integer> map = f(list);
System.out.println(map);
Iterator<Entry<String, Integer>> entries = map.entrySet().iterator();
while(entries.hasNext()) {
Entry<String, Integer> entry = entries.next();
System.out.println(entry.getKey()+":"+entry.getValue()+" ");
}
}
public static Map<String, Integer> f(List<String> list){
//注意hashmap是无序的,linkedHashMap是有序的。
Map<String, Integer> map = new LinkedHashMap<>();
for(String i:list) {
map.put(i, map.containsKey(i)?map.get(i)+1:1);
}
return map;
}
}
package test;
import java.util.ArrayList;
import java.util.List;
public class test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<>();
list.add("123");
list.add("abc");
list.add("456");
list.add("def");
list.add("789");
list.add("ghi");
List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
for(String i:list) {
if(i.charAt(0)>=97&&i.charAt(0)<=122) {
list2.add(i);
}else {
list1.add(i);
}
}
System.out.println("数字列表:"+list1+",字母列表:"+list2);
}
}
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, List<Integer>> map = new LinkedHashMap<>();
List<Integer> list1 = new ArrayList<>();
list1.add(85);
list1.add(90);
list1.add(95);
List<Integer> list2 = new ArrayList<>();
list2.add(80);
list2.add(85);
list2.add(90);
List<Integer> list3 = new ArrayList<>();
list3.add(95);
list3.add(100);
list3.add(105);
map.put("John", list1);
map.put("Bob", list2);
map.put("Alice", list3);
Map<String, Double> map1 = f(map);
for(String key:map1.keySet()) {
System.out.println(key+":"+map1.get(key));
}
}
public static Map<String, Double> f(Map<String, List<Integer>> map){
Map<String, Double> map1 = new LinkedHashMap<>();
Set<String> set=map.keySet();
for(String i:set) {
double avg=(double)(map.get(i).get(0)+map.get(i).get(1)+map.get(i).get(2))/3;
map1.put(i, avg);
}
return map1;
}
}
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class test5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<>();
list.add("John");
list.add("[email protected]");
list.add("Alice");
list.add("[email protected]");
list.add("Bob");
list.add("[email protected]");
Map<String, String> map=f(list);
System.out.println(map);
Set<String> set=map.keySet();
for(String i:set) {
System.out.println(i+":"+map.get(i));
}
}
public static Map<String, String> f(List<String> list){
Map<String, String> map = new LinkedHashMap<>();
for(int i=0;i<list.size();i=i+2) {
map.put(list.get(i), list.get(i+1));
}
return map;
}
}