[Java 13 类集合] BasicContainer, Collection, Set, HashSet, TreeSet

时间:2023-02-12 17:56:25
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]