Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7899c3

Browse files
committed
map with timestamp done
1 parent 97addbe commit d7899c3

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

‎src/main/java/com/rampatra/java8/FlatMapInStreams.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
public class FlatMapInStreams {
1212

1313
public static long countTotalIngredientsInAllDishes(List<Dish> dishes) {
14-
return dishes.stream().map(Dish::getIngredients).flatMap(List::stream).count();
14+
return dishes.stream()
15+
.map(Dish::getIngredients)
16+
.flatMap(List::stream)
17+
.count();
1518
}
1619

1720
public static void main(String[] args) {
@@ -21,7 +24,7 @@ public static void main(String[] args) {
2124
ingredients.add("haldi");
2225
List<Dish> dishes = Arrays.asList(
2326
new Dish("biriyani", 600, ingredients),
24-
new Dish("biriyani", 600, new ArrayList<>()));
27+
new Dish("pulao", 600, new ArrayList<>()));
2528
// to show whether empty List is counted in flatMap
2629
System.out.println(countTotalIngredientsInAllDishes(dishes));
2730
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.rampatra.misc;
2+
3+
import java.util.Date;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* @author rampatra
9+
* @since 2019年05月15日
10+
*/
11+
public class MapWithTimestamp<K, V> {
12+
13+
private final Map<K, Map<Long, V>> map = new HashMap<>();
14+
15+
public V get(K key, Long timestamp) {
16+
Map<Long, V> entry = map.get(key);
17+
18+
return entry != null ? entry.get(timestamp) : null;
19+
}
20+
21+
public void put(K key, Long timestamp, V value) {
22+
Map<Long, V> entry = map.get(key);
23+
24+
if (entry == null) {
25+
map.put(key, new HashMap<Long, V>() {{
26+
put(timestamp, value);
27+
}});
28+
} else {
29+
entry.put(timestamp, value);
30+
}
31+
}
32+
33+
public static void main(String[] args) throws Exception {
34+
MapWithTimestamp<Integer, Integer> mapWithTimestamp = new MapWithTimestamp<>();
35+
long timestamp1;
36+
long timestamp2;
37+
long timestamp3;
38+
39+
mapWithTimestamp.put(1, timestamp1 = new Date().getTime(), 10_0);
40+
mapWithTimestamp.put(2, timestamp2 = new Date().getTime(), 20_0);
41+
Thread.sleep(100);
42+
mapWithTimestamp.put(2, new Date().getTime(), 20_1);
43+
Thread.sleep(100);
44+
mapWithTimestamp.put(2, new Date().getTime(), 20_2);
45+
mapWithTimestamp.put(3, timestamp3 = new Date().getTime(), 30_0);
46+
System.out.println(mapWithTimestamp.get(2, timestamp2));
47+
System.out.println(mapWithTimestamp.get(3, timestamp2));
48+
System.out.println(mapWithTimestamp.get(3, timestamp3));
49+
}
50+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /