zoukankan      html  css  js  c++  java
  • TreeSet&第三方比较器&Map

    TreeSet集合

    特点:无序,但是可排序,不重复

    • CompareTo方法:对于String类的CompareTo方法,由对象的unicode码-参数的unicode码,并且按位比较。
    • 如果值大于0,证明对象要大于参数。
    • 如果值小于0,证明对象要小于参数。
    • 如果值等于0,证明对象等于参数

    代码演示:

     1 public class TreeSet 集合 {
     2 public static void main(String[] args) {
     3 /*TreeSet<String> s=new TreeSet<>();
     4 s.add("1");
     5 s.add("5");
     6 s.add("2");
     7 s.add("9");
     8 s.add("3");
     9 System.out.println(s);*/
    10 TreeSet<Person> set=new TreeSet<>();
    11 set.add(new Person("ls", 20));
    12 set.add(new Person("zs", 19));
    13 set.add(new Person("ml", 22));
    14 set.add(new Person("ww", 21));
    15 System.out.println(set);
    16 // System.out.println("ab".compareTo("abcd"));
    17 }
    18 }
    19 class Person implements Comparable<Person>{
    20 String name;
    21 int age;
    22 @Override
    23 public String toString() {
    24 return "Person [name=" + name + ", age=" + age + "]";
    25 }
    26 public Person() {
    27 super();
    28 // TODO Auto-generated constructor stub
    29 }
    30 public Person(String name, int age) {
    31 super();
    32 this.name = name;
    33 this.age = age;
    34 }
    35 // 假设排序规则: 按照年龄进行升序排序 -- 》要降序?将原有顺序结果加负号或者将参数与对象对调位置。
    36 @Override
    37 public int compareTo(Person o) {
    38 return -this.name.compareTo(o.name);
    39 }
    40 }

     第三方比较器:

    代码演示:

     1 public class  第三方比较器 {
     2 public static void main(String[] args) {
     3 /*TreeSet<Student> set=new TreeSet<>(new StudentComparator());
     4 set.add(new Student(19, "zs"));
     5 set.add(new Student(20, "ls"));
     6 set.add(new Student(21, "ww"));
     7 System.out.println(set);*/
     8 // 按姓名排序
     9 TreeSet<Student> set=new TreeSet<>(new Comparator<Student>() {
    10 @Override
    11 public int compare(Student o1, Student o2) {
    12 // TODO Auto-generated method stub
    13 return o1.name.compareTo(o2.name);
    14 }
    15 });
    16 set.add(new Student(19, "zs"));
    17 set.add(new Student(20, "ls"));
    18 set.add(new Student(21, "ww"));
    19 System.out.println(set);
    20 }
    21 }
    22 // 第三方比较器的子类
    23 class StudentComparator implements Comparator<Student>{
    24 //o1 相当于 this o2 相当于 o
    25 // 按照年龄比 升序
    26 @Override
    27 public int compare(Student o1, Student o2) {
    28 // TODO Auto-generated method stub
    29 return o1.age-o2.age;
    30 }
    31 }
    32 class Student {
    33 int age;
    34 String name;
    35 public Student() {
    36 super();
    37 // TODO Auto-generated constructor stub
    38 }
    39 public Student(int age, String name) {
    40 super();
    41 this.age = age;
    42 this.name = name;
    43 }
    44 @Override
    45 public String toString() {
    46 return "Student [age=" + age + ", name=" + name + "]";
    47 }
    48 }

    Map集合:

    Map集合  称为双列集合,存储的是一对值,称为键值对,其中键用key,值用value表示

    Map集合  键是不重复的,并且每个键最多映射一个值。

    如果插入集合的键与原有的键重复,会覆盖原有键的值。

    3. 常用方法:
    ①.containsKey(Object key) /containsValue(Object value) 包含键 / 值
    ***②.get(key) 通过 key 获取 value
    ***③.put(K key, V value) 添加,将 key 与 value 进行关联
    ④.remove(Object key) 通过 key ,删除。

    Map集合的遍历

    ①增强for

    ②迭代器

    注意:要使用迭代器,就要将Map集合转换为Set集合

    ① keySet() 将 Map 集合转换为 Set 集合,其中将 Map 集合中的 key 转换为 Set 集合。
    ② entrySet() 将 Map 集合转换为 Set 集合,其中将 Map 集合中的 key 和 value 同时看做一个 Entry ,
    一个 Entry 中同时包括 key 和 value 。
    1>getKey 获取键
    2>getValue 获取值

    代码演示:

     1 public class Map 集合的遍历 {
     2 public static void main(String[] args) {
     3 HashMap<String, String> map=new HashMap<>();
     4 map.put("1", "");
     5 map.put("3", "");
     6 map.put("2", "");
     7 map.put("5", "");
     8 //① keySet
     9 /*Set<String> keySet = map.keySet();
    10 //for/ 迭代器
    11 Iterator<String> it = keySet.iterator();
    12 while(it.hasNext()) {
    13 String key = it.next();
    14 // get(key)  通过 key 获取值
    15 System.out.println(key+"====>"+map.get(key));
    16 }*/
    17 //② entrySet
    18 Set<Map.Entry<String, String>> set1=map.entrySet();
    19 Iterator<Map.Entry<String, String>> it=set1.iterator();
    20 while(it.hasNext()) {
    21 Map.Entry<String, String> entry=it.next();
    22 System.out.println(entry.getKey()+"===>"+entry.getValue());
    23 }
    24 }
    25 }

    集合应用之简版斗地主:

     1 public class  简版斗地主 {
     2 public static void main(String[] args) {
     3 String[] colors= {" 方块 "," 梅花 "," 黑桃 "," 红桃 "};
     4 String[] nums= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
     5 // 洗牌: 将对应的花色与牌面拼接成字符串
     6 ArrayList<String> pokers=new ArrayList<>();
     7 for (String string : colors) {
     8 for (String st : nums) {
     9 pokers.add(string+st);
    10 }
    11 }
    12 // 加入大小王
    13 pokers.add(" 小王 ");
    14 pokers.add(" 大王 ");
    15 System.out.println(pokers);
    16 System.out.println(pokers.size());
    17 // 洗牌
    18 Collections.shuffle(pokers);
    19 System.out.println(pokers);
    20 // 底牌
    21 ArrayList<String> diPai=new ArrayList<>();
    22 // 玩家 1
    23 ArrayList<String> player1=new ArrayList<>();
    24 // 玩家 2
    25 ArrayList<String> player2=new ArrayList<>();
    26 // 玩家 3
    27 ArrayList<String> player3=new ArrayList<>();
    28 // 底牌 3 张
    29 diPai.add(pokers.remove(0));
    30 diPai.add(pokers.remove(0));
    31 diPai.add(pokers.remove(0));
    32 System.out.println(diPai);
    33 System.out.println(pokers);
    34 while(true) {
    35 if(pokers.isEmpty()) {
    36 break;
    37 }
    38 player1.add(pokers.remove(0));
    39 if(pokers.isEmpty()) {
    40 break;
    41 }
    42 player2.add(pokers.remove(0));
    43 if(pokers.isEmpty()) {
    44 break;
    45 }
    46 player3.add(pokers.remove(0));
    47 }
    48 System.out.println("========================");
    49 System.out.println(player1);
    50 System.out.println(player2);
    51 System.out.println(player3);
    52 System.out.println(player3);
    53 }
    54 }
  • 相关阅读:
    BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子
    BZOJ 2705: [SDOI2012]Longge的问题 | 数论
    BZOJ 1257[CQOI2007]余数之和sum | 数论
    BZOJ 3781: 小B的询问 | 莫队
    文件切割与合并
    [科普贴]为何Flash被淘汰?附Chrome看视频最完美教程!
    JQ模仿select
    JS正则表达式
    Vuejs——Vue生命周期,数据,手动挂载,指令,过滤器
    Vuejs——v-on
  • 原文地址:https://www.cnblogs.com/ywzbky/p/10679118.html
Copyright © 2011-2022 走看看