534
京東網上商城
Java類集--Set接口、HashSet、TreeSet、SortedSet接口
Set接口的定義

Collection就不能進行雙向輸出,因為沒有提供get()方法,但是Set接口與Collection接口的定義一致,所以其本身也不能雙向輸出。
HashSet:使用散列的方式存放內容,本身沒有順序。
import java.util.HashSet ; import java.util.Set ; public class HashSetDemo01{ public static void main(String args[]){ Set<String> allSet = new HashSet<String>() ; allSet.add("A") ; // 增加內容 allSet.add("B") ; // 增加內容 allSet.add("C") ; // 增加內容 allSet.add("C") ; // 重複內容 allSet.add("C") ; // 重複內容 allSet.add("D") ; // 增加內容 allSet.add("E") ; // 增加內容 System.out.println(allSet) ; } };
執行結果中可以看出插入的順序是無序排列的,而List接口的內同插入的順序是其保存的順序。
如果現在希望所有的內容可以自動進行排序的操作,則可以使用Set中的第二個子類————TreeSet.
import java.util.TreeSet ; import java.util.Set ; public class TreeSetDemo01{ public static void main(String args[]){ Set<String> allSet = new TreeSet<String>() ; allSet.add("C") ; // 增加內容 allSet.add("C") ; // 重複內容 allSet.add("C") ; // 重複內容 allSet.add("D") ; // 增加內容 allSet.add("B") ; // 增加內容 allSet.add("A") ; // 增加內容 allSet.add("E") ; // 增加內容 System.out.println(allSet) ; } };
TreeSet類的內容是可以排序的,那麼任意給出一個類,觀察能否進行排序。
import java.util.Set ; import java.util.TreeSet ; class Person{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String toString(){ return "姓名:" + this.name + ";年齡:" + this.age ; } }; public class TreeSetDemo02{ public static void main(String args[]){ Set<Person> allSet = new TreeSet<Person>() ; allSet.add(new Person("張三",30)) ; allSet.add(new Person("李四",31)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("趙六",33)) ; allSet.add(new Person("孫七",33)) ; System.out.println(allSet) ; } };

import java.util.Set ; import java.util.TreeSet ; class Person implements Comparable<Person>{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String toString(){ return "姓名:" + this.name + ";年齡:" + this.age ; } public int compareTo(Person per){ if(this.age>per.age){ return 1 ; }else if(this.age<per.age){ return -1 ; }else{ return 0 ; } } }; public class TreeSetDemo03{ public static void main(String args[]){ Set<Person> allSet = new TreeSet<Person>() ; allSet.add(new Person("張三",30)) ; allSet.add(new Person("李四",31)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("趙六",33)) ; allSet.add(new Person("孫七",33)) ; System.out.println(allSet) ; } };

import java.util.Set ; import java.util.TreeSet ; class Person implements Comparable<Person>{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String toString(){ return "姓名:" + this.name + ";年齡:" + this.age ; } public int compareTo(Person per){ if(this.age>per.age){ return 1 ; }else if(this.age<per.age){ return -1 ; }else{ return this.name.compareTo(per.name) ; // 調用String中的compareTo()方法 } } }; public class TreeSetDemo04{ public static void main(String args[]){ Set<Person> allSet = new TreeSet<Person>() ; allSet.add(new Person("張三",30)) ; allSet.add(new Person("李四",31)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("趙六",33)) ; allSet.add(new Person("孫七",33)) ; System.out.println(allSet) ; } };
此時去掉的重複元素並不是真正意義上的重複元素的取消。
import java.util.Set ; import java.util.HashSet ; class Person{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String toString(){ return "姓名:" + this.name + ";年齡:" + this.age ; } }; public class RepeatDemo01{ public static void main(String args[]){ Set<Person> allSet = new HashSet<Person>() ; allSet.add(new Person("張三",30)) ; allSet.add(new Person("李四",31)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("趙六",33)) ; allSet.add(new Person("孫七",33)) ; System.out.println(allSet) ; } };

import java.util.Set ; import java.util.HashSet ; class Person{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public boolean equals(Object obj){ // 覆寫equals,完成對象比較 if(this==obj){ return true ; } if(!(obj instanceof Person)){ return false ; } Person p = (Person)obj ; // 向下轉型 if(this.name.equals(p.name)&&this.age==p.age){ return true ; }else{ return false ; } } public int hashCode(){ return this.name.hashCode() * this.age ; // 定義一個公式 } public String toString(){ return "姓名:" + this.name + ";年齡:" + this.age ; } }; public class RepeatDemo02{ public static void main(String args[]){ Set<Person> allSet = new HashSet<Person>() ; allSet.add(new Person("張三",30)) ; allSet.add(new Person("李四",31)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("王五",32)) ; allSet.add(new Person("趙六",33)) ; allSet.add(new Person("孫七",33)) ; System.out.println(allSet) ; } };
SortedSet接口:

import java.util.SortedSet ; import java.util.TreeSet ; public class TreeSetDemo05{ public static void main(String args[]){ SortedSet<String> allSet = new TreeSet<String>() ; // allSet.add("A") ; // 增加內容 allSet.add("B") ; // 增加內容 allSet.add("C") ; // 增加內容 allSet.add("C") ; // 增加內容 allSet.add("C") ; // 增加內容 allSet.add("D") ; // 增加內容 allSet.add("E") ; // 增加內容 System.out.println("第一個元素:" + allSet.first()) ; System.out.println("最後一個元素:" + allSet.last()) ; System.out.println("headSet元素:" + allSet.headSet("C")) ; System.out.println("tailSet元素:" + allSet.tailSet("C")) ; System.out.println("subSet元素:" + allSet.subSet("B","D")) ; } };
總結:

最後更新:2017-04-03 14:54:25