JavaMap通過key或者value過濾
今天寫根過濾的時候一下子有點愣眼,先是想到用 Java 原生的 map 迴圈查出來,但是覺得太 low, 後面思考了一下可以用 Java8 的 Lambda,寫完了,又發現 Google Guava 有現成的方法,這裡一一列出來,供參考使用。
首先提示,如果照搬我的程式碼的話別忘了引這些依賴
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
</dependencies>
filter by key
public class FilterMapByKeyTest {
private Map<Integer, String> WEEK = new HashMap<>();
@Before
public void setUp () {
WEEK.put(1, "Monday");
WEEK.put(2, "Tuesday");
WEEK.put(3, "Wednesday");
WEEK.put(4, "Thursday");
WEEK.put(5, "Friday");
WEEK.put(6, "Saturday");
WEEK.put(7, "Sunday");
}
/**
* Java 8之前的版本
*/
@Test
public void filterMapByKey () {
Map<Integer, String> map = new HashMap<>();
for (Map.Entry<Integer, String> entry : WEEK.entrySet()) {
if (entry.getKey() <= 3) {
map.put(entry.getKey(), entry.getValue());
}
}
assertThat(map.keySet(), contains(1, 2, 3));
}
/**
* Java 8 Lambda
*/
@Test
public void filterMapByKeyJava8Lambda () {
Map<Integer, String> map = WEEK.entrySet().stream().filter(r -> r.getKey() <= 3)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
assertThat(map.keySet(), contains(1, 2, 3));
}
/**
* Google Guava
*/
@Test
public void filterMapByKeyGuava () {
Map<Integer, String> map = Maps.filterKeys(WEEK, r -> r <= 3);
assertThat(map.keySet(), contains(1, 2, 3));
}
}
filter by value
public class FilterMapByValueTest {
private Map<Integer, String> WEEK = new HashMap<>();
@Before
public void setUp () {
WEEK.put(1, "Monday");
WEEK.put(2, "Tuesday");
WEEK.put(3, "Wednesday");
WEEK.put(4, "Thursday");
WEEK.put(5, "Friday");
WEEK.put(6, "Saturday");
WEEK.put(7, "Sunday");
}
/**
* Java 8之前的版本
*/
@Test
public void filterMapByValue () {
Map<Integer, String> map = new HashMap<>();
for (Map.Entry<Integer, String> entry : WEEK.entrySet()) {
if (entry.getValue().startsWith("S")) {
map.put(entry.getKey(), entry.getValue());
}
}
assertThat(map.values(), contains("Saturday","Sunday"));
}
/**
* Java 8 Lambda
*/
@Test
public void filterMapByValueJava8Lambda () {
Map<Integer, String> map = WEEK.entrySet().stream().filter(r -> r.getValue().startsWith("S"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
assertThat(map.values(), contains("Saturday","Sunday"));
}
/**
* Google Guava
*/
@Test
public void filterMapByValueGuava () {
Map<Integer, String> map = Maps.filterValues(WEEK, r -> r.startsWith("S"));
assertThat(map.values(), contains("Saturday","Sunday"));
}
}
如果覺得內容還不錯,可以關注一下我哦
相關文章
- 過濾或者查詢敏感詞(DFA 演算法)演算法
- 過濾
- Flutter渲染之通過demo瞭解Key的作用Flutter
- 過濾器過濾器
- filter過濾Filter
- 過濾FilteringFilter
- Django(67)drf搜尋過濾和排序過濾Django排序
- 通過Vue的過濾器實現資料的資料脫敏Vue過濾器
- 4、過濾器的使用及自定義過濾器過濾器
- CAN過濾器過濾器
- 檢視過濾
- Filter過濾器Filter過濾器
- vue 過濾器Vue過濾器
- 過濾函式函式
- NetCore過濾器NetCore過濾器
- 代理過濾器過濾器
- Vue過濾器Vue過濾器
- winshark 過濾包
- vue 列表過濾Vue
- DataV過濾器過濾器
- hbase過濾器過濾器
- SpringBoot(18)---通過Lua指令碼批量插入資料到Redis布隆過濾器Spring Boot指令碼Redis過濾器
- CSS: 通過CSS控制字串長度(一行或者多行)CSS字串
- Xor過濾器:比布隆Bloom過濾器更快,更小過濾器OOM
- MySQL 主從複製過濾新增庫表過濾方案MySql
- 通過幾個問題深入分析Vue中的keyVue
- Python 通過 .cube LUT 檔案對影像加濾鏡Python
- Spring Cloud Gateway ---GatewayFilter過濾器、過濾器工廠(入門)SpringCloudGatewayFilter過濾器
- PHP 過濾器(Filter)PHP過濾器Filter
- RxSwift 之過濾操作Swift
- vue---過濾器Vue過濾器
- JavaScript過濾特殊字串JavaScript字串
- jQuery之過濾元素jQuery
- Wireshark過濾規則
- Vue中過濾器Vue過濾器
- js陣列過濾JS陣列
- vue filters過濾器VueFilter過濾器
- wireshark 過濾規則