集合類
day 10
Collection類
-
public interface Collection<E> extends Iterable<E>
-
常用方法:
public boolean add(E e) : 把給定的對象添加到當前集合中 。 public void clear() :清空集合中所有的元素。 public boolean remove(E e) : 把給定的對象在當前集合中刪除。 public boolean contains(E e) : 判斷當前集合中是否包含給定的對象。 public boolean isEmpty() : 判斷當前集合是否爲空。 public int size() : 返回集合中元素的個數。 public Object[] toArray() : 把集合中的元素,存儲到數組中。
-
Iterator接口
-
常用方法:
public Iterator iterator() : 獲取集合對應的迭代器,用來遍歷集合中的元素的。 public E next() :返回迭代的下一個元素。 public boolean hasNext() :如果仍有元素可以迭代,則返回 true
-
如果集合中已經沒有元素了,還繼續使用迭代器的next方法,將會發生
java.util.NoSuchElementException
沒有集合元素的錯誤。
-
List集合接口
-
是一個有序、帶索引、可重複的集合
ArrayList集合
-
ArrayList集合底層是一個數組結構,元素增刪慢,查找快。
LinkedList集合
-
LinkedList集合是一個雙向鏈表結構,底層是一個鏈表結構,元素增刪快,查詢慢。
-
常用方法:
public void addFirst(E e) :將指定元素插入此列表的開頭。 public void addLast(E e) :將指定元素添加到此列表的結尾。 public E getFirst() :返回此列表的第一個元素。 public E getLast() :返回此列表的最後一個元素。 public E removeFirst() :移除並返回此列表的第一個元素。 public E removeLast() :移除並返回此列表的最後一個元素。 public E pop() :從此列表所表示的堆棧處彈出一個元素。 public void push(E e) :將元素推入此列表所表示的堆棧。 public boolean isEmpty() :如果列表不包含元素,則返回true。
-
Set集合接口
-
是一個無序、不可重複的集合
HashSet集合
-
保證元素唯一性 的方式依賴於: hashCode 與 equals 方法。
-
給HashSet中存放自定義類型元素時,需要重寫對象中的hashCode和equals方法,建立自己的比較方式,才能保證HashSet集合中的對象唯一。
LinkedHashSet集合
-
在HashSet下面有一個子類 java.util.LinkedHashSet ,它是鏈表和哈希表組合的一個數據存儲結構。
-
特點是一個有序的集合。
-
Collections工具類
-
常用方法
public static <T> boolean addAll(Collection<T> c, T... elements) :往集合中添加一些元素。 public static void shuffle(List<?> list) 打亂順序 :打亂集合順序。 public static <T> void sort(List<T> list) :將集合中元素按照默認規則排序。 public static <T> void sort(List<T> list,Comparator<? super T> ) :將集合中元素按照指定規則排序。
-
Comparator比較器
Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.charAt(0) ‐ o1.charAt(0); } }); //如果要按照升序排序, 則o1 小於o2,返回(負數),相等返回0,01大於02返回(正數) 如果要按照 降序排序 則o1 小於o2,返回(正數),相等返回0,01大於02返回(負數)
Map集合接口
-
常用方法
public V put(K key, V value) : 把指定的鍵與指定的值添加到Map集合中。 public V remove(Object key) : 把指定的鍵 所對應的鍵值對元素 在Map集合中刪除,返回被刪除元素的值。 public V get(Object key) 根據指定的鍵,在Map集合中獲取對應的值。 boolean containsKey(Object key) 判斷集合中是否包含指定的鍵。 public Set<K> keySet() : 獲取Map集合中所有的鍵,存儲到Set集合中。 public Set<Map.Entry<K,V>> entrySet() : 獲取到Map集合中所有的鍵值對對象的集合(Set集合)。
-
Map集合遍歷方式
-
Map集合不能直接使用迭代器或者foreach進行遍歷。但是轉成Set之後就可以使用了。
-
方法一:
-
獲取集合中的所有鍵組成集合map.keySet();
-
遍歷鍵集合,通過Key值找到Value值。
HashMap<String, String> map = new HashMap<String,String>(); //獲取所有的鍵 獲取鍵集 Set<String> keys = map.keySet(); // 遍歷鍵集 得到 每一個鍵 for (String key : keys) { //key 就是鍵 //獲取對應值 String value = map.get(key); System.out.println(key+"的CP是:"+value); }
-
-
方法二:
-
Map 中存放的是兩種對象,一種稱爲key(鍵),一種稱爲value(值),它們在在 Map 中是一一對應關係,這一對對象又稱做 Map 中的一個Entry(項) 。 Entry 將鍵值對的對應關係封裝成了對象。
-
Entry表示了一對鍵和值,那麼也同樣提供了獲取對應鍵和對應值得方法。
-
public K getKey() :獲取Entry對象中的鍵。
-
public V getValue() :獲取Entry對象中的值。
-
-
在Map集合中也提供了獲取所有Entry對象的方法:
-
public Set<Map.Entry<K,V>> entrySet() : 獲取到Map集合中所有的鍵值對對象的集合(Set集合)
Set<Entry<String,String>> entrySet = map.entrySet(); for (Entry<String, String> entry : entrySet) { // 解析 String key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"的CP是:"+value); }
-
-
-
-
HashMap集合
-
HashMap保證成對元素唯一,並且查詢速度很快
-
-
LinkedHashMap集合
-
在HashMap的基礎上還保證,元素存取有序。
-
JDK 9 新特性
public class HelloJDK9 { public static void main(String[] args) { Set<String> str1=Set.of("a","b","c"); //str1.add("c");這裏編譯的時候不會錯,但是執行的時候會報錯,因爲是不可變的集合 System.out.println(str1); Map<String,Integer> str2=Map.of("a",1,"b",2); System.out.println(str2); List<String> str3=List.of("a","b"); System.out.println(str3); } }
-
of()方法只是Map,List,Set這三個接口的靜態方法,其父類接口和子類實現並沒有這類方法,比如 HashSet,ArrayList等;
-
返回的集合是不可變的;
Debug追蹤
-
f5執行下一行代碼
-
f6執行下一行代碼如果方法裏面有嵌套的方法回調到那個嵌套的方法
-
f7從一個方法跳出,到一行代碼中
-
f8從一個斷點跳到下一個斷點
日常練習
紅名單校驗和短信羣發系統
package groupSMS; import java.util.*; /* * 1、以下手機號碼段(每隔一個號添加一次)添加到Map集合中, 2、從控制檯輸入要發送信息的號碼,如果發送多個,請用英文“,”號隔開,輸入短信內容。 3、編寫一個短信發送類,號碼發送前,先判斷號碼是否爲空,再判斷是否爲紅名單用戶(手機號碼在集合中), 如果爲紅名單,輸出紅名單數據,並計算紅名單校驗耗費的時間,如果條件都滿足,發送短信,短信發送成功概率爲98%,最後展示發送成功的短信號碼和內容。 末四位的號碼區間在1000-9999 * * */ public class RedList { public static ArrayList<String> redList =new ArrayList<>(); public void createRedList(String[] headNum,String[][] middleNum){ if (headNum.length!=middleNum.length){ System.out.println("頭號碼數組和中間號碼數組不匹配,請重新輸入"); return; }else{ for (int i = 0; i < headNum.length; i++) { for (int j = 0; j < middleNum[i].length; j++) { for (int k = 1000;k<=9999;k+=2){ redList.add(headNum[i]+middleNum[i][j]+k); } } } } } } class GroupSMS { public static ArrayList<String> groupNUM =new ArrayList<>(); public static ArrayList<String> noLegalNum =new ArrayList<>(); public static ArrayList<String> isRedList =new ArrayList<>(); public static ArrayList<String> sendList =new ArrayList<>(); public static ArrayList<String> successList =new ArrayList<>(); public static String TXT; public static void SendSMS(){ String phoneNum = GroupSMSUtil.getPhoneNum(); GroupSMSUtil.groupNUM(phoneNum); for (String s : groupNUM) { boolean legal = GroupSMSUtil.isLegal(s); if (!legal){ noLegalNum.add(s); }else { sendList.add(s); } } System.out.println("紅名單檢驗時長爲:"+GroupSMSUtil.timeSUM(groupNUM)+"毫秒"); GroupSMSUtil.getTXT(); for (String s : sendList) { if (GroupSMSUtil.getRandom()){ successList.add(s); } } System.out.println("================================="); System.out.println("輸入的電話號碼:"+groupNUM.toString()); System.out.println("不合法電話號碼:"+noLegalNum.toString()); System.out.println("紅名單電話號碼:"+isRedList.toString()); System.out.println("發送的電話號碼:"+sendList.toString()); System.out.println("成功的電話號碼:"+successList.toString()); System.out.println(successList.size()!=0?("短信發送成功,發送的內容:"+TXT):("短信發送失敗!!")); System.out.println("================================="); } } class GroupSMSUtil{ //判斷電話號碼是否合法 public static boolean isLegal(String phoneNum) { for (int i = 0; i < phoneNum.length(); i++) { if (phoneNum.charAt(i)<48||phoneNum.charAt(i)>57){ return false; } } return (phoneNum.length() == 11) && (phoneNum.charAt(0) == '1'); } //判斷電話號碼是否爲紅名單 public static boolean isRedList(String phoneNum) { return RedList.redList.contains(phoneNum); } //獲取執行時間 public static long getTime(){ return new Date().getTime(); } //獲取成功概率 public static boolean getRandom(){ return (new Random().nextInt(100))<=98; } //獲取輸入的電話字符串 public static String getPhoneNum(){ Scanner sc =new Scanner(System.in); String str; System.out.println("請輸入電話號碼,相鄰號碼用“,”隔開"); while (true){ str=sc.nextLine(); if (str.length()%12==11){ break; } System.out.println("請合法性輸入"); } return str; } //整理羣發電話集合 public static void groupNUM(String str){ String[] split = str.split(","); for (String s : split) { GroupSMS.groupNUM.add(s); } } //紅名單校驗時長 public static long timeSUM(Collection<String> col){ long start = GroupSMSUtil.getTime(); for (String s : col) { boolean redList = GroupSMSUtil.isRedList(s); if (redList){ GroupSMS.isRedList.add(s); GroupSMS.sendList.remove(s); } } long end = GroupSMSUtil.getTime(); return end - start; } //獲取文本 public static void getTXT(){ Scanner sc =new Scanner(System.in); System.out.println("請輸入要發送的內容"); String s = sc.nextLine(); GroupSMS.TXT=s; } }
package groupSMS; public class Test { public static void main(String[] args) { String[] headNum = new String[]{"136", "137", "181", "180", "150"}; String[][] middleNum = new String[][] {{"0371", "0766", "7335", "7362", "6385", "0769", "7494", "3381", "7496", "7370"}, {"3383", "3319", "0088", "8361", "3315", "8168", "8151", "0386"}, {"3788", "3789", "3782", "3787", "0394", "3567", "2234", "0382"}, {"3951", "0169", "3991", "3955", "3928", "3788", "0387", "3997", "3923"}, {"0381", "3719", "0371", "3816", "0389", "3681", "9326", "3837", "3802"}}; RedList redList =new RedList(); redList.createRedList(headNum,middleNum); GroupSMS.SendSMS(); } }