經驗不在于年限,在于積累---專注互聯網軟件開發

          把工作當事業做,把項目當作品做!

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            55 Posts :: 0 Stories :: 66 Comments :: 0 Trackbacks
           1package googleCollections;
           2
           3import java.util.ArrayList;
           4import java.util.Arrays;
           5import java.util.Collections;
           6import java.util.List;
           7import java.util.Map.Entry;
           8
           9import com.google.common.collect.ImmutableList;
          10import com.google.common.collect.ImmutableMap;
          11import com.google.common.collect.ImmutableSet;
          12
          13/**
          14 * Copyright (C): 2009
          15 * @author 陳新漢 
          16 * @version 創建時間:Jan 12, 2010 11:10:05 PM
          17 */

          18
          19/**
          20 * 不可變集合類
          21 * 適合作為內容數據不可變的容器類
          22 * 例如:常量數組、常量Map等等。
          23 * 
          24 * 注意:為了實現不可變集合,JDK 5.0里面是通過Collections.unmodifiableList(list)實現的!
          25 */

          26public class ImmutableListTest {
          27    
          28    public static final List<String> imlist; //以前實現(方式一)
          29    public static final List<String> imlist2; //以前實現(方式二)
          30    
          31    static{
          32        List<String> list=new ArrayList<String>();
          33        list.add("a");
          34        list.add("b");
          35        list.add("c");
          36        imlist=Collections.unmodifiableList(list); //通過這種方式實現不可變的集合,以前實現(方式一)
          37        imlist2=Collections.unmodifiableList(Arrays.asList("a","b","c")); //或者,通過這種方式實現不可變的集合,以前實現(方式二)
          38    }

          39
          40    /**
          41     * @param args
          42     */

          43    public static void main(String[] args) 
          44    {
          45        //常量列表
          46        ImmutableList<String> imlist=ImmutableList.of("a""b""c");
          47        //imlist.add("d"); //注意,編譯會報異常
          48        for(String s:imlist){
          49            System.out.println(s);
          50        }

          51        
          52        //常量Map
          53        ImmutableMap<String,String> immap=new ImmutableMap.Builder<String,String>()
          54                                            .put("a","1")
          55                                            .put("b","2")
          56                                            .put("c""3")
          57                                            .build();
          58        for(Entry<String,String> e:immap.entrySet()){
          59            System.out.println(e.getKey()+"="+e.getValue());
          60        }

          61        
          62        //常量Set
          63        ImmutableSet<String> imset=ImmutableSet.of("a","b","c");
          64        for(String s:imset){
          65            System.out.println(s);
          66        }

          67    }

          68
          69}

          70

          (友情提示:本博文章歡迎轉載,但請注明出處:hankchen,http://www.aygfsteel.com/hankchen

          posted on 2010-01-13 00:47 hankchen 閱讀(7758) 評論(0)  編輯  收藏 所屬分類: Java基礎
          主站蜘蛛池模板: 南投市| 丰原市| 鄂州市| 信阳市| 临潭县| 襄城县| 蒙山县| 岑溪市| 瑞昌市| 江源县| 杂多县| 陕西省| 平远县| 六安市| 云林县| 原阳县| 教育| 浮梁县| 庄浪县| 于都县| 泸西县| 静安区| 越西县| 罗城| 宣城市| 富顺县| 鸡泽县| 舞阳县| 高碑店市| 繁昌县| 青岛市| 武冈市| 德州市| 南漳县| 夏津县| 修水县| 左权县| 察雅县| 壤塘县| 泰宁县| 赤城县|