要点Java20java.util.Collections

WBOY
发布: 2016-06-07 15:56:41
原创
1346 人浏览过

java.util.Collections 集合帮助类 示例程序(JUnit演示) 排序 @Test public void testSort() { ListInteger demoList = new ArrayListInteger(Arrays.asList(3, 2, 1)); assertEquals(3, demoList.get(0).intValue()); //public static T extends Comparable

java.util.Collections 集合帮助类

示例程序(JUnit演示)

排序

    @Test
    public void testSort() {
        List demoList = new ArrayList(Arrays.asList(3, 2, 1));
        assertEquals(3, demoList.get(0).intValue());

        //public static > void sort(List list)
        Collections.sort(demoList);

        assertEquals(1, demoList.get(0).intValue());

        //public static void shuffle(List list) // 随机排序
        //public static void reverse(List list) // 反序          
    }
登录后复制

查找

    @Test
    public void testBinarySearch() {
        List demoList = new ArrayList(Arrays.asList(3, 2, 1));
        //必须先排序
        Collections.sort(demoList);
        assertEquals(1, demoList.get(0).intValue());

        //二分查找位置
        //public static  int binarySearch(List> list, T key)
        assertEquals(0, Collections.binarySearch(demoList, 1));

        //public static int indexOfSubList(List source, List target)
        // public static int lastIndexOfSubList(List source, List target)
    }
登录后复制

置换

    @Test
    public void testSwap() {
        List demoList = new ArrayList(Arrays.asList(3, 2, 1));

        //public static void swap(List list, int i, int j)
        Collections.swap(demoList, 0, 2);
        assertEquals(1, demoList.get(0).intValue());
        //public static void rotate(List list, int distance)  //指定距离轮换
        //public static  boolean replaceAll(List list, T oldVal, T newVal)  //指定替换
    }
登录后复制

拷贝

    @Test
    public void testCopy() {
        List demoList = new ArrayList(Arrays.asList(3, 2, 1));

        //public static  void copy(List dest, List src)
        //注意目的List的size最少要等于src的size
        List copyList = new ArrayList(Arrays.asList(1, 2, 3));
        Collections.copy(copyList, demoList);
        assertEquals(3, copyList.size());
        assertEquals(3, copyList.get(0).intValue());

        copyList.add(4);
        assertEquals(3, demoList.size());
    }
登录后复制

比较

    @Test
    public void testCompary() {
        List demoList = new ArrayList(Arrays.asList(3, 2, 1));
        assertEquals(1, Collections.min(demoList).intValue());
        assertEquals(3, Collections.max(demoList).intValue());

        List compareList = new ArrayList(Arrays.asList(5, 6, 7));
        //Returns true if the two specified collections have no elements in common
        assertTrue(Collections.disjoint(demoList, compareList));
    }
登录后复制

创造不同的集合

    @SuppressWarnings("unused")
    @Test
    public void testCreate() {
        List demoList = new ArrayList(Arrays.asList(3, 2, 1));

        //空对象 size=0  无添加方法
        List emptyList = Collections.emptyList();

        //返回一个只包含指定对象的不可变列表。
        List singletonList = Collections.singletonList(1);

        //返回指定列表的一个动态类型安全视图。
        List checkedList = Collections.checkedList(demoList, Integer.class);

        //返回指定列表的不可修改视图。
        List unmodifiableList = Collections.unmodifiableList(demoList);

        //返回指定列表支持的同步(线程安全的)列表。
        List synchronizedList = Collections.synchronizedList(demoList);

        synchronized (synchronizedList) {
            Iterator i = synchronizedList.iterator(); // Must be in synchronized block
            while (i.hasNext())
                i.next();
        }

        //map set sortedMap sortedSet
    }
登录后复制

环境 jdk1.6 window7 junit4

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!