01_Map集合概述和特点
02_Map集合的基本功能
03_Map集合的获取功能
04_Map集合的遍历方式
方式一:
实例:
//创建集合对象
Map<String,String> map = new HashMap<String, String>();
map.put("cc001","王祖蓝");
map.put("cc002","五号");
map.put("cc003","沉陷"); //put键值相同会替换
//map.put("cc003","liuyan");
Set<String> set = map.keySet();
for (String k : set){
System.out.println(k+","+map.get(k));
}
System.out.println(map);
方式二:
//创建集合对象
Map<String,String> map = new HashMap<String, String>();
map.put("cc001","王祖蓝");
map.put("cc002","五号");
map.put("cc003","沉陷"); //put键值相同会替换
//map.put("cc003","liuyan");
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> k : entries){
System.out.println(k.getKey()+","+k.getValue());
}
System.out.println(map);
案例1:键是String,值是Student
//创建集合对象
Map<String,Student> hm = new HashMap<String, Student>();
Student student = new Student("王宇", 50, 50);
Student student1 = new Student("范泽辰", 60, 60);
Student student2 = new Student("吴彬", 100, 100);
hm.put("cc000",student);
hm.put("cc001",student1);
hm.put("cc002",student2);
//方式一:根据键找值
Set<String> set = hm.keySet();
for (String k:set){
System.out.println(k+","+hm.get(k).getName());
}
System.out.println("----------");
//方式二:根据键值对
Set<Map.Entry<String, Student>> entries = hm.entrySet();
for (Map.Entry<String,Student> k:entries){
System.out.println(k.getKey()+","+k.getValue().getName());
}
输出
案例2:键是Student,值是String
Map<Student, String> map = new HashMap<>();
Student student = new Student("盖俊豪", 1, 1);
Student student1 = new Student("吴彬", 2, 2);
Student student2 = new Student("王宇", 3, 3);
Student student3 = new Student("王宇", 3, 3);
map.put(student,"东营");
map.put(student1,"浙江");
map.put(student2,"安徽");
map.put(student3,"北京"); //Student中重写equals()和hascode()后不显示
Set<Student> keySet = map.keySet();
for (Student s:keySet){
System.out.println(s.getName()+","+map.get(s));
}、
案例3:ArrayList嵌套HashMap
ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String,String>>();
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("孙策","大乔");
hashMap.put("周瑜","小乔");
HashMap<String, String> hashMap1 = new HashMap<>();
hashMap1.put("郭靖","黄蓉");
hashMap1.put("杨过","小龙女");
HashMap<String, String> hashMap2 = new HashMap<>();
hashMap2.put("令狐冲","任盈盈");
hashMap2.put("林平之","岳灵珊");
array.add(hashMap);
array.add(hashMap1);
array.add(hashMap2);
for (HashMap<String,String> s : array){
Set<String> keySet = s.keySet();
for (String c : keySet){
System.out.println(c+","+s.get(c));
}
}
输出结果
案例3:HashMap嵌套ArrayList
//创建集合对象
HashMap<String, ArrayList<String>> hashMap = new HashMap<>();
ArrayList<String> arrayList1 = new ArrayList<>();
arrayList1.add("诸葛亮");
arrayList1.add("赵云");
hashMap.put("三国演义",arrayList1);
ArrayList<String> arrayList2 = new ArrayList<>();
arrayList2.add("唐僧");
arrayList2.add("孙悟空");
hashMap.put("西游记",arrayList2);
ArrayList<String> arrayList3 = new ArrayList<>();
arrayList3.add("武松");
arrayList3.add("鲁智深");
hashMap.put("水浒传",arrayList3);
Set<String> keySet = hashMap.keySet();
for (String key : keySet){
ArrayList<String> ss = hashMap.get(key);
System.out.println("----------"+key+"----------");
for (String s : ss){
System.out.println(s);
}
}
结果:
案例4:统计字符串中每个字符出现的次数
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串");
String line = scanner.nextLine();
//创建HashMap集合
// HashMap<Character, Integer> map = new HashMap<>(); //HashMap无序
TreeMap<Character, Integer> map = new TreeMap<>(); //TreeMap有序
for (int i = 0 ; i<line.length() ; i++){
char key = line.charAt(i);
if (map.get(key)==null){
map.put(key,1);
}else {
map.put(key,map.get(key)+1);
}
}
Set<Character> keySet = map.keySet();
for (char c : keySet){
System.out.print(c+"("+map.get(c)+")");
}
结果
.Collections的概述和使用
List<Integer> list = new ArrayList<>();
//添加元素
list.add(10);
list.add(30);
list.add(50);
list.add(20);
list.add(40);
Collections.sort(list); //升序排序
System.out.println(list);
Collections.reverse(list);//反转
System.out.println(list);
Collections.shuffle(list);//随机排序
System.out.println(list);
结果
ArrayList存储学生集合排序
ArrayList<Student> list = new ArrayList<Student>();
//添加元素
Student student1 = new Student("lll",50,60);
Student student2 = new Student("222",55,90);
Student student3 = new Student("ll33l",40,60);
list.add(student1);
list.add(student2);
list.add(student3);
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//按照数学成绩从小到大到达排序。成绩相同时,按照姓名排序
int num1 = s1.getMath()-s2.getMath();
int num2 = num1==0?s1.getName().compareTo(s2.getName()):num1;
return num2;
}
});
for (Student student : list){
System.out.println(student.getName()+","+student.getMath());
}
*斗地主案例
public static void main( String[] args )
{
HashMap<Integer, String> hashMap = new HashMap<>();
String colors[] = {"♥","♦","♠","♣"};
String numbers[] = {"2","3","4","5","6","7","8","9","10","J","Q","k","A"};
ArrayList<Integer> arrayList = new ArrayList<>();
int index= 0;
for (String s:colors){
for (String c:numbers){
hashMap.put(index,s+c);
arrayList.add(index);
index++;
}
}
hashMap.put(52,"小王");
arrayList.add(52);
hashMap.put(53,"大王");
arrayList.add(53);
Collections.shuffle(arrayList);//洗牌
TreeSet<Integer> treeSet1 = new TreeSet<>();//玩家1
TreeSet<Integer> treeSet2 = new TreeSet<>();//玩家2
TreeSet<Integer> treeSet3 = new TreeSet<>();//玩家3
TreeSet<Integer> treeSet = new TreeSet<>(); //底牌
for (int i = 0;i<arrayList.size();i++){
if (arrayList.size()-i<=3){
treeSet.add(arrayList.get(i));
}else if (i%3==0){
treeSet1.add(arrayList.get(i));
}else if (i%3==1){
treeSet2.add(arrayList.get(i));
}else if (i%3==2){
treeSet3.add(arrayList.get(i));
}
}
System.out.print("一号玩家的牌是:");
for (Integer s:treeSet1){
System.out.print(hashMap.get(s)+" ");
}
System.out.print("\n二号玩家的牌是:");
for (Integer s:treeSet2){
System.out.print(hashMap.get(s)+" ");
}
System.out.print("\n三号玩家的牌是:");
for (Integer s:treeSet3){
System.out.print(hashMap.get(s)+" ");
}
System.out.print("\n底牌是:"+" ");
for (Integer s:treeSet){
System.out.print(hashMap.get(s));
}
}*
结果