溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java Map 通過(guò) key 或者 value 過(guò)濾的實(shí)例代碼

發(fā)布時(shí)間:2020-10-25 03:14:49 來(lái)源:腳本之家 閱讀:163 作者:志哥 欄目:編程語(yǔ)言

今天寫(xiě)根過(guò)濾的時(shí)候一下子有點(diǎn)愣眼,先是想到用 Java 原生的 map 循環(huán)查出來(lái),但是覺(jué)得太 low, 后面思考了一下可以用 Java8 的 Lambda,寫(xiě)完了,又發(fā)現(xiàn) Google Guava 有現(xiàn)成的方法,這里一一列出來(lái),供參考使用。

首先提示,如果照搬我的代碼的話別忘了引這些依賴

<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"));
  }
}

總結(jié)

以上所述是小編給大家介紹的Java Map 通過(guò) key 或者 value 過(guò)濾的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI