Skip to content

Instantly share code, notes, and snippets.

@jerolba
Created June 5, 2019 17:34
Show Gist options
  • Save jerolba/667a9afc6329053c537328d568faeb21 to your computer and use it in GitHub Desktop.
Save jerolba/667a9afc6329053c537328d568faeb21 to your computer and use it in GitHub Desktop.
//Given the stock of some products and stores
//BikeyMap<Product, Store, Stock> stock = new TableBikeyMap<>();
BikeyMap<String, String, Integer> stock = new TableBikeyMap<>();
stock.put("shirt-ref-123", "store-76", 10);
stock.put("pants-ref-456", "store-12", 24);
...
stock.put("tie-ref-789", "store-23", 2);
//Get the stock of a product/store
Integer inStock = stock.get("shirt-ref-1234", "store-45");
//Total Stock in store-123
stock.entrySet().stream()
.filter(entry -> entry.getColumn().equals("store-123"))
.mapToInt(entry -> entry.getValue())
.sum();
//Total Stock of pants-ref-457
stock.entrySet().stream()
.filter(entry -> entry.getRow().equals("pants-ref-457"))
.mapToInt(entry -> entry.getValue())
.sum();
//All existing products
Set<String> products = stock.rowKeySet();
//All existing stores
Set<String> stores = stock.columnKeySet();
//If it contains a product/store
if (stock.containsKey("tie-ref-789", "store-23")) {
....
}
//Get all products/stores present in the map
BikeySet<String, String> productStores = map.bikeySet();
//BikeySet<R, C> implements also Set<Bikey<R, C>>
Set<Bikey<String, String>> productStoresSet = map.bikeySet();
//Get all products/stores with stock
BikeySet<String, String> withStock = stock.entrySet().stream()
.filter(entry -> entry.getValue() > 0)
.map(BikeyEntry::getKey)
.collect(BikeyCollectors.toSet());
//Do something with each map element
stock.forEach((product, store, units) -> {
System.out.println("Product " + product + " has " + units + " in store " + store);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment