美文网首页
java二十三(Set)(HashSet、TreeSet、Lin

java二十三(Set)(HashSet、TreeSet、Lin

作者: Nic_ofh | 来源:发表于2017-10-16 15:16 被阅读0次

    一、HashSet

    • HashSet的父类为Set,特点是:
      1、没有顺序
      2、没有重复元素。
      3、没有下标,不能用for i 循环
    • demo
     Dog dog1 = new Dog(1, "菲菲");
    Set<Dog> dogSet = new HashSet<>();
            dogSet.add(dog1);
            dogSet.add(dog1);
    //[Dog{age=1, name='菲菲'}],最后只打印一项
            System.out.println(Arrays.toString(dogSet.toArray()));
    

    二、TreeSet

    • TreeSet的父类为TreeSet,特点是:
      1、TreeSet对自定的Comparator元素比较进行排序输出
      2、Comparator元素比较进行排除重复元素
      3、没有下标,不能用for i 循环
     Dog dog1 = new Dog(1, "菲菲");
            Dog dog2 = new Dog(10, "哈皮");
            Dog dog3 = new Dog(100, "爷爷");
            Dog dog4 = new Dog(1, "Tom");
            Dog dog5 = new Dog(2, "哈皮");
    // 对年龄进行排序
     TreeSet<Dog> dogTreeSet = new TreeSet<>(((d1, d2) -> d1.getAge() - d2.getAge()));
            dogTreeSet.add(dog1);
            dogTreeSet.add(dog2);
            dogTreeSet.add(dog3);
            dogTreeSet.add(dog4);
            dogTreeSet.add(dog5);
    
            // [Dog{age=1, name='菲菲'}, Dog{age=2, name='哈皮'}, Dog{age=10, name='哈皮'}, Dog{age=100, name='爷爷'}]
            System.out.println(Arrays.toString(dogTreeSet.toArray()));
    

    三、LinkedHashSet

    1、固定位置
    2、没有重复元素。
    3、没有下标,不能用for i 循环

     Dog dog1 = new Dog(1, "菲菲");
            Dog dog2 = new Dog(10, "哈皮");
            Dog dog3 = new Dog(100, "爷爷");
            Dog dog4 = new Dog(1, "Tom");
            Dog dog5 = new Dog(2, "哈皮");
     LinkedHashSet<Dog> dogLinkedHashSet = new LinkedHashSet<>();
            dogLinkedHashSet.add(dog1);
            dogLinkedHashSet.add(dog1);
            dogLinkedHashSet.add(dog2);
            dogLinkedHashSet.add(dog3);
            dogLinkedHashSet.add(dog4);
            dogLinkedHashSet.add(dog5);
            System.out.println(Arrays.toString(dogLinkedHashSet.toArray()));
    //[Dog{age=1, name='菲菲'}, Dog{age=10, name='哈皮'}, Dog{age=100, name='爷爷'}, Dog{age=1, name='Tom'}, Dog{age=2, name='哈皮'}]
    

    Dog.java

    package com.kouding;
    
    public class Dog {
        private int age;
        private String name;
    
        public Dog(int age, String name) {
            this.age = age;
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            Dog dog = (Dog) o;
    
            if (age != dog.age) return false;
            return name.equals(dog.name);
        }
    
        @Override
        public String toString() {
            return "Dog{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    
        @Override
        public int hashCode() {
            int result = age;
            result = 31 * result + name.hashCode();
            return result;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:java二十三(Set)(HashSet、TreeSet、Lin

          本文链接:https://www.haomeiwen.com/subject/vjgkuxtx.html