風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
          數(shù)據(jù)加載中……

          聚集Collection的技巧

          1、??MAP
          特點:同鍵的新值將覆蓋舊值。
          示例:
          ????????Map<String,?String>?map?=?new?HashMap<String,?String>();
          ????????String?str;
          ????????str?
          =?map.put("1",?"a");?//str=null
          ????????str?=?map.put("1",?"b");//str=a
          ????????str?=?map.put("1",?"c");?//str=b?新值覆蓋舊值,最后map只有(1,c)一個值。返回的b是被覆蓋的值

          循環(huán)遍歷:
          ????????Map<String,?Long>?map?=?new?HashMap<String,?Long>();
          ????????map.put(
          "a",?new?Long(1));
          ????????map.put(
          "b",?new?Long(2));

          ????????
          for?(Map.Entry<String,?Long>?entry?:?map.entrySet())?{
          ????????????String?key?
          =?entry.getKey();
          ????????????Long?value?
          =?entry.getValue();
          ????????}

          2、Set
          特點:無序排列,不會存在重復的值,添加重復值也不會報錯.Set可以加入null值
          示例:
          ????????Set < String > ?set? = ? new ?HashSet < String > ();
          ????????
          boolean ?b;
          ????????b?
          = ?set.add( " 1 " ); // b=true
          ????????b? = ?set.add( " 2 " ); // b=true
          ????????b? = ?set.add( " 1 " ); // b=false
          ????????b? = ?set.add( " 2 " ); // b=false?返回值為false,表示沒有加入

          3、List
          特點:有序排序,可以加入重復的元素
          示例:
          ????????Arrays.asList( new ?String[]?{ " 11 " ,? " 222 " ?}); // 返回一個不可改的List
          ????????List < String > ?l? = ?Arrays.asList( " 11 " ,? " 222 " ,? " 333 " );
          ????????l.subList(
          1 ,? 2 );? // 由第二個和第三個元素生成一個新List

          ????????ArrayList
          < String > ?list? = ? new ?ArrayList < String > (l);? // 生成一個可改的List
          ???????? boolean ?b;
          ????????b?
          = ?list.add( " 11 " ); // b=true??List可以加入重復的元素
          ????????b? = ?list.add( " 11 " ); // b=true

          4、Collections
          特點:Collections.class是集合的工具方法類
          示例:
          ????????List<String>?list1?=?new?ArrayList<String>();
          ????????List
          <String>?list?=?new?ArrayList<String>();

          ????????Collections.sort(list);?
          //排序
          ????????int?pos?=?Collections.binarySearch(list,?"key");//用二叉樹算法來查找,首先得排序好
          ????????System.out.println("pos="?+?pos);

          ????????Collections.copy(list,?list1);?
          //把list1復制到list上,原list元素被覆蓋
          ????????Collections.fill(list,?"ss");?//用"ss"填充list
          ????????Collections.max(list);?//找最大元素
          ????????Collections.reverse(list);?//把排序
          ????????Collections.shuffle(list);//混亂排序

          ????????Collections.singleton(
          "ss");//返回一個Set集合,不可改,只有ss一個元素
          ????????Collections.unmodifiableList(list);//返回一個不可改的新的List,原list不變
          ????????Collections.synchronizedList(list);//返回一個同步的新的List,原list不變

          5、Properties
          特點:Properties繼承自Hashtable,是Map的一個實現(xiàn)。不過它在讀取Properties文件方法特別方便
          示例:
          ????????Properties?p?=?System.getProperties();?//得到系統(tǒng)屬性
          ????????p.list(System.out);//打印所有

          ????????
          /*
          ?????????*?從user.home=C:\Documents?and?Settings\chengang
          ?????????*?讀取properties文件填充到Properties
          ?????????
          */
          ????????Properties?p2?
          =?System.getProperties();?//得到系統(tǒng)屬性
          ????????File?file?=?new?File(p.getProperty("user.home"),?"argo.user.properties");
          ????????p2.load(
          new?FileInputStream(file));
          ????????System.err.println(
          "------------------------------");
          ????????p2.list(System.out);

          posted on 2007-01-30 10:34 風人園 閱讀(234) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 张家川| 庆安县| 章丘市| 鞍山市| 黑龙江省| 辉县市| 泽州县| 四川省| 辛集市| 友谊县| 南丰县| 辉县市| 阳春市| 息烽县| 澎湖县| 永定县| 双鸭山市| 宁乡县| 长岛县| 沈丘县| 云浮市| 奉贤区| 平泉县| 叙永县| 柘荣县| 山阳县| 双牌县| 江源县| 饶阳县| 汕头市| 司法| 赣州市| 罗定市| 基隆市| 英山县| 平利县| 磴口县| 沭阳县| 开化县| 广东省| 南乐县|