public class MapSaveArrayListDemo {
public static void main(String[] args) {
//定义一个Mapy集合,且于存放ArrayList
HashMap<String, ArrayList<Student>> arrListMap = new HashMap<String, ArrayList<Student>>();
//定义一个ArrayList集合,用于存放学生对象
ArrayList<Student> arrListA = new ArrayList<>();
//定义一班的三个学生,并加入arrList集合
Student stuA1 = new Student("李一", 34);
Student stuA2 = new Student("王一", 28);
Student stuA3 = new Student("张一", 35);
arrListA.add(stuA1);
arrListA.add(stuA2);
arrListA.add(stuA3);
//把arrList集合加入 arrListMap<k,v>中的v
arrListMap.put("20201",arrListA);
ArrayList<Student> arrListB = new ArrayList<>();
//定义二班的三个学生,并加入arrList集合
Student stuB1 = new Student("李二", 18);
Student stuB2 = new Student("王二", 22);
Student stuB3 = new Student("张二", 32);
arrListB.add(stuB1);
arrListB.add(stuB2);
arrListB.add(stuB3);
//把arrList集合加入 arrListMap<k,v>中的v
arrListMap.put("20202",arrListB);
//遍历集合
Set<String> mapKeySet = arrListMap.keySet();
for(String key :mapKeySet){
System.out.println("班级"+key+"的学生:");
ArrayList<Student> listByKey = arrListMap.get(key);
for(Student stu :listByKey){
String name = stu.getName();
int age = stu.getAge();
System.out.println("\t"+name+","+age);
}
}
}
}
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
/*
1.接收一个输入字符串 :abcjiefeaafkfikfjikgfkjgwojfut
2.遍历这个字符串
3.统计字母 k:字母,v:数量
4.拼接打印出来 :a(3)b(1)c(1)e(2)f(6)g(2)i(3)j(4)k(4)o(1)t(1)u(1)w(1)
*/
public class TongJiByMapDemo {
public static void main(String[] args) {
//接收控制台输入的一串字符
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String scLine = sc.nextLine();
//定义一个Map集合 用TreeMap 可以排序,用HashMap不能排序
TreeMap<Character, Integer> charMap = new TreeMap<>();
//遍历得到每一个字符
for(int i=0;i<scLine.length();i++){
char key = scLine.charAt(i);
boolean b = charMap.containsKey(key);
if(b){
Integer value = charMap.get(key);
value++;
charMap.put(key,value);
}else {
charMap.put(key,1);
}
}
StringBuilder resulte = new StringBuilder();
//遍历charMap集合
Set<Character> keySet = charMap.keySet();
for(Character key: keySet){
Integer vule = charMap.get(key);
resulte.append(key).append("(").append(vule).append(")");
}
System.out.println(resulte);
}
}