jav核心(十四):集合類型操作:Collection、List、Set;Map集合;Iterator叠代器
阿新 • • 發佈:2019-03-20
equal exc ron 添加 推出 使用 映射關系 不同的 stat 一、java.util.List
1、List總結
3、Vertor向量類型數據輸出:Vertor --> Iterator | Enumeration
Java.util.Vertor是JDK1.0中設計的向量類型。後來在JDK1.2中新增了Collection、List、Set接口,為保持集合統一型,修改Vertor類的定義,使其實現了List接口。與新集合的實現不同,Vertor是同步的。
Java.util.Enumeration是JDK1.0中定義的接口,和Java.util.Iterator(JDK1.2出現)叠代器功能類似。其中Enumeration接口包含兩個方法:
3、HashMap和HashTable的區別
4、將Map中的數據,通過叠代器輸出:Map —> Set —> Iterator
- List中允許保存重復的數據;
- List中允許保存多個null;
- 常用實現類:ArrayList【推薦使用】、Vector、LinkedList;
- List中數據是有序的,按照數據添加順序進行排序;
- 由於List是有序的,因此List新增加了一個方法:E get(int index) ,【Collection、Set接口均沒有get(int index)方法】。
NO | 區別點 | ArrayList | Vector |
1 | 推出時間 | JDK1.2,屬於新增類 | JDK1.0,屬於舊類 |
2 | 性能 | 異步處理 | 同步處理 方法采用synchronized修飾,為同步方法 |
3 | 安全 | 非線程安全 | 線程安全 |
4 | 數據輸出方式 | Iterator、ListIterator、foreach | Iterator、ListIterator、foreach、 Enumeration |
- hasMoreElements():測試此枚舉是否包含更多的元素。
- nextElement():如果此枚舉對象至少還有一個可提供的元素,則返回此枚舉的下一個元素。
public class Test { public static void main(String[] args) { List<String> vertor = new Vector<String>(); vertor.add("A"); vertor.add("B"); vertor.add("C"); //輸出數據:使用叠代器Iterator Iterator<String> iterator = vertor.iterator(); while (iterator.hasNext()){ String data = iterator.next(); System.out.println(data); } System.out.println("*******"); //輸出數據:使用Enumeration Enumeration<String> enumeration = ((Vector<String>) vertor).elements(); while (enumeration.hasMoreElements()){ String data = enumeration.nextElement(); System.out.println(data); } } }
//程序運行結果: A B C ******* A B C
二、Java.util.Set 1、Set總結 API描述:一個不包含重復元素的 collection。更正式地說,set 不包含滿足 e1.equals(e2) 的元素對 e1 和 e2,並且最多包含一個 null 元素。正如其名稱所暗示的,此接口模仿了數學上的 set 抽象。
- Set不允許保存重復的數據【不同實現類,對重復數據的評定標準不一樣】;
- 常用實現類:HashSet、TreeSet;
- HashSet允許保存一個null,TreeSet不允許保存null;
- HashSet中的數據是無序的;
- TreeSet中的數據是有序的,數據按照升序排列【通過調用數據元素的compareTo()方法進行排序,因此數據元素需要實現Comparable接口,否則會拋出ClassCastException異常】。
/** * 按照規範重寫Book類的equals()方法、hashCode()方法 */ class Book { private int price; private String name; public Book(int price, String name) { this.price = price; this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Book book = (Book) o; return price == book.price && Objects.equals(name, book.name); } @Override public int hashCode() { return Objects.hash(price, name); } @Override public String toString() { return this.name + "--" + this.price; } } public class Test { public static void main(String[] args) { Book book = new Book(100, "java開發"); Book book1 = new Book(100, "java開發"); Book book2 = new Book(100, "設計模式"); Book book3 = new Book(10, "abc"); System.out.println("*********"); System.out.println(book.hashCode()); //-1166307760 System.out.println(book1.hashCode()); //-1166307760 System.out.println(book2.hashCode()); //1100964814 System.out.println(book.equals(book1)); //true System.out.println(book.equals(book2)); //false System.out.println("*********"); Set<Book> hashSet = new HashSet<>(); hashSet.add(book); hashSet.add(book1); //book1 與 book 重復,不進行保存 hashSet.add(book2); //book2 不重復 hashSet.add(book3); //book3 不重復 System.out.println(hashSet); } }
//程序執行結果: ********* -1166307760 -1166307760 1100964814 true false ********* [設計模式--100, abc--10, java開發--100]
(2)數據重復測試二:e1.equals(e2)為true,但hashCode()值不同的情況
class Book { private int price; private String name; public Book(int price, String name) { this.price = price; this.name = name; } @Override public String toString() { return this.name + "--" + this.price; } //任何對象之間比較,均返回true public boolean equals(Object o) { return true; } //使用隨機數生成hashCode public int hashCode() { return Objects.hash(Math.random()); } } public class Test { public static void main(String[] args) { Book book = new Book(100, "java開發"); Book book1 = new Book(100, "java開發"); Book book2 = new Book(100, "設計模式"); System.out.println("*********"); System.out.println(book.hashCode()); System.out.println(book1.hashCode()); System.out.println(book2.hashCode()); System.out.println(book.equals(book1)); System.out.println(book.equals(book2)); System.out.println("*********"); Set<Book> hashSet = new HashSet<>(); hashSet.add(book); hashSet.add(book1); //book1 與 book 重復 hashSet.add(book2); //book2 不重復 System.out.println(hashSet); } }
//程序執行結果 ********* 920603493 -1541131927 1273275611 true true ********* [java開發--100, 設計模式--100, java開發--100]
3、TreeSet中,數據重復的評定標準: TreeSet<T>中的元素T必須實現java.lang.Comparable接口,重寫接口定義的 public int compareTo(T o) 方法。 當且僅當 e1.compareTo(e2) 返回結果為0時,認為e1、e2重復。 TreeSet不使用equals()、hashCode()方法進行判定。
class Book implements Comparable{ private int price; private String name; public Book(int price, String name) { this.price = price; this.name = name; } @Override public String toString() { return this.name + "--" + this.price; } @Override public boolean equals(Object o) { return true; } @Override public int hashCode() { return Objects.hash("1111"); } @Override public int compareTo(Object o) { return 1; } } public class Test { public static void main(String[] args) { Book book = new Book(100, "java開發"); Book book1 = new Book(100, "java開發"); Book book2 = new Book(100, "設計模式"); System.out.println("*********"); Set<Book> hashSet = new TreeSet<>(); hashSet.add(book); hashSet.add(book1); hashSet.add(book2); System.out.println(hashSet); } }
//程序執行結果 ********* [java開發--100, java開發--100, 設計模式--100]
4、說明:一般情況下,比較兩個對象是否相同,通過hashCode()和equals()方法判斷
- 首先判斷hashCode()值是否相等,如果不相等,則認為e1、e2不相同;如果相等,則繼續判斷e1.equals(e2)是否為true,若為true則認為e1、e2相同,否則認為e1、e2不相同;
- Comparable的compareTo()方法只是作為對象的排序使用,但並不能真正區分兩個對象是否相同。
- 將鍵映射到值的對象。一個映射不能包含重復的鍵;每個鍵最多只能映射一個值。
//語法定義2、Map接口中的方法
public interface Map<K,V>
NO | 方法名稱 |
1 | public V put(K key, V value) 將指定的值與此映射中的指定鍵相關聯(可選操作)。 |
2 | public V get(Object key) 返回此映射中映射到指定鍵的值。 |
3 | public Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射關系的 set 視圖。 |
4 | public Set<K> keySet() 返回此映射中包含的鍵的 set 視圖。 |
NO | 區別點 | HashMap | HashTable |
1 | 推出時間 | JDK1.2,屬於新增類 | JDK1.0,屬於舊類 |
2 | 性能 | 異步處理 | 同步處理 方法采用synchronized修飾 |
3 | 安全 | 非線程安全 | 線程安全 |
4 | 是否允許null | 允許key或value為null | key和value均不能為null |
- 調用Map.entrySet()方法,將Map集合轉換為Set集合,其中Set中的泛型為:Map.Entry<K,V>;
- 調用Set.iterator()方法,將Set集合轉換為Iterator叠代器,泛型依然是:Map.Entry<K,V>;
- 調用Iterator的hasNext()、next()方法,循環取出Map.Entry<K,V>接口對象;
- 調用Map.Entry接口的getKey()獲取key、getValue()獲取value。
class Book { private int price; private String name; public Book(int price, String name) { this.price = price; this.name = name; } @Override public String toString() { return this.name + "--" + this.price; } } public class Test { public static void main(String[] args) { Book book = new Book(100, "java開發"); Book book1 = new Book(100, "java開發"); Book book2 = new Book(56, "設計模式"); System.out.println("*********"); Map<String,Book> map = new HashMap<>(); map.put("book",book); map.put("book1",book1); map.put("book2",book2); //將Map中的數據,通過叠代器輸出:Map —> Set —> Iterator Set<Map.Entry<String,Book>> set = map.entrySet(); Iterator<Map.Entry<String,Book>> iterator = set.iterator(); while (iterator.hasNext()){ Map.Entry<String,Book> entry = iterator.next(); String key = entry.getKey(); Book value = entry.getValue(); System.out.println(key+" : "+value); } } }
//程序執行結果 ********* book2 : 設計模式—56 book1 : java開發--100 book : java開發--100
jav核心(十四):集合類型操作:Collection、List、Set;Map集合;Iterator叠代器