java代码(9) ---guava之Lists、Maps


 guava之Lists、Maps


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

一、概述

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);

 2、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);

        //传入多参数
        List<String> list2 = Lists.newArrayList("1", "2", "3");
        System.out.println("list2:"+list2);

        //传入数组
        List<String> list3=Lists.newArrayList(new String[]{"22","33"});
        System.out.println("list3:"+list3);
        //传入集合
        List<String> list4=Lists.newArrayList(list1);
        System.out.println("list4:"+list4);

        //使用条件:你确定你的容器会装多少个,不确定就用一般形式的
        //说明:这个容器超过10个还是会自动扩容,不用担心容量不够用,默认是分配一个容量为10的数组,不够将扩容
        //整个来说的优点有:节约内存,节约时间,节约性能,代码质量提高
        List<String> list=Lists.newArrayListWithExpectedSize(10);
        //这个方法就是直接返回一个10的数组
        List<String>  list5=Lists.newArrayListWithCapacity(10);

    }

}

三、Maps案例

public class MapsTest {
    public static void main(String[] args) {
        //1 Maps.newHashMap() 获得HashMap();
        Map<Integer,Integer> map1= Maps.newHashMap();
        for (int i = 0; i < 10; i++) {
            map1.put(i,i);
        }
        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}

        //2、传入map1参数构建map
        Map<Integer,Integer> map2 = Maps.newHashMap(map1);
        map2.put(10,10);
        System.out.println("map2:"+map2);
        //输出 :map2:{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> map3 = Maps.newHashMapWithExpectedSize(3);
        map3.put(1,1);
        map3.put(2,2);
        map3.put(3,3);
        System.out.println(map3);
        //输出: {1=1, 2=2, 3=3}

        //4、LinkedHashMap<K, V> 有序map
        //Map<Integer,Integer> map4 = Maps.newLinkedHashMap();
        //Map<Integer,Integer> map4 = Maps.newLinkedHashMapWithExpectedSize(11);
        Map<Integer, Integer> map4 = Maps.newLinkedHashMap(map1);
        map4.put(11, 11);
        System.out.println("map3:" + map4);
        //输出: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(map4);

    }

    /**
     * 遍历map的四种方法
     * @param map4
     */
    private static void outMapKeyValue(Map<Integer,Integer> map4){
        //1:通过Map.entrySet遍历key和value
        for (Map.Entry<Integer, Integer> integerEntry : map4.entrySet()) {
            System.out.println("key:"+integerEntry.getKey()+"value:"+integerEntry.getValue());
        }

        //2:通过Map.entrySet使用iterator遍历key和value --不推荐,直接使用上面的for each循环替代此方法
        Iterator<Map.Entry<Integer, Integer>> it = map4.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 : map4.keySet()) {
            System.out.println("key:"+integer+"value:"+map4.get(integer));
        }
        //4通过Map.values遍历所有的value,但不能遍历key
        for (Integer integer : map4.values()) {
            System.out.println("value:"+integer);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/zhenbian/p/12942597.html