java代碼之美(9)---guava之Lists、Maps


guava之Lists、Maps

谷歌提供了guava包里面有很多的工具類,Lists和Maps集合工具,集合操作做了些優化提升。

1、概述

1、靜態工廠方法

  (1)Guava提供了能夠推斷范型的靜態工廠方法

List<Person> list = Lists.newArrayList();
Map<KeyType, Person> map = Maps.newLinkedHashMap();

   (2) 用工廠方法模式,我們可以方便地在初始化時就指定起始元素。

Set<Type> copySet = Sets.newHashSet(elements);
List<String> theseElements = Lists.newArrayList("alpha", "beta", "gamma");

  (3) 通過為工廠方法命名,我們可以提高集合初始化大小的可讀性.

List<Type> exactly100 = Lists.newArrayListWithCapacity(100);
List<Type> approx100 = Lists.newArrayListWithExpectedSize(100);
Set<Type> approx100Set = Sets.newHashSetWithExpectedSize(100);

 

二、Lists案例

public class ListsTest {

    public static void main(String args[]){

    List<String> list1 = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
        list1.add(i + "");
    }
        System.out.println("list1: " + list1);
        //輸出:list1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

   //2、傳入多參數
     List<String> list2 = Lists.newArrayList("1", "2", "3");
        System.out.println("list2: " + list2);
        //輸出:list2: [1, 2, 3]

   //3、傳入數組
     List<String> list3 = Lists.newArrayList(new String[]{"22", "22"});
        System.out.println("list3: " + list3);
        //輸出:list3: [22, 22]

   //4、傳入集合
     List<String> list4 = Lists.newArrayList(list1);
        System.out.println("list4: " + list4);
        //輸出:list4: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

   //5、使用條件:你確定你的容器會裝多少個,不確定就用一般形式的
    //說明:這個容器超過10個還是會自動擴容的。不用擔心容量不夠用。默認是分配一個容量為10的數組,不夠將擴容
    //整個來說的優點有:節約內存,節約時間,節約性能。代碼質量提高。
    List<String> list = Lists.newArrayListWithExpectedSize(10);
    //這個方法就是直接返回一個10的數組。
    List<String> list_ = Lists.newArrayListWithCapacity(10);
   }
}

 

三、Maps案例

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

            //1、Maps.newHashMap()獲得HashMap();
            Map<Integer, Integer> map0 = Maps.newHashMap();
            for (int i = 0; i < 10; i++) {
                map0.put(i, i);
            }
            System.out.println("map0:" + map0);
            //輸出:map0:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}

            //2、傳入map0參數構建map
            Map<Integer, Integer> map1 = Maps.newHashMap(map0);
            map1.put(10, 10);
            System.out.println("map1:" + map1);
            //輸出:map1:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10}


            //3、使用條件:你確定你的容器會裝多少個,不確定就用一般形式的
            //說明:這個容器超過3個還是會自動擴容的。不用擔心容量不夠用。默認是分配一個容量為16的數組,不夠將擴容
            Map<Integer, Integer> map2 = Maps.newHashMapWithExpectedSize(3);
            map2.put(1, 1);
            map2.put(2, 2);
            map2.put(3, 3);
            System.out.println("map2:" + map2);
            //輸出:map2:{1=1, 2=2, 3=3}

            //4、LinkedHashMap<K, V> 有序map
            //Map<Integer,Integer> map3 = Maps.newLinkedHashMap();
            //Map<Integer,Integer> map3 = Maps.newLinkedHashMapWithExpectedSize(11);
            Map<Integer, Integer> map3 = Maps.newLinkedHashMap(map1);
            map3.put(11, 11);
            System.out.println("map3:" + map3);
            //輸出:map3:{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=11}
            
            outMapKeyValue(map3);
            
        }

        /**
         * 遍歷Map的四種方法
         */
        private static void outMapKeyValue(Map<Integer, Integer> map3) {

            //1.通過Map.entrySet遍歷key和value
            for (Map.Entry<Integer, Integer> integerEntry : map3.entrySet()) {
                System.out.println("key:" + integerEntry.getKey() + " value:" + integerEntry.getValue());
            }

            //2.通過Map.entrySet使用iterator遍歷key和value-----不推薦,直接用上面的for each循環代替此方法
            Iterator<Map.Entry<Integer, Integer>> it = map3.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Integer, Integer> entry = it.next();
                System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
            }

            //3.通過Map.keySet遍歷key;根據key得到value
            for (Integer integer : map3.keySet()) {
                System.out.println("key:" + integer + " value:" + map3.get(integer));
            }

            //4.通過Map.values()遍歷所有的value,但不能遍歷key
            for (Integer integer : map3.values()) {
                System.out.println("value:" + integer);
            }
        }
}

 

想太多,做太少,中間的落差就是煩惱。想沒有煩惱,要么別想,要么多做。中校【18】

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM