Commit 6c5b2d99 authored by Trisha Gee's avatar Trisha Gee
Browse files

[appB] Examples for lambdas and maps

parent 8b230436
Loading
Loading
Loading
Loading
+41 −0
Changes for src/main/java/appB/MapMethods.java: 41 added lines, 0 removed lines.
Original line number Diff line number Diff line
package appB;

import java.util.HashMap;
import java.util.Map;

public class MapMethods {
  void addOrGetCustomer(String usr) {
    Map<String, Actions> custActs = new HashMap<>();
    Actions actions = custActs.computeIfAbsent(usr, name -> new Actions(name));
    // do something with activity
  }

  void addOrGetCustomerOld(String usr) {
    Map<String, Actions> custActs = new HashMap<>();
    // probably other stuff happens here...
    Actions actions = custActs.get(usr);
    if (actions == null) {
      actions = new Actions(usr);
      custActs.put(usr, actions);
    }
    // do something with actions
  }

  void incrementMetricWeCareAbout(String metric) {
    Map<String, Integer> metrics = new HashMap<>();
    // probably other stuff happens here...

    if (metrics.containsKey(metric)) {
      Integer integer = metrics.get(metric);
      metrics.put(metric, ++integer);
    }
  }

  private class Actions {
    private String username;

    public Actions(String username) {
      this.username = username;
    }
  }
}
+69 −0
Changes for src/test/java/appB/MapMethodsTest.java: 69 added lines, 0 removed lines.
Original line number Diff line number Diff line
package appB;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

class MapMethodsTest {

  @Test
  void shouldOnlyAddMetricsThatWeCareAbout() {
    Map<String, Integer> metrics = new HashMap<>();
    metrics.put("metric 1", 0);
    metrics.put("metric 2", 0);
    metrics.put("metric 3", 0);

    Integer newValue = metrics.computeIfPresent("metric 1", (key, value) -> ++value);
    System.out.println("metrics = " + metrics);
    Assertions.assertEquals(1, metrics.get("metric 1"));
    Assertions.assertEquals(1, newValue);

    metrics.computeIfPresent("metric 4", (s, integer) -> ++integer);
    System.out.println("metrics = " + metrics);
    Assertions.assertFalse(metrics.containsKey("metric 4"));
  }

  @Test
  void shouldOnlyAddMetricsThatWeCareAboutOld() {
    Map<String, Integer> metrics = new HashMap<>();
    metrics.put("metric 1", 0);
    metrics.put("metric 2", 0);
    metrics.put("metric 3", 0);

    String key = "metric 1";
    if (metrics.containsKey(key)) {
      Integer value = metrics.get(key);
      metrics.put(key, ++value);
    }
    System.out.println("metrics = " + metrics);
    Assertions.assertEquals(1, metrics.get("metric 1"));
  }

  @Test
  void shouldSeeWhatReplaceAllLooksLike() {
    Map<String, Integer> metrics = new HashMap<>();
    metrics.put("metric 1", 0);
    metrics.put("metric 2", 0);
    metrics.put("metric 3", 0);

    metrics.replaceAll((key, value) -> ++value);
    Assertions.assertEquals(1, metrics.get("metric 1"));
    Assertions.assertEquals(1, metrics.get("metric 2"));
    Assertions.assertEquals(1, metrics.get("metric 3"));
  }

  @Test
  void shouldSeeWhatForEachLooksLike() {
    Map<String, Integer> metrics = new HashMap<>();
    metrics.put("metric 1", 0);
    metrics.put("metric 2", 0);
    metrics.put("metric 3", 0);

    metrics.forEach((key, value) -> {
      System.out.println("key = " + key);
      System.out.println("value = " + value);
    });
  }
}
 No newline at end of file