[Java 13 类集合] BasicContainer, Collection, Set, HashSet, TreeSet
2014-06-11 15:01
661 查看
Container, Set, equals, hashCode()
输出 :
[姓名 : 张3 年龄 : 30, 姓名 : 张7 年龄 : 30, 姓名 : 张4 年龄 : 28, 姓名 : 张5 年龄 : 30]
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]
相关文章推荐
- [Java 13 类集合] BasicContainer, Collection, Set, HashSet, TreeSet
- Lesson_for_java_day13--java中的集合——Collection、List、ArrayList、LinkedList、Set、HashSet、TreeSet
- Java高级之集合与泛型>Collection、Set、HashSet、LinkedHashSet、TreeSet
- 复习java基础第三天(集合:Collection、Set、HashSet、LinkedHashSet、TreeSet)
- 30、Java中Set集合之HashSet、TreeSet和EnumSet
- Java基础-17总结,登录注册案例,Set集合,HashSet,TreeSet,LinkHashSet
- Java中的集合HashSet、LinkedHashSet、TreeSet和EnumSet(二)
- 黑马程序员------毕老师视频笔记第13-17天------集合Collection-Set-(HashSet、TreeSet)
- 小东吖 之 java Set接口 HashSet集合 TreeSet集合
- Java基础-17总结,登录注册案例,Set集合,HashSet,TreeSet,LinkHashSet
- JAVA学习.JAVA集合类型Collection.Set.HashSet&TreeSet
- Java:集合,Array、Collection(List/Set/Queue)、Map的遍历,比如:ArrayList,LinkedList,HashSet,HashMap
- 黑马程序员_Java第14天知识总结_集合类(集合框架)_Collection_迭代器_List_Set_HashSet
- 集合类(常见的集合类:Collection、List、Set、ArrayList、linkedList、Vector、HashSet、TreeSet)
- Java集合Set、Map、HashSet、HashMap、TreeSet、TreeMap等
- 【Java集合之Set】HashSet、TreeSet、LinkedHashSet区别
- Java--集合(Set:HashSet,TreeSet)
- JavaSE入门学习36:Java集合框架之Set接口及其实现类HashSet和TreeSet
- Java Collection —— Set(HashSet、TreeSet、LinkedHashSet)
- Java集合Set、Map、HashSet、HashMap、TreeSet、TreeMap等