1. 程式人生 > >Java類集 1 List, Set基本使用

Java類集 1 List, Set基本使用

.com list span iterator next() oracle開發 java pri jsp開發

首先看下繼承結構:

技術分享圖片

ArrayList(常用):

技術分享圖片
 1  /**
 2          * List接口繼承Collection接口
 3          * ArrayList, Vector為List接口的實現類
 4          * add()添加新元素,remove()刪除指定位置元素,get()通過索引獲取對應位置元素,set()設置索引位置元素
 5          * Iterator(最常用)接口實現集合遍歷
 6          */
 7         List list = new ArrayList<String>();
8 list.add("Hello aa"); 9 list.add("Hello bb"); 10 list.add("Hello cc"); 11 list.add("Hello dd"); 12 list.add("Hello dd"); 13 list.add("Hello dd"); 14 list.remove(0); 15 System.out.println(list); 16 System.out.println(list.get(0));
17 list.set(0, "nihao"); 18 System.out.println(list.get(0)); 19 Iterator iter = list.iterator(); 20 while (iter.hasNext()) { 21 System.out.println(iter.next()); 22 }
View Code

Vector(舊版):

技術分享圖片
 1 /**
 2          * List接口繼承Collection接口
 3          * ArrayList, Vector為List接口的實現類
4 * add()添加新元素,remove()刪除指定位置元素,get()通過索引獲取對應位置元素,set()設置索引位置元素 5 * Iterator(最常用)接口實現集合遍歷 6 */ 7 List list = new Vector<String>(); 8 list.add("Hello aa"); 9 list.add("Hello bb"); 10 list.add("Hello cc"); 11 list.add("Hello dd"); 12 list.add("Hello dd"); 13 list.add("Hello dd"); 14 list.remove(0); 15 System.out.println(list); 16 System.out.println(list.get(0)); 17 list.set(0, "nihao"); 18 System.out.println(list.get(0)); 19 Iterator iter = list.iterator(); 20 while (iter.hasNext()) { 21 System.out.println(iter.next()); 22 }
View Code

兩者的主要區別:

ArrayList是JDK1.2新加入的, Vector在JDK1.0中就已經出現, Vector是同步的, 所以是線程安全的(當然性能會低),ArrayList是異步的,所以線程不安全,日常開發中Vector已經很少用了,ArrayList更常用一些,兩者的用法基本一致。

HashSet, TreeSet:

技術分享圖片
 1 /**
 2          * Set(集合)是不重復的, Collection集合的子接口
 3          */
 4         Set hashSet = new HashSet<String>();
 5         hashSet.add("1111");
 6         hashSet.add("1111");
 7         hashSet.add("2222");
 8         hashSet.add("3333");
 9         hashSet.add("X");
10         hashSet.add("C");
11         hashSet.add("E");
12         hashSet.add("A");
13         System.out.println(hashSet); // 發現HashSet是無序的
14         Set treeSet = new TreeSet<String>();
15         treeSet.add("1111");
16         treeSet.add("1111");
17         treeSet.add("2222");
18         treeSet.add("3333");
19         treeSet.add("X");
20         treeSet.add("C");
21         treeSet.add("E");
22         treeSet.add("A");
23         System.out.println(treeSet); // 發現TreeSet是有序的
View Code

用HashSet, TreeSet存儲自定義類Book:

Book類:

技術分享圖片
 1 public class Book {
 2     private String title;
 3     private double price;
 4     public Book(){
 5         this("", 0.0);
 6     }
 7     public Book(String title, double price){
 8         this.title = title;
 9         this.price = price;
10     }
11 }
View Code

執行如下代碼:

技術分享圖片
 1 public class Ph {
 2     public static void main(String[] args) {
 3         Set treeSet = new TreeSet<String>();
 4         treeSet.add(new Book("Java開發", 29.8));
 5         treeSet.add(new Book("Java開發", 29.8));
 6         treeSet.add(new Book("JSP開發", 39.8));
 7         treeSet.add(new Book("Oracle開發", 79.8));
 8         System.out.println(treeSet);
 9     }
10 }
View Code

運行時異常:

Exception in thread "main" java.lang.ClassCastException: MyPackageOne.Book cannot be cast to java.lang.Comparable
看到Comparable明白TreeSet通過Comparable接口實現讓元素不重復和排序,所以運用TreeSet存儲自定義類時應實現Comparable接口並實現CompareTo方法

故Book類此時應為:

技術分享圖片
 1 public class Book implements Comparable<Book>{
 2     private String title;
 3     private double price;
 4     public Book(){
 5         this("", 0.0);
 6     }
 7     public Book(String title, double price){
 8         this.title = title;
 9         this.price = price;
10     }
11 
12     @Override
13     public int compareTo(Book o) {
14         if(price > o.price){
15             return 1;
16         }else if(price < o.price){
17             return -1;
18         }else{
19             // 註意應該把所有元素的比較填入, 不然有一個屬性相同可能就會誤以為相同元素
20             return title.compareTo(o.title);
21         }
22     }
23 }
View Code

而此時換成HashSet,發現會有重復元素,因為HashSet通過HashCode()和equals()方法實現去重,故此時Book類應為:

技術分享圖片
 1 import java.util.Objects;
 2 
 3 public class Book {
 4     private String title;
 5     private double price;
 6 
 7     public Book() {
 8         this("", 0.0);
 9     }
10 
11     public Book(String title, double price) {
12         this.title = title;
13         this.price = price;
14     }
15 
16     @Override
17     public boolean equals(Object o) {
18         if (this == o) return true;
19         if (o == null || getClass() != o.getClass()) return false;
20         Book book = (Book) o;
21         return Double.compare(book.price, price) == 0 &&
22                 Objects.equals(title, book.title);
23     }
24 
25     @Override
26     public int hashCode() {
27         return Objects.hash(title, price);
28     }
29 
30     @Override
31     public String toString() {
32         return title + price;
33     }
34 }
View Code

Java類集 1 List, Set基本使用