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); } }
輸出 :java
[姓名 : 張3 年齡 : 30, 姓名 : 張7 年齡 : 30, 姓名 : 張4 年齡 : 28, 姓名 : 張5 年齡 : 30]
ide