Java 集合框架 : Collection、Map
1. Collection接口是Java集合框架的基本接口,所所有集合都繼承該接口。
1.1 方法 :
public interface Collection<E> extends Iterable<E> {//向集合添加元素,成功返回true,失敗返回falseboolean add(E e);//用于返回一個實現了Iterator接口的對象。Iterator<E> iterator();...}1.2 Iterator 迭代器
public interface Iterator<E> {//如果迭代器對象還有很多元素訪問,返回true,boolean hasNext();//逐個訪問集合中的每個元素,達到集合的末尾,//會拋出NoSuchElementException E next();default void remove() {throw new UnsupportedOperationException("remove");}default void forEachRemaining(Consumer<? super E> action) {Objects.requireNonNull(action);while (hasNext())action.accept(next());}}1.3 遍歷集合
方法1:用迭代器
Collection<String> c = ...;Iterator<String> iterator = c.iterator();while (iterator.hasNext()){//iterator.next() 越過一個元素,并返回剛剛越過的那個元素String element = iterator.next();//在調用過next方法之后,才可以調用remove()方法,// 來刪除剛剛越過的那個元素 iterator.remove();...}方法2:用for each
Collection<String> c = ...;for (String element : c){do something with "element"...}方法3:java8 lambda表達式 forEachRemaning()
Collection<String> c = ...;Iterator<String> iterator = c.iterator();iterator.forEachRemaining( ele -> {do something with "element"...});※ 在刪除元素時還可以調用一個更簡單的方法:
Collection<String> c = ...;c.removeIf(ele -> {if (ele ...){return true;}else {return false;}});2. List 有序集合
遍歷元素的方法:
2.1 迭代器訪問 :適用鏈表結構集合
2.2 整數索引訪問——又稱隨機訪問 :數組結構的集合
2.3 ArrayList 數組結構 從中刪除一個元素,之后所有的元素都要向前移動,開銷很大,增加元素同理。
2.3 LinkedList 鏈表結構 ,每個元素存儲在獨立的節點中,每個節點都有指向前一個元素和后一個元素的引用。
3.Set集?
3.1 HashSet 散列集,實現了基于散列表的集 ,位置隨機,無序集合。
add()?
contains() 用來查看某個元素是已經存在集中。
3.2 TreeSet 樹集 ,有序集合。樹結構(紅黑樹 red-black tree)
import java.util.*;/*** This program sorts a set of item by comparing their descriptions.* @version 1.12 2015-06-21* @author Cay Horstmann*/ public class TreeSetTest {public static void main(String[] args){SortedSet<Item> parts = new TreeSet<>();parts.add(new Item("Toaster", 1234));parts.add(new Item("Widget", 4562));parts.add(new Item("Modem", 9912));System.out.println(parts);NavigableSet<Item> sortByDescription = new TreeSet<>(Comparator.comparing(Item::getDescription));sortByDescription.addAll(parts);System.out.println(sortByDescription);} }?
import java.util.*;/*** An item with a description and a part number.*/ public class Item implements Comparable<Item> {private String description;private int partNumber;/*** Constructs an item.* * @param aDescription* the item's description* @param aPartNumber* the item's part number*/public Item(String aDescription, int aPartNumber){description = aDescription;partNumber = aPartNumber;}/*** Gets the description of this item.* * @return the description*/public String getDescription(){return description;}public String toString(){return "[description=" + description + ", partNumber=" + partNumber + "]";}public boolean equals(Object otherObject){if (this == otherObject) return true;if (otherObject == null) return false;if (getClass() != otherObject.getClass()) return false;Item other = (Item) otherObject;return Objects.equals(description, other.description) && partNumber == other.partNumber;}public int hashCode(){return Objects.hash(description, partNumber);}public int compareTo(Item other){int diff = Integer.compare(partNumber, other.partNumber);return diff != 0 ? diff : description.compareTo(other.description);} }?4. Deque 雙端隊列,
4.1 ArrayDeque、LinkList
4.2 java.util.Queue<E>?
boolean add(E element); //添加元素到隊尾,返回true,如果隊列已滿,返回falseboolean offer(E element);//添加元素到隊尾,返回true,如果隊列已滿,拋出IllegalStateExceptionE remove() // 刪除并返回這個隊列的頭部元素,隊列空,拋出NoSuchElementExceptionE poll() //刪除并返回這個隊列的頭部元素,隊列空,返回nullE element() // 返回隊列的頭部元素,隊列空,則拋出NoSuchElementExceptionE peek() //返回隊列的頭部元素,隊列空,返回null4.3 PriorityQueue 優先級隊列
優先級隊列中的元素可以按照任意的順序插入,但總是按照排序的順序進行檢索,無論何時調用remove() 方法,總會獲得當前隊列中優先級最小的元素
......
5. HashMap 散列映射、TreeMap 樹映射 ,
散列映射對鍵值進行散列,樹映射用鍵的整體順序對元素進行排序,并將其組織成搜索樹,散列或比較函數只能作用于鍵。與鍵關聯的值不能進行散列或比較。
無排序要求的話 選擇散列映射,速度快。
5.1 遍歷集合?
for each遍歷
Map<String,Object> hashMap = new HashMap<>();hashMap.put("name","Jack");hashMap.put("dept","開發部");hashMap.put("Sal",4000);//foreach遍歷for (Map.Entry<String,Object> ele : hashMap.entrySet()){String key = ele.getKey();Object val = ele.getValue();System.out.print(key +"=" + val +",");}for each + lambda表達式遍歷
//Lambda表達式遍歷hashMap.forEach((k,v) -> {System.out.print(k + "=" + v + ",");});?
?
?
轉載于:https://www.cnblogs.com/lovleo/p/11323241.html
總結
以上是生活随笔為你收集整理的Java 集合框架 : Collection、Map的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对C++中new的认识
- 下一篇: Java基础整理