zoukankan      html  css  js  c++  java
  • [Java 13 类集合] BasicContainer, Collection, Set, HashSet, TreeSet

    Container, Set, equals, hashCode()
    package com.qunar.basicJava.javase.p13_ClassCollection;
    
    import java.util.Collection; /** 存放单值的最大父接口 */
    import java.util.HashSet;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-6-9 15:58
     */
    public class BasicContainer {
        public static void main(String[] args) {
            Collection c = new HashSet();
    
            c.add("hello");
            c.add(new Name("f1", "l1"));
            c.add(new Integer(100));
    
            c.remove("hello");
            c.remove(new Integer(100));
    
            System.out.println(c.remove(new Name("f1", "l1")));
            System.out.println(c);
        }
    }
    
    class Name implements Comparable {
        private String firstName, lastName;
    
        public Name(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public String toString() {
            return firstName + " " + lastName;
        }
    
        public boolean equals(Object obj) {
            if (obj instanceof Name) {
                Name name = (Name) obj;
                return (firstName.equals(name.firstName))
                        && (lastName.equals(name.lastName));
            }
            return super.equals(obj);
        }
    
        public int hashCode() {
            return firstName.hashCode(); // 重写 equals 方法必须要重写 hashCode 方法, 当你 Name 类这个对象作为索引,key的时候,就要用到 hashCode
        }
    
        public int compareTo(Object o) {
            Name n = (Name) o;
            int lastCmp = lastName.compareTo(n.lastName); // 调用 String 的 compareTo 方法
            return (lastCmp != 0 ? lastCmp : firstName.compareTo(n.firstName));
        }
    
    }
    
    HashSet
    package com.qunar.basicJava.javase.p13_ClassCollection;
    
    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-6-9 19:30
     *
     * HashSet 知识点, 采用散列的存储方式,所有没有顺序,直接 println(HashSet) : 调用的 toString()
     */
    public class HashSetDemo01 {
        public static void main(String[] args) {
            Set<String> strings = new HashSet<>();
            strings.add("A");
            strings.add("B");
            strings.add("C");
            strings.add("D");
            System.out.println(strings); //
        }
    }
    /**
     * 输出 :
     * [D, A, B, C]
     */
    
    TreeSet
    package com.qunar.basicJava.javase.p13_ClassCollection;
    
    import java.util.Set;
    import java.util.TreeSet;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-6-9 19:43
     *
     * TreeSet 有序,extends AbstractSet<E> implements SortedSet<E>, Serializable
     */
    public class TreeSetDemo01 {
        public static void main(String[] args) {
            Set<String> strings = new TreeSet<String>();
            strings.add("C");
            strings.add("A");
            strings.add("B");
            System.out.println(strings);
    
        }
    }
    /** 输出 :
     [A, B, C]
     */
    
    Repeat, HashSet
    package com.qunar.basicJava.javase.p13_ClassCollection;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-6-9 20:45
     */
    class Person {
        private String name;
        private int age;
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj) return true;
            if (!(obj instanceof Person)) {
                return false;
            }
            Person person = (Person) obj;
            if (this.name.equals(person.name) && this.age == person.age) {
                return true;
            } else return false;
        }
    
        @Override
        public int hashCode() {
            return this.name.hashCode() * this.age;
        }
    
        @Override
        public String toString() {
            return "姓名 : " + this.name + " 年龄 : " + this.age;
        }
    }
    
    package com.qunar.basicJava.javase.p13_ClassCollection;
    
    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-6-9 20:51
     */
    public class RepeatDemo {
        public static void main(String[] args) {
            Set<Person> persons = new HashSet<>();
            persons.add(new Person("张3", 30));
            persons.add(new Person("张4", 28));
            persons.add(new Person("张5", 30));
            persons.add(new Person("张4", 28));
            persons.add(new Person("张7", 30));
            System.out.println(persons);
        }
    }
    

    输出 :

    [姓名 : 张3 年龄 : 30, 姓名 : 张7 年龄 : 30, 姓名 : 张4 年龄 : 28, 姓名 : 张5 年龄 : 30]


  • 相关阅读:
    php使用redis的有序集合zset实现延迟队列
    php使用redis的几种常见方式和用法
    redis缓存雪崩,缓存穿透,缓存击穿的解决方法
    php操作redis数据库方法总结
    mysql 悲观锁与乐观锁的理解
    OAuth2.0 协议的理解
    windows下的mongodb安装与配置
    node.js中对 redis 的安装和基本操作
    node.js中对 mysql 进行增删改查等操作和async,await处理
    node.js中 koa 框架的基本使用方法
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786484.html
Copyright © 2011-2022 走看看