๐ก Java Collections Framework: List, Set, Map โ Executive Summary
Technical architectural analysis, practical configuration manifests, and deployment instructions for Java Collections Framework: List, Set, Map.
Subpage
Spring
Roadmap
Java Collection
Map
Object โ Map (Jackson.ObjectMapper)
java
ObjectMapper objectMapper = new ObjectMapper();
Person person = new Person(12, "kim");
Map<String, Object> map = objectMapper.convertValue(person, Map.class);
Map โ String for Logging (Collection.Stream)
java
String resultSet =
{{ map_var }}.entrySet()
.stream()
.map(e -> e.getKey() + "=" + String.join(", ", e.getValue()))
.collect(Collectors.joining(" "));
log.debug(resultSet);
...
List
Array Parse with List (Lists or Arrays)
java
// List to Array
long[] longArr = Longs.toArray(longList);
// List to Array (Object)
String[] strArr = StringList.toArray(new String[0]);
// Array to List
Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
List<Integer> targetList = Lists.newArrayList(sourceArray);
List<String> targetList2 = Arrays.asList(new String[] {""});
JSON
Object โ JsonString (Jackson.ObjectMapper)
The Object -> Json conversion process is called serialization. In addition to String, Jackson can also write to files or streams.
java
ObjectMapper objectMapper = new ObjectMapper();
Person person = new Person("Nicole", 20);
String personJson = objectMapper.writeValueAsString(person);
System.out.println(personJson);
JsonString โ Object (Jackson.ObjectMapper)
The Json -> Object conversion process is called Deserialization. Like serialization, it is also possible to write to a file or stream.
java
String personJson = "{isJsonString}"
Person jsonToPerson = objectMapper.readValue(personJson, Person.class);
Reactive Streams
| Comparison items | Java Stream API | WebFlux (Project Reactor) |
| How it works | Pull-based (synchronous) | Push-based (asynchronous) |
| Blocking | Blocking. The thread waits until all data has been collected. | Non-blocking. Schedule to be notified when the data is ready. |
| Data Source | Collections already in memory (List, Set, etc.) | Data flow that does not know when it will arrive (network, DB, etc.) |
| Purpose of use | Quickly calculate large amounts of data (parallel processing, etc.) | Efficiently handle many concurrent requests with fewer threads |
| Reusability | Consume it once and it’s over (closed) | Multiple subscription possible |
Java Stream
java
/* Stream.map() */
List<?> baseList = new ArrayList<>();
List<?> rtnList = baseList.stream().map(obj->{
return obj.value
}).collect(Collectors.toList());
/** Map ๋ด ๋ชจ๋ ๋ฆฌ์คํธ ๋ณํฉ */
((HashMap<String,List<String>>) obj).values().stream().flatMap(List::stream).collect(Collectors.toList()));
/** ํํ์ ์ค๋ช
์๋ ๋ชจ๋ ๊ธฐ๋ฅ ๋์ผ*/
// .A(B::C) ์ข
๋ฅ (.A(D->{})๋ฅผ ๊ฐ์ )
// 1. .A(D->D.C())
.map(Map.Entry::getValue) == .map(entry -> entry.getValue())
// 2. Consumer<T(D)> void : .A(D->B.C(D))
.forEach(System.out::println) == .forEach(item -> System.out.println(item))
// 3. Supplier<T(new)> retrun T : .A(()->B.C())
(Math::random) == (()->Math.random())
// 4. Function<T(D),T(B)> ? : .A(D->B.C(D))
(Interger::parseInt) == (str->Integer.parseInt(str));
// 5. Predicate<T(B)> return Boolean : .A(D->D.C())
(String::isEmpty)
// ์ด๊ฒ์ ๋จ์ํํ๋ ค ํด๋ณด์์ง๋ง, ๋ถ์๊ฒฐ๊ณผ ์ฌ์ค Functional Interface์ ์ ์์ ๋ฐ๋ผ ๋ชจ๋ ๋ค๋ฅธ ๊ฒฐ๊ณผ์ผ๊ฒ์ผ๋ก ๋ณด์
WebFlux (Project Reactor)
java
//ClientRequest
//ClientResponse
response.bodyToMono()
//Body and Mono
Publisher.subscribe(
value -> { //when then (Published value)
},
error -> { //when throwed
},
() -> { //when completed
}
)
Failed
java
//ClientResponse๋ฅผ Publisher(bodyToMono(), bodyToFlux())๋ฅผ ์ฌ์ฉํ์ฌ ๋ก๊น
//์ธ๋ถ ์นํด๋ผ์ด์ธํธ์ ์ด๋ฒคํธ์ ์ํด ๋ค์ค์คํ๋๋ ๋ฉ์๋์ด๋ฏ๋ก subscribe()-๋น๋๊ธฐ-๋ฅผ ํ๊ฒ๋๋ฉด ์๋ฌ๋ฐ์.
/** ์๋ฌ ์์ */
clientResponse.bodyToMono(String.class)
.doOnNext(responseBody -> {
sb.append("\tBody: \n");
sb.append("\t\tContents: " + responseBody + " \n");
})
.doOnTerminate(()-> {
log.info(sb.toString());
})
.subscribe();
return Mono.just(clientResponse);
/** ์๋ชป๋ ์์ */
clientResponse.bodyToMono(String.class)
.flatMap(responseBody -> {
sb.append("\tBody: \n");
sb.append("\t\tContents: " + responseBody + " \n");
log.info(sb.toString());
return Mono.empty();
});
return Mono.just(clientResponse);
/*
java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-2
์ด ๋ฉ์์ง๋ ๋ค์์ ๋ํ๋
๋๋ค:
block(), blockFirst(), ๋๋ blockLast()์ ๊ฐ์ ๋ธ๋กํน ์ฐ์ฐ์ ์ค๋ ๋ "reactor-http-nio-2"์์ ์ฌ์ฉํ๊ณ ์๋ค๋ ๊ฒ์
๋๋ค.
Reactor๋ ๋น๋๊ธฐ ๋ฐ ๋
ผ๋ธ๋กํน ์คํธ๋ฆผ ์ฒ๋ฆฌ๋ฅผ ์ํด ์ค๊ณ๋์์ผ๋ฏ๋ก ๋ธ๋กํน ์ฐ์ฐ์ ์ฌ์ฉํ๋ฉด ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.
*/
/** ๋ฐฉ๋ฒ */
clientResponse.bodyToMono(String.class)
.subscribeOn(Schedulers.elastic())
.subscribe(responseBody -> {
sb.append("\tBody: \n");
sb.append("\t\tCount: " + JsonParserFactory.getJsonParser().parseMap(responseBody).size() + " \n");
sb.append("\t\tContents: " + responseBody + " \n");
log.info(sb.toString());
});
// }
return Mono.just(clientResponse);
/* Steam์ ์ฆ์ ์ข
๋ฃํ๊ณ ๋ด์ฉ๋ฌผ์ ๊บผ๋ด๊ธฐ */
Mono<String> mono = Mono.just("Hello, Reactor!");
mono
.take(1)
.subscribe(
value -> {
// ์ฒซ ๋ฒ์งธ ์์๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.
System.out.println("Received value: " + value);
},
error -> {
// ์๋ฌ ์ฒ๋ฆฌ
},
() -> {
// ์๋ฃ ์ฒ๋ฆฌ
}
);
| Method | Input | Output | Reactor processing (Unwrapping) |
map
|
T
|
R (generic object)
|
Move on to the next step as received |
flatMap
|
T
|
Mono or Flux
|
Open the box Take out only the contents R and move on to the next step
|
flatMapMany
|
T
|
Flux
|
Expands Mono into Flux, which contains multiple particles.
|
flatMapIterable
|
T
|
Iterable (List, etc.)
|
The list is split one by one and exported sequentially. |




Leave a Reply