forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap2Objects.java
More file actions
45 lines (34 loc) · 1015 Bytes
/
HashMap2Objects.java
File metadata and controls
45 lines (34 loc) · 1015 Bytes
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
42
43
44
45
package com.zetcode;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Stream;
public class HashMap2Objects {
public static void main(String[] args) {
Map<String, Integer> values = new HashMap<>();
values.put("Ollie", 3);
values.put("Finn", 4);
values.put("Teddy", 6);
values.put("Jasper", 1);
values.put("Lucky", 2);
Stream<Cat> cats = values.entrySet().stream().map(entry ->
new Cat(entry.getKey(), entry.getValue())
);
cats.forEach(System.out::println);
}
}
class Cat {
private String name;
private Integer age;
public Cat(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return new StringJoiner(", ", Cat.class.getSimpleName() + "[", "]")
.add("name='" + name + "'")
.add("age=" + age)
.toString();
}
}