生活随笔
收集整理的這篇文章主要介紹了
Guava中针对集合的 filter和过滤功能
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Guava中針對(duì)集合的 filter和過(guò)濾功能
博客分類(lèi):? JAVA相關(guān)
在guava庫(kù)中,自帶了過(guò)濾器(filter)的功能,可以用來(lái)對(duì)collection 進(jìn)行過(guò)濾,先看例子:??? 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"));??}?? ? 在這個(gè)例子中,給出一個(gè)list,過(guò)濾出含有字母a的元素?此外,可以使用Collections2.filter() 去進(jìn)行過(guò)濾? 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中,當(dāng)在上面的result中增加了元素后,會(huì)直接影響原來(lái)的names這個(gè)list的,就是names中的集合元素是5了。?? 再來(lái)看下predicates判斷語(yǔ)言,?com.google.common.base. Predicate : 根據(jù)輸入值得到 true 或者 false?拿Collections2中有2個(gè)函數(shù)式編程的接口:filter , transform ,例如 :在Collection<Integer>中過(guò)濾大于某數(shù)的內(nèi)容:? 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類(lèi)型轉(zhuǎn)換為String , 并添加test作為后綴字符:? Java代碼??
List<String>?c2?=?Lists.transform(list,?new?Function<Integer?,?String>(){??????????????????????@Override??????????????????????public?String?apply(Integer?input)?{????????????????????????????return?String.valueOf(input)?+?"test";??????????????????????}????????????????});?? 需要說(shuō)明的是每次調(diào)用返回都是新的對(duì)象,同時(shí)操作過(guò)程不是線程安全的。???? 再來(lái)點(diǎn)例子:??? 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"));??}?? ??? 將多個(gè)prdicate進(jìn)行組合? 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"));??}?? ??? 檢查一個(gè)collection中的所有元素是否符合某個(gè)條件:??? 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);?? ?? 下面看如何把一個(gè)list進(jìn)行轉(zhuǎn)換,? 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));??}?? ????? 再看結(jié)合transform和predicates結(jié)合使用的例子:??? 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));??}?? ??? 在這個(gè)例子中,將一個(gè)LIST中的每一個(gè)元素進(jìn)行使用Predicates.containsPattern,判斷是否包含m,返回的是boolean,然后再得到的boolean值一起轉(zhuǎn)換為collection???? 下面是兩個(gè)function一起結(jié)合使用的例子:?? 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));??}?? ?? 在這個(gè)例子中,首先應(yīng)用函數(shù)f1,求出每個(gè)元素的長(zhǎng)度,然后再根據(jù)f1函數(shù),分別返回?它們的boolean值,再轉(zhuǎn)換為collection.?????? 最后看下將filter和transform結(jié)合使用的例子:??? 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)); ?
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的Guava中针对集合的 filter和过滤功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。