美文网首页
Guava Collections之Immutable Coll

Guava Collections之Immutable Coll

作者: 神豪VS勇士赢 | 来源:发表于2020-09-01 09:12 被阅读0次

    Immutable Collections :

    public class ImmutableCollectionsTest
    {
    
        @Test(expected = UnsupportedOperationException.class)
        public void testOf()
        {
            ImmutableList<Integer> list = ImmutableList.of(1, 2, 3);
            assertThat(list, notNullValue());
            list.add(4);
            fail();
        }
    
        @Test
        public void testCopy()
        {
            Integer[] array = {1, 2, 3, 4, 5};
            System.out.println(ImmutableList.copyOf(array));
        }
    
        @Test
        public void testBuilder()
        {
            ImmutableList<Integer> list = ImmutableList.<Integer>builder()
                    .add(1)
                    .add(2, 3, 4).addAll(Arrays.asList(5, 6))
                    .build();
            System.out.println(list);
        }
    
        @Test
        public void testImmutableMap()
        {
            ImmutableMap<String, String> map = ImmutableMap.<String, String>builder().put("Oracle", "12c")
                    .put("Mysql", "7.0").build();
            System.out.println(map);
            try
            {
                map.put("Scala", "2.3.0");
                fail();
            } catch (Exception e)
            {
                assertThat(e instanceof UnsupportedOperationException, is(true));
            }
        }
    }
    

    Sorted Collections:

    public class OrderingExampleTest
    {
    
        @Test
        public void testJDKOrder()
        {
            List<Integer> list = Arrays.asList(1, 5, 3, 8, 2);
            System.out.println(list);
            Collections.sort(list);
            System.out.println(list);
        }
    
        @Test(expected = NullPointerException.class)
        public void testJDKOrderIssue()
        {
            List<Integer> list = Arrays.asList(1, 5, null, 3, 8, 2);
            System.out.println(list);
            Collections.sort(list);
            System.out.println(list);
        }
    
        @Test
        public void testOrderNaturalByNullFirst()
        {
            List<Integer> list = Arrays.asList(1, 5, null, 3, 8, 2);
            Collections.sort(list, Ordering.natural().nullsFirst());
            System.out.println(list);
        }
    
        @Test
        public void testOrderNaturalByNullLast()
        {
            List<Integer> list = Arrays.asList(1, 5, null, 3, 8, 2);
            Collections.sort(list, Ordering.natural().nullsLast());
            System.out.println(list);
        }
    
        @Test
        public void testOrderNatural()
        {
            List<Integer> list = Arrays.asList(1, 5, 3, 8, 2);
            Collections.sort(list);
            assertThat(Ordering.natural().isOrdered(list), is(true));
        }
    
    
        @Test
        public void testOrderReverse()
        {
            List<Integer> list = Arrays.asList(1, 5, 3, 8, 2);
            Collections.sort(list, Ordering.natural().reverse());
            System.out.println(list);
        }
    }
    

    相关文章

      网友评论

          本文标题:Guava Collections之Immutable Coll

          本文链接:https://www.haomeiwen.com/subject/ximmsktx.html