java.util.Collections 集合帮助类

24
25
26
27
28
29



<code class= "java     " >    @SuppressWarnings( "unused" )
     @Test
     public void testCreate() {
         List< Integer > demoList = new ArrayList< Integer >(Arrays.asList(3, 2, 1));
 
         //空对象 size =0  无添加方法
         List< Integer > emptyList = Collections.emptyList();
 
         //返回一个只包含指定对象的不可变列表。
         List< Integer > singletonList = Collections.singletonList(1);
 
         //返回指定列表的一个动态类型安全视图。
         List< Integer > checkedList = Collections.checkedList(demoList, Integer .class);
 
         //返回指定列表的不可修改视图。
         List< Integer > unmodifiableList = Collections.unmodifiableList(demoList);
 
         //返回指定列表支持的同步(线程安全的)列表。
         List< Integer > synchronizedList = Collections.synchronizedList(demoList);
 
         synchronized (synchronizedList) {
             Iterator< Integer > i = synchronizedList.iterator(); // Must be in synchronized block
             while (i.hasNext())
                 i. next ();
         }
 
         //map set sortedMap sortedSet
     }


<code class= "java     " >    @Test
     public void testCopy() {
         List< Integer > demoList = new ArrayList< Integer >(Arrays.asList(3, 2, 1));
 
         // public static <T> void copy(List<? super T> dest, List<? extends T> src)
         //注意目的List的 size 最少要等于src的 size
         List< Integer > copyList = new ArrayList< Integer >(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 ());
     }
</code>
</code>

<code class= "java     " >    @Test
     public void testCompary() {
         List< Integer > demoList = new ArrayList< Integer >(Arrays.asList(3, 2, 1));
         assertEquals(1, Collections. min (demoList).intValue());
         assertEquals(3, Collections. max (demoList).intValue());
 
         List< Integer > compareList = new ArrayList< Integer >(Arrays.asList(5, 6, 7));
         // Returns true if the two specified collections have no elements in common
         assertTrue(Collections.disjoint(demoList, compareList));
     }
</code>


<code class= "java     " >    @Test
     public void testCopy() {
         List< Integer > demoList = new ArrayList< Integer >(Arrays.asList(3, 2, 1));
 
         // public static <T> void copy(List<? super T> dest, List<? extends T> src)
         //注意目的List的 size 最少要等于src的 size
         List< Integer > copyList = new ArrayList< Integer >(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 ());
     }
</code>



<code class= "java     " >    @Test
     public void testSwap() {
         List< Integer > demoList = new ArrayList< Integer >(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 <T> boolean replaceAll(List<T> list, T oldVal, T newVal)  //指定替换
     }
</code>



<code class= "java" >    @Test
     public void testBinarySearch() {
         List< Integer > demoList = new ArrayList< Integer >(Arrays.asList(3, 2, 1));
         //必须先排序
         Collections.sort(demoList);
         assertEquals(1, demoList.get(0).intValue());
 
         //二分查找位置
         // public static <T> int binarySearch(List<? extends Comparable<? super T>> 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)
     }
</code>

<code class= "java" >    @Test
     public void testSort() {
         List< Integer > demoList = new ArrayList< Integer >(Arrays.asList(3, 2, 1));
         assertEquals(3, demoList.get(0).intValue());
 
         // public static <T extends Comparable<? super T>> void sort(List<T> list)
         Collections.sort(demoList);
 
         assertEquals(1, demoList.get(0).intValue());
 
         // public static void shuffle(List<?> list) // 随机排序
         // public static void reverse(List<?> list) // 反序         
     }
</code>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值