zoukankan      html  css  js  c++  java
  • java集合之set

      1 public class Demo1_Set {
      2 
      3     /*
      4      * set集合无序、不可重复、无索引
      5      */
      6     public static void main(String[] args) {
      7 
      8         //demo1();
      9         HashSet<Student> hs = new HashSet<>();
     10         hs.add(new Student("張三",19));
     11         hs.add(new Student("張三",19));
     12         hs.add(new Student("李四",20));
     13         hs.add(new Student("李四",20));
     14         hs.add(new Student("李四",20));
     15         System.out.println(hs);
     16         /*
     17           注意:  向集合中添加自定义类对象时,要想不添加重复的数据(如:同姓名、同年龄认为是同一个人),需要在自定义类中重写equals和hashCode方法
     18          执行结果如下:
     19          -----------------------        
     20                   執行了嗎
     21             執行了嗎
     22             執行了嗎
     23             執行了嗎
     24             [Student [name=李四, age=20], Student [name=張三, age=19]]
     25          --------------------------------
     26             @Override
     27             public boolean equals(Object arg0) {
     28                 System.out.println("執行了嗎");
     29                 Student s = (Student) arg0;
     30                 return this.name.equals(s.name) && this.age == s.age;
     31             }
     32             
     33             @Override
     34             public int hashCode() {
     35                 return 1;
     36             }
     37          */
     38     }
     39 
     40     public static void demo1() {
     41         HashSet<String> hs = new HashSet<>();
     42         boolean b1 = hs.add("a");
     43         boolean b2 = hs.add("a");
     44         boolean b3 = hs.add("c");
     45         boolean b4 = hs.add("d");
     46         boolean b5 = hs.add("e");
     47         boolean b6 = hs.add("f");
     48         
     49         System.out.println(b1);   // true
     50         System.out.println(b2);   //false
     51         System.out.println(hs);   //[f, d, e, c, a]
     52         for (String string : hs) {
     53             System.out.print(string);  //fdeca  Set集合类有实现toString的方法,所以打印对象时是打印的值
     54         }
     55     }
     56 
     57 }
     58 
     59 --------------------------------------------------------------------------------
     60 
     61 public class Student implements Comparable<Student> {
     62     private String name;
     63     private int age;
     64 
     65     @Override
     66     public String toString() {
     67         return "Student [name=" + name + ", age=" + age + "]";
     68     }
     69     /*
     70      * @Override public boolean equals(Object arg0) { System.out.println("執行了嗎");
     71      * Student s = (Student) arg0; return this.name.equals(s.name) && this.age ==
     72      * s.age; }
     73      * 
     74      * 
     75      * @Override public int hashCode() { return 1; }
     76      */
     77 
     78     public Student(String name, int age) {
     79         super();
     80         this.name = name;
     81         this.age = age;
     82     }
     83 
     84     @Override
     85     public int hashCode() {
     86         final int prime = 31;
     87         int result = 1;
     88         result = prime * result + age;
     89         result = prime * result + ((name == null) ? 0 : name.hashCode());
     90         return result;
     91     }
     92 
     93     @Override
     94     public boolean equals(Object obj) {
     95         if (this == obj)
     96             return true;
     97         if (obj == null)
     98             return false;
     99         if (getClass() != obj.getClass())
    100             return false;
    101         Student other = (Student) obj;
    102         if (age != other.age)
    103             return false;
    104         if (name == null) {
    105             if (other.name != null)
    106                 return false;
    107         } else if (!name.equals(other.name))
    108             return false;
    109         return true;
    110     }
    111 
    112     public String getName() {
    113         return name;
    114     }
    115 
    116     public void setName(String name) {
    117         this.name = name;
    118     }
    119 
    120     public int getAge() {
    121         return age;
    122     }
    123 
    124     public void setAge(int age) {
    125         this.age = age;
    126     }
    127 
    128     /*
    129      * 优先比较姓名的长度,其次比较姓名的内容,再比较年龄
    130      */
    131     @Override
    132     public int compareTo(Student arg0) {
    133         int length = this.name.length() - arg0.name.length();
    134         int num = length == 0 ? this.name.compareTo(arg0.name) : length;
    135         return num == 0 ? this.age - arg0.age : num;
    136     }
    137 
    138     /*
    139      * 优先按照姓名排序,之后再按照年龄排序
    140      * 
    141      * @Override public int compareTo(Student arg0) { int num =
    142      * this.name.compareTo(arg0.name); return num == 0 ? this.age - arg0.age : num;
    143      * }
    144      */
    145 
    146     /*
    147      * 优先按照年龄排序,之后再按照姓名排序
    148      * 
    149      * @Override public int compareTo(Student arg0) { int num = this.age - arg0.age;
    150      * return num == 0 ? this.name.compareTo(arg0.name) : num; }
    151      */
    152 
    153 }
    154 
    155 ------------------------------------------------------------------------
    156 
    157 public class Demo2_Quchong {
    158 
    159     /*
    160      * 需求:将一个集合中的重复元素去除掉
    161      * 分析:
    162      *         1.获取一个拥有重复元素的list集合
    163      *         2.将list集合中的元素添加到set集合中
    164      *         3.将list集合中的元素清除
    165      *         4.将获取到的set集合添加到清空了的list集合中
    166      * 
    167      * LinkedHashSet 集合怎么存进去的就怎么取出来
    168      * TreeSet 集合是可以对存进去的集合进行排序,同样也可以保证集合中元素的唯一行
    169      */
    170     public static void main(String[] args) {
    171          //*         1.获取一个拥有重复元素的list集合
    172         ArrayList<String> list = new ArrayList<>();
    173         list.add("a");
    174         list.add("a");
    175         list.add("b");
    176         list.add("b");
    177         list.add("b");
    178         list.add("c");
    179         list.add("c");
    180         list.add("c");
    181         list.add("c");
    182         
    183         //通过方法去除重复元素
    184         getSingle(list);
    185         
    186         //打印list集合
    187         System.out.println(list);
    188          
    189     }
    190 
    191     public static void getSingle(List<String> list) {
    192         
    193         //2.将list集合中的元素添加到set集合中
    194         LinkedHashSet<String> lhs = new LinkedHashSet<>();
    195         lhs.addAll(list);
    196         
    197          //3.将list集合中的元素清除
    198         list.clear();
    199         
    200          //4.将获取到的set集合添加到清空了的list集合中
    201         list.addAll(lhs);
    202     }
    203 
    204 }
    205 
    206 ------------------------------------------------------------------
    207 
    208 public class Demo3_Quchong {
    209 
    210     /*
    211      * 需求:通过键盘输入一串字符,然后去除重复,输出不同的字符
    212      * 分析:
    213      *   1.提示输入一串字符
    214      *   2.将这一串字符转换成字符数组
    215      *   3.将字符数组存入set集合
    216      *   4.打印最终的结果
    217      */
    218     public static void main(String[] args) {
    219         //1.提示输入一串字符
    220         Scanner sc = new Scanner(System.in);
    221         System.out.println("请输入一串字符:");
    222         
    223         //2.将这一串字符转换成字符数组
    224         String line = sc.nextLine();
    225         char[] ch = line.toCharArray();
    226         
    227         //3.将字符数组存入set集合
    228         HashSet<Character> hs = new HashSet<>();
    229         for (Character character : ch) {
    230             hs.add(character);
    231         }
    232         
    233         //4.打印最终的结果
    234         System.out.println(hs);
    235 
    236     }
    237 
    238 }
    239 
    240 -------------------------------------------------------------------
    241 
    242 public class Demo4_TreeSet {
    243 
    244     /*
    245      * TreeSet 集合自动对集合进行排序,同样也能满足集合中元素的唯一性
    246      * 
    247      * 当自定义对象没有实现Comparable方法的时候,新增对象的时候会报类型转换异常
    248      * 当compareTo方法返回值为 0 的时候,集合中只会有一个元素被存进去
    249      * 当compareTo方法返回值为正数的时候,怎么存就怎么取
    250      * 当compareTo方法返回值为负数的时候,取的时候会倒序去值
    251      */
    252     public static void main(String[] args) {
    253         //demo1();
    254         //demo2();
    255         //demo3();
    256         TreeSet<String> ts = new TreeSet<>(new CompareByLen());
    257         ts.add("aaaaaaaa");
    258         ts.add("b");
    259         ts.add("cc");
    260         ts.add("avb");
    261         ts.add("nba");
    262         System.out.println(ts);
    263     }
    264 
    265     public static void demo3() {
    266         TreeSet<Student> ts = new TreeSet<>();
    267         ts.add(new Student("zhangsan", 23));
    268         ts.add(new Student("lisi", 33));
    269         ts.add(new Student("zhaoliu", 43));
    270         ts.add(new Student("wangwu", 13));
    271         ts.add(new Student("aaaa", 13));
    272         System.out.println(ts);
    273     }
    274 
    275     public static void demo2() {
    276         TreeSet<Student> ts = new TreeSet<>();
    277         ts.add(new Student("张三",23));
    278         ts.add(new Student("王五",22));
    279         ts.add(new Student("赵五",22));
    280         ts.add(new Student("李四",25));
    281         ts.add(new Student("赵六",20));
    282         System.out.println(ts);
    283     }
    284 
    285     public static void demo1() {
    286         TreeSet<String> ts = new TreeSet<>();
    287         ts.add("c");
    288         ts.add("a");
    289         ts.add("b");
    290         ts.add("a");
    291         ts.add("b");
    292         ts.add("b");
    293         ts.add("a");
    294         
    295         System.out.println(ts);   //[a, b, c]
    296     }
    297 
    298 }
    299 
    300 class CompareByLen implements Comparator<String> {
    301 
    302     @Override
    303     public int compare(String s1, String s2) {
    304         int num = s1.length() - s2.length();
    305         return num == 0 ? s1.compareTo(s2) : num;
    306     }
    307     
    308 }
  • 相关阅读:
    nodejs 异步转同步整理
    使用async-utility 转换异步请求为同步
    cube.js schemaVersion npm 包
    开发一个cube.js schemaVersion 包装
    cube.js 多租户参考设计说明
    cube.js 调度&&查询队参考参数
    cube.js 自定义首页显示信息
    cube.js data-blending一些说明
    cube.js 新版本的一些特性
    cube.js TimeoutError: ResourceRequest timed out 问题参考解决方法
  • 原文地址:https://www.cnblogs.com/jiangjunwei/p/9261479.html
Copyright © 2011-2022 走看看