一、TreeSet对象排序:
需求:
public class Student implements Comparable<Student>{private String name;private int age;private int grade_Yu;private int grade_Shu;private int grade_Yin;private int sum=this.grade_Yin+this.grade_Shu+this.grade_Yu;public Student(){}public Student(String name,int age,int getGrade_Yu,int getGrade_Shu,int getGrade_Yin){this.name=name;this.age=age;this.grade_Yu=grade_Yu;this.grade_Shu=grade_Shu;this.grade_Yin=grade_Yin;}@Overridepublic int compareTo(Student o) {int i=this.sum-o.sum;i=i==0?this.grade_Yu-o.grade_Yu:i;i=i==0?this.grade_Shu-o.grade_Shu:i;i=i==0?this.age-o.age:i;i=i==0?this.name.compareTo(o.name):i;return i;}@Overridepublic String toString(){return "["+this.name+" "+this.age+"]";}
}
import java.util.TreeSet;
public class Main {public static void main(String[] args) {TreeSet<Student> treeSet=new TreeSet<>();Student stu1=new Student("zhangsan",23,90,99,50);Student stu2=new Student("lisi",24,90,98,50);Student stu3=new Student("wangwu",25,95,100,30);Student stu4=new Student("zhaoliu",26,60,99,70);Student stu5=new Student("qianqi",26,70,80,70);treeSet.add(stu1);treeSet.add(stu2);treeSet.add(stu3);treeSet.add(stu4);treeSet.add(stu5);System.out.println(treeSet); //[[zhangsan 23], [lisi 24], [wangwu 25], [qianqi 26], [zhaoliu 26]]}
}
二、Map集合案例-统计投票人数
需求:
import java.util.*;
import java.util.function.BiConsumer;public class Main {public static void main(String[] args) {//统计投票人数//1、创建集合对象HashMap<String,Integer> map=new HashMap<>();int n1=0,n2=0,n3=0,n4=0;//4个景点map.put("A",n1);map.put("B",n2);map.put("C",n3);map.put("D",n4);//模拟80个学生投票Random r=new Random();for(int i=0;i<80;i++){int choose=r.nextInt(4);if(choose==0){n1++;map.put("A",n1);}else if(choose==1){n2++;map.put("B",n2);}else if(choose==2){n3++;map.put("C",n3);}else if(choose==3){n4++;map.put("D",n4);}}//遍历HashMapint max=0;String result=null;Set<String> sets=map.keySet();for(String set:sets){int number=map.get(set);System.out.println("地点:"+set+",人数:"+number);if(max<number) {max = number;result =set;}}System.out.println("最多的人选择:"+result+",一共有"+max+"人");}
}
三、存储学生对象并遍历
创建Student类 alt+Insert 重写HashCode 与 Equals方法
import java.util.Objects;public class Student {private String name;private int age;private String area;public Student(){}public Student(String name,int age){this.name=name;this.age=age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Objects.equals(name, student.name) && Objects.equals(area, student.area);}@Overridepublic int hashCode() {return Objects.hash(name, age, area);}@Overridepublic String toString(){return "[姓名:"+this.name+",年龄:"+this.age+"]";}
}
import java.util.*;
import java.util.function.BiConsumer;public class Main {public static void main(String[] args) {//存储学生对象并遍历//同姓名和同年龄的认为是同一个人//创建学生对象Student stu1=new Student("zhangsan",18);Student stu2=new Student("zhangsan",18);Student stu3=new Student("zhangwu",12);//创建HashMap集合 建是学生对象,值是籍贯//存储对象HashMap<Student,String> hm=new HashMap<>();hm.put(stu1,"nanjin");hm.put(stu2,"suzhou");hm.put(stu3,"beijin");//遍历HashMap集合//keySet方法获取所有集合中的键值Set<Student> key=hm.keySet();//增强for循环遍历for(Student s:key){String area=hm.get(s);System.out.println(s.toString()+"="+area);}//[姓名:zhangwu,年龄:12]=beijin//[姓名:zhangsan,年龄:18]=nanjin//键值对 遍历//增强for循环//获取每一个 键值对 对象Set<Map.Entry<Student,String>> entries=hm.entrySet();for(Map.Entry<Student,String> s:hm.entrySet()){Student keyy=s.getKey();String value=s.getValue();System.out.println(keyy+"="+value);}//[姓名:zhangwu,年龄:12]=beijin//[姓名:zhangsan,年龄:18]=suzhou//lambda表达式遍历hm.forEach(new BiConsumer<Student, String>() {@Overridepublic void accept(Student student, String s) {System.out.println(student+"="+s);}});//[姓名:zhangwu,年龄:12]=beijin//[姓名:zhangsan,年龄:18]=suzhou}
}
四、TreeMap集合基本应用(一)
import java.util.*;public class Main {public static void main(String[] args) {//TreeMap的基本应用//键表示整数id 值表示字符串商品名称//按id的升序排列、按id的降序排列//方法一:创建对象时构造comparator比较器对象来制定排序规则//o1为添加的元素 o2为存在的元素//返回值小于零放左边,大于放右边,为0表示重复不存储TreeMap<Integer,String> tm=new TreeMap<>(new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o1-o2;//降序 return o2-o1;}});tm.put(1,"牙膏");tm.put(5,"牙刷");tm.put(3,"纸巾");tm.put(4,"镜子");tm.put(2,"梳子");System.out.println(tm);}
}
五、TreeMap集合基本应用(二)
import java.util.Objects;public class Student implements Comparable<Student>{private String name;private int age;private String area;public Student(){}public Student(String name,int age){this.name=name;this.age=age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Objects.equals(name, student.name) && Objects.equals(area, student.area);}@Overridepublic int hashCode() {return Objects.hash(name, age, area);}@Overridepublic String toString(){return "[姓名:"+this.name+",年龄:"+this.age+"]";}@Overridepublic int compareTo(Student o) {//this表示添加的元素 o表示存在的元素//返回值小于0放左边,大于0放右边,等于零表示重复元素不存储//按年龄升序int i=this.age-o.age;i=i==0?this.name.compareTo(o.name):i;return i;}
}
import java.util.*;public class Main {public static void main(String[] args) {//键:学生对象,值:籍贯//按照学生年龄进行排序,年龄一样按照学生姓名进行排序,同年龄同姓名视为同一个人//创建学生对象Student stu1=new Student("jinluhao",18);Student stu2=new Student("caixukun",15);Student stu3=new Student("wangjunkai",12);Student stu4=new Student("liusi",20);Student stu5=new Student("jinluhao",18);Student stu6=new Student("zhangsan",18);//方法二实现:在类中实现Comparable接口重新制定排序规则TreeMap<Student,String> treeMap=new TreeMap<>();treeMap.put(stu1,"jiangsu");treeMap.put(stu2,"shandong");treeMap.put(stu3,"shanghai");treeMap.put(stu4,"beijing");treeMap.put(stu5,"beijing");treeMap.put(stu6,"beijing");System.out.println(treeMap);}
}
六、TreeMap集合基本应用(三)
import java.util.*;
import java.util.function.BiConsumer;public class Main {public static void main(String[] args) {//TreeMap的基本应用//统计字符串中每一个字符的出现次数//字符串:aababcabcdabcde//要求输出结果: a: b: c: d: +次数//创建TreeMap对象 (需要可排序)String str="aababcabcdabcde";TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>(new Comparator<Character>() {@Overridepublic int compare(Character o1, Character o2) {//从小到达排序 a b c dreturn o1-o2;}});//统计次数 循坏覆盖for(int i=0;i<str.length();i++){char c=str.charAt(i);if(tm.get(c)!=null){int number=tm.get(c);number++;tm.put(c,number);}else tm.put(c,1);}//遍历打印输出//获取每个键值对对象存入Set集合中Set<Map.Entry<Character,Integer>> entries=tm.entrySet();//构建迭代器对象Iterator<Map.Entry<Character,Integer>> it=entries.iterator();//迭代器遍历while(it.hasNext()){char key=it.next().getKey();int value=tm.get(key);System.out.print(key+"("+value+")"+" ");}//a(5) b(4) c(3) d(2) e(1)System.out.println();//lambda表达式遍历tm.forEach((character,integer)-> System.out.print(character+"("+integer+")"+" "));//a(5) b(4) c(3) d(2) e(1)System.out.println();//要求输出为字符串格式:StringBuilder sb=new StringBuilder();tm.forEach((character,integer)-> sb.append(character).append("(").append(integer).append(")").append(" "));System.out.println(sb);//a(5) b(4) c(3) d(2) e(1)//构建StringJoiner对象时括号内指定 开始标记、结束标记、中间间隔符号StringJoiner sj=new StringJoiner("","","");//add方法内参数需要是字符串,因此加上一个“” 使其转为字符串tm.forEach((character,integer)-> sj.add(character+"").add("(").add(integer+"").add(")").add(" "));System.out.println(sj);//a(5) b(4) c(3) d(2) e(1)}
}