1. 程式人生 > >Java基礎提高篇(三)持有物件

Java基礎提高篇(三)持有物件

1.新增一組元素

通過Arrays.asList()或者Collections.addAll()新增一組元素

Arrays.asList()方法接收一個數組或一個用逗號分隔的元素列表

Collections.addAll(),接收一個Collection物件,以及一個數組或一個用逗號分隔的元素列表(推薦使用這種方法)

public class Demo1 {
    public static void main(String[] args) {
        List<Integer> list1 = Arrays.asList(1, 2, 3);
        System.out.println(list1); // [1,2,3]
        // list1.add(4); 報錯java.lang.UnsupportedOperationException
        // 因為Arrays.asList底層是陣列,陣列大小無法改變
        list1.set(1, 4);   // 修改資料
        System.out.println(list1); // [1,4,3]

        Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
        Collections.addAll(collection, 4, 5, 6);
        System.out.println(collection);// [1, 2, 3, 4, 5, 6]

        Integer[] integers = { 7, 8, 9 }; 
        Collections.addAll(collection, integers);
        System.out.println(collection);  // [1, 2, 3, 4, 5, 6, 7, 8, 9]

    }
}

2.List,Set,Map的區別

先看一個小例子

class NBA{
    static Collection<Object> fill(Collection<Object> collection){
        collection.add("火箭");
        collection.add("騎士");
        collection.add("馬刺");
        collection.add("馬刺");
        return collection;
    }
    static Map fill(Map<String, String> map){
        map.put("hj","火箭"); 
        map.put("qs","騎士"); 
        map.put("mc","馬刺"); 
        map.put("mc","馬刺"); 
        return map;
    }
}

public class Demo1 {
    public static void main(String[] args) {

  System.out.println(NBA.fill(new ArrayList<>()));  //[火箭, 騎士, 馬刺, 馬刺]
  System.out.println(NBA.fill(new LinkedList<>()));  //[火箭, 騎士, 馬刺, 馬刺]

  System.out.println(NBA.fill(new HashSet<>()));   //[馬刺, 火箭, 騎士]
  System.out.println(NBA.fill(new LinkedHashSet<>()));  // [火箭, 騎士, 馬刺]
  System.out.println(NBA.fill(new TreeSet<>()));// [火箭, 馬刺, 騎士]

  System.out.println(NBA.fill(new HashMap()));   // {mc=馬刺, hj=火箭, qs=騎士}
  System.out.println(NBA.fill(new LinkedHashMap())); // {hj=火箭, qs=騎士, mc=馬刺}
  System.out.println(NBA.fill(new TreeMap()));  // {hj=火箭, mc=馬刺, qs=騎士}
    }
}

List:以特定的順序儲存元素

List有二種型別:

1.ArrayList:擅長隨機訪問元素,但是在List中間插入和移除元素比較慢

2.LinkedList:擅長在List中間插入和移除元素,但隨機訪問元素慢

Set:元素不能重複

Map:在每個槽內儲存了二個物件,即鍵與之相關聯的值。

HashSet使用了相當複雜的方式儲存元素,它是最快獲取元素的方式。

TreeSet:按照比較結果的升序儲存物件

LinkedHashSet:按照被新增的順序儲存物件

HashMap,LinkedHashMap,TreeMap和Set相類似。

迭代器

Java的Iterator只能單向移動,用於遍歷資料

ListInterator是可以雙向移動的

public class Demo1 {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3);
        // 1.獲得Iterator物件
        Iterator<Integer> iterator = list.iterator();
        // 檢查序列中是否還有元素
        while (iterator.hasNext()) {
            // 獲取序列中的下一個元素
            Integer value = iterator.next();
            System.out.println(value);
        }
        System.out.println("---------------");
        // 用foreach更簡單
        for (Integer integer : list) {
            System.out.println(integer);
        }
    }
}

LinkedList常用方法

1.getFirst()和element() 返回第一個元素,並不移除,如果List為空,丟擲NoSuchElementException.peek()在列表為空時,返回null

2.removeFirst和remove,移除並返回列表的頭,而在列表為空時丟擲NoSuchElementException.poll()在列表為空時,返回null

3.addFirst(),add(),addLast(),都將某元素插入到列表的頭尾。

4.removeLast()移除並返回列表的最後一個元素。

棧與佇列

棧是一種後進後出的容器。而佇列是一種先進先出的容器。LinkedList擁有各種棧與佇列的行為。