集合判斷:
例1: 判斷集合是否爲空:
CollectionUtils.isEmpty(null): true
CollectionUtils.isEmpty(new ArrayList()): true
CollectionUtils.isEmpty({a,b}): falsejava
例2: 判斷集合是否不爲空:
CollectionUtils.isNotEmpty(null): false
CollectionUtils.isNotEmpty(new ArrayList()): false
CollectionUtils.isNotEmpty({a,b}): trueapache
CollectionUtils在真實項目中,是一個很是好用的工具類,使用很是頻繁。它能夠使代碼更加簡潔和安全。恰好在工做中利用這個工具類重構代碼,順便總結下分享分享:數組
並集安全
@Test public void testUnion(){ String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //2個數組取並集 System.out.println(ArrayUtils.toString(CollectionUtils.union(listA, listB))); //[A, B, C, D, E, F, G, H, K] }
交集工具
@Test public void testIntersection(){ String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //2個數組取交集 System.out.println(ArrayUtils.toString(CollectionUtils.intersection(listA, listB))); //[B, D, F] }
交集的補集(析取)code
@Test public void testDisjunction(){ String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //2個數組取交集 的補集 System.out.println(ArrayUtils.toString(CollectionUtils.disjunction(listA, listB))); //[A, C, E, G, H, K] }
差集(扣除)對象
@Test public void testSubtract(){ String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" }; String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //arrayA扣除arrayB System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listA, listB))); //[A, C, E] }
集合是否爲空io
@Test public void testIsEmpty(){ class Person{} class Girl extends Person{} List<Integer> first = new ArrayList<>(); List<Integer> second = null; List<Person> boy = new ArrayList<>(); //每一個男孩內心都裝着一個女孩 boy.add(new Girl()); //判斷集合是否爲空 System.out.println(CollectionUtils.isEmpty(first)); //true System.out.println(CollectionUtils.isEmpty(second)); //true System.out.println(CollectionUtils.isEmpty(boy)); //false //判斷集合是否不爲空 System.out.println(CollectionUtils.isNotEmpty(first)); //false System.out.println(CollectionUtils.isNotEmpty(second)); //false System.out.println(CollectionUtils.isNotEmpty(boy)); //true }
集合是否相等class
@Test public void testIsEqual(){ class Person{} class Girl extends Person{ } List<Integer> first = new ArrayList<>(); List<Integer> second = new ArrayList<>(); first.add(1); first.add(2); second.add(2); second.add(1); Girl goldGirl = new Girl(); List<Person> boy1 = new ArrayList<>(); //每一個男孩內心都裝着一個女孩 boy1.add(new Girl()); List<Person> boy2 = new ArrayList<>(); //每一個男孩內心都裝着一個女孩 boy2.add(new Girl()); //比較兩集合值 System.out.println(CollectionUtils.isEqualCollection(first,second)); //true System.out.println(CollectionUtils.isEqualCollection(first,boy1)); //false System.out.println(CollectionUtils.isEqualCollection(boy1,boy2)); //false List<Person> boy3 = new ArrayList<>(); //每一個男孩內心都裝着一個女孩 boy3.add(goldGirl); List<Person> boy4 = new ArrayList<>(); boy4.add(goldGirl); System.out.println(CollectionUtils.isEqualCollection(boy3,boy4)); //true }
不可修改的集合test
咱們對c進行操做,s也一樣得到了和c相同的內容,這樣就能夠避免其餘人員修改這個s對象。有時候須要對它進行保護,避免返回結果被人修改。
@Test public void testUnmodifiableCollection(){ Collection<String> c = new ArrayList<>(); Collection<String> s = CollectionUtils.unmodifiableCollection(c); c.add("boy"); c.add("love"); c.add("girl"); //! s.add("have a error"); System.out.println(s); }
Collections.unmodifiableCollection能夠獲得一個集合的鏡像,它的返回結果是不可直接被改變,不然會提示錯誤
java.lang.UnsupportedOperationException at org.apache.commons.collections.collection.UnmodifiableCollection.add(UnmodifiableCollection.java:75)