子在川上曰

  逝者如斯夫不舍昼夜
随笔 - 71, 文章 - 0, 评论 - 915, 引用 - 0
数据加载中……

聚集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是被覆盖的值

循环遍历:
        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的一个实现。不过它在读取Properties文件方法特别方便
示例:
        Properties p = System.getProperties(); //得到系统属性
        p.list(System.out);//打印所有

        
/*
         * 从user.home=C:\Documents and Settings\chengang
         * 读取properties文件填充到Properties
         
*/
        Properties p2 
= System.getProperties(); //得到系统属性
        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 2006-04-01 15:48 陈刚 阅读(1113) 评论(2)  编辑  收藏 所属分类: Java

评论

# re: 聚集Collection的技巧  回复  更多评论   

陈老师,最近在读你写的,JAVA开发利器,可是按照你的办法,我注册不了SWT。总是在Directly over web 后一步提示网络链接错误,然后按照网上的说法,把网络链接都禁了都链接了,都没什么用,有没有别的办法嘛?谢谢了!
2006-04-09 11:39 | roy

# re: 聚集Collection的技巧  回复  更多评论   

如果你是教育网,可能就会连不上
2006-04-12 16:30 | 陈刚

只有注册用户登录后才能发表评论。


网站导航: