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

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

            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基礎
          主站蜘蛛池模板: 芦山县| 棋牌| 如东县| 平塘县| 大埔县| 丰城市| 武隆县| 绥宁县| 朝阳市| 民和| 大埔区| 永和县| 三亚市| 黎川县| 商丘市| 蓬莱市| 兴海县| 称多县| 海丰县| 武安市| 镶黄旗| 玛曲县| 荣成市| 梨树县| 揭阳市| 突泉县| 丽江市| 赞皇县| 云梦县| 渝北区| 江阴市| 怀集县| 石景山区| 嘉禾县| 黔西县| 浪卡子县| 康平县| 长兴县| 根河市| 明水县| 杭锦后旗|