-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP12.java
More file actions
41 lines (35 loc) · 1.03 KB
/
P12.java
File metadata and controls
41 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package lists;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
/**
* Given a run-length code list generated as specified in problem P11.
* Construct its uncompressed version.
*/
public final class P12 {
private P12() {
}
/**
* Uncompressed list of items.
*
* @param list input list of items
* @param <T> type of item
* @return list of uncompressed list
*/
@SuppressWarnings("unchecked")
public static <T> List<T> decode(final List<Object> list) {
List<Object> newList = new ArrayList<>();
for (Object obj : list) {
if (obj instanceof SimpleEntry) {
IntStream.range(0, (Integer) ((SimpleEntry) obj)
.getKey())
.mapToObj(i -> ((SimpleEntry) obj).getValue())
.forEach(newList::add);
} else {
newList.add(obj);
}
}
return (List<T>) newList;
}
}