生活随笔
收集整理的這篇文章主要介紹了
Guava中针对集合的 filter和过滤功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Guava中針對集合的 filter和過濾功能
博客分類:? JAVA相關
在guava庫中,自帶了過濾器(filter)的功能,可以用來對collection 進行過濾,先看例子:??? Java代碼??
@Test??public?void?whenFilterWithIterables_thenFiltered()?{??????List<String>?names?=?Lists.newArrayList("John",?"Jane",?"Adam",?"Tom");??????Iterable<String>?result?=?Iterables.filter(names,?Predicates.containsPattern("a"));?????????assertThat(result,?containsInAnyOrder("Jane",?"Adam"));??}?? ? 在這個例子中,給出一個list,過濾出含有字母a的元素?此外,可以使用Collections2.filter() 去進行過濾? Java代碼??
@Test??public?void?whenFilterWithCollections2_thenFiltered()?{??????List<String>?names?=?Lists.newArrayList("John",?"Jane",?"Adam",?"Tom");??????Collection<String>?result?=?Collections2.filter(names,?Predicates.containsPattern("a"));?????????????assertEquals(2,?result.size());??????assertThat(result,?containsInAnyOrder("Jane",?"Adam"));?????????result.add("anna");??????assertEquals(5,?names.size());??}?? ? 這里注意的是,Collections2.filter中,當在上面的result中增加了元素后,會直接影響原來的names這個list的,就是names中的集合元素是5了。?? 再來看下predicates判斷語言,?com.google.common.base. Predicate : 根據輸入值得到 true 或者 false?拿Collections2中有2個函數式編程的接口:filter , transform ,例如 :在Collection<Integer>中過濾大于某數的內容:? Java代碼??
Collection<Integer>?filterList?=?Collections2.filter(collections?????????,?new?Predicate<Integer>(){??????????????????????@Override??????????????????????public?boolean?apply(Integer?input)?{????????????????????????????if(input?>?4)??????????????????????????????????return?false;????????????????????????????else??????????????????????????????????return?true;??????????????????????}????});?? 把Lis<Integer>中的Integer類型轉換為String , 并添加test作為后綴字符:? Java代碼??
List<String>?c2?=?Lists.transform(list,?new?Function<Integer?,?String>(){??????????????????????@Override??????????????????????public?String?apply(Integer?input)?{????????????????????????????return?String.valueOf(input)?+?"test";??????????????????????}????????????????});?? 需要說明的是每次調用返回都是新的對象,同時操作過程不是線程安全的。???? 再來點例子:??? Java代碼??
@Test??public?void?whenFilterCollectionWithCustomPredicate_thenFiltered()?{??????Predicate<String>?predicate?=?new?Predicate<String>()?{??????????@Override??????????public?boolean?apply(String?input)?{??????????????return?input.startsWith("A")?||?input.startsWith("J");??????????}??????};?????????List<String>?names?=?Lists.newArrayList("John",?"Jane",?"Adam",?"Tom");??????Collection<String>?result?=?Collections2.filter(names,?predicate);?????????assertEquals(3,?result.size());??????assertThat(result,?containsInAnyOrder("John",?"Jane",?"Adam"));??}?? ??? 將多個prdicate進行組合? Java代碼??
@Test??public?void?whenFilterUsingMultiplePredicates_thenFiltered()?{??????List<String>?names?=?Lists.newArrayList("John",?"Jane",?"Adam",?"Tom");??????Collection<String>?result?=?Collections2.filter(names,?????????Predicates.or(Predicates.containsPattern("J"),?????????Predicates.not(Predicates.containsPattern("a"))));?????????assertEquals(3,?result.size());??????assertThat(result,?containsInAnyOrder("John",?"Jane",?"Tom"));??}?? ??????? 上面的例子中找出包含J字母或不包含a的元素;????再看下如何將集合中的空元素刪除:??? Java代碼??
@Test??public?void?whenRemoveNullFromCollection_thenRemoved()?{??????List<String>?names?=?Lists.newArrayList("John",?null,?"Jane",?null,?"Adam",?"Tom");??????Collection<String>?result?=?Collections2.filter(names,?Predicates.notNull());?????????assertEquals(4,?result.size());??????assertThat(result,?containsInAnyOrder("John",?"Jane",?"Adam",?"Tom"));??}?? ??? 檢查一個collection中的所有元素是否符合某個條件:??? Java代碼??
@Test??ublic?void?whenCheckingIfAllElementsMatchACondition_thenCorrect()?{?????List<String>?names?=?Lists.newArrayList("John",?"Jane",?"Adam",?"Tom");???????boolean?result?=?Iterables.all(names,?Predicates.containsPattern("n|m"));?????assertTrue(result);???????result?=?Iterables.all(names,?Predicates.containsPattern("a"));?????assertFalse(result);?? ?? 下面看如何把一個list進行轉換,? Java代碼??
@Test??public?void?whenTransformWithIterables_thenTransformed()?{??????Function<String,?Integer>?function?=?new?Function<String,?Integer>()?{??????????@Override??????????public?Integer?apply(String?input)?{??????????????return?input.length();??????????}??????};?????????List<String>?names?=?Lists.newArrayList("John",?"Jane",?"Adam",?"Tom");??????Iterable<Integer>?result?=?Iterables.transform(names,?function);?????????assertThat(result,?contains(4,?4,?4,?3));??}?? ????? 再看結合transform和predicates結合使用的例子:??? Java代碼??
@Test??public?void?whenCreatingAFunctionFromAPredicate_thenCorrect()?{??????List<String>?names?=?Lists.newArrayList("John",?"Jane",?"Adam",?"Tom");??????Collection<Boolean>?result?=????????Collections2.transform(names,????????Functions.forPredicate(Predicates.containsPattern("m")));?????????assertEquals(4,?result.size());??????assertThat(result,?contains(false,?false,?true,?true));??}?? ??? 在這個例子中,將一個LIST中的每一個元素進行使用Predicates.containsPattern,判斷是否包含m,返回的是boolean,然后再得到的boolean值一起轉換為collection???? 下面是兩個function一起結合使用的例子:?? Java代碼??
@Test??public?void?whenTransformingUsingComposedFunction_thenTransformed()?{??????Function<String,Integer>?f1?=?new?Function<String,Integer>(){??????????@Override??????????public?Integer?apply(String?input)?{??????????????return?input.length();??????????}??????};?????????Function<Integer,Boolean>?f2?=?new?Function<Integer,Boolean>(){??????????@Override??????????public?Boolean?apply(Integer?input)?{??????????????return?input?%?2?==?0;??????????}??????};?????????List<String>?names?=?Lists.newArrayList("John",?"Jane",?"Adam",?"Tom");??????Collection<Boolean>?result?=?Collections2.transform(names,?Functions.compose(f2,?f1));?????????assertEquals(4,?result.size());??????assertThat(result,?contains(true,?true,?true,?false));??}?? ?? 在這個例子中,首先應用函數f1,求出每個元素的長度,然后再根據f1函數,分別返回?它們的boolean值,再轉換為collection.?????? 最后看下將filter和transform結合使用的例子:??? Java代碼??
@Test??public?void?whenFilteringAndTransformingCollection_thenCorrect()?{??????Predicate<String>?predicate?=?new?Predicate<String>()?{??????????@Override??????????public?boolean?apply(String?input)?{??????????????return?input.startsWith("A")?||?input.startsWith("T");??????????}??????};?????????Function<String,?Integer>?func?=?new?Function<String,Integer>(){??????????@Override??????????public?Integer?apply(String?input)?{??????????????return?input.length();??????????}??????};?????????List<String>?names?=?Lists.newArrayList("John",?"Jane",?"Adam",?"Tom");??????Collection<Integer>?result?=?FluentIterable.from(names)?????????????????????????????????????????????????.filter(predicate)?????????????????????????????????????????????????.transform(func)?????????????????????????????????????????????????.toList();?????????assertEquals(2,?result.size());??????assertThat(result,?containsInAnyOrder(4,?3)); ?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的Guava中针对集合的 filter和过滤功能的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。