Skip to content

[BE] Java 17 ์‚ฌ์šฉ

kyY00n edited this page Aug 17, 2023 · 1 revision

Java 17


Java 17์„ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ๊ทผ๊ฑฐ๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ์ˆ ์ , ์ƒ์‚ฐ์ ์ธ ์ด์œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  17์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

JDK 17

Type Features Content
ํŒจํ‚ค์ง€ ํ–ฅ์ƒ๋œ ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ(Pseudo-Random Number Generator)๋ฅผ ์œ„ํ•œ ์ƒˆ๋กœ์šด ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…๊ณผ ๊ตฌํ˜„์„ ์ œ๊ณต
ํŒจํ‚ค์ง€ ์‹ ๊ทœ Mac OS ๋ Œ๋”๋ง ํŒŒ์ดํ”„๋ผ์ธ Apple ๋ฉ”ํƒˆ API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” Mac OS์šฉ Java ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ตฌํ˜„
๊ธฐ๋Šฅ ํ…์ŠคํŠธ ๋ธ”๋ก ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๊ธฐ์กด String์„ ์—ฌ๋Ÿฌ ์ค„ ์ž‘์„ฑํ•  ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ธฐ๋Šฅ, ๊ฐ€๋…์„ฑ ์žˆ๋Š” ์ฝ”๋“œ ์ง€์›
๊ธฐ๋Šฅ Switch ํ‘œํ˜„์‹ ๊ธฐ๋Šฅ ํ–ฅ์ƒ Switch ๋ฌธ ์ด์šฉ ์‹œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ด์šฉ ๊ฐ€๋Šฅ ํ•˜๋ฉฐ, ๋žŒ๋‹ค ์Šคํƒ€์ผ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
๊ธฐ๋Šฅ Record Data class ์ถ”๊ฐ€ immutable ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ƒˆ๋กœ์šด ์œ ํ˜•์˜ ํด๋ž˜์Šค๋กœ ๊ธฐ์กด toString, equals, hashCode Method์— ๋Œ€ํ•œ ๊ตฌํ˜„์„ ์ž๋™ ์ œ๊ณต
๊ธฐ๋Šฅ Instanceof ๋งค์นญ ์ด์ „ ๋ฒ„์ „ ๊ฒฝ์šฐ Instanceof ๋‚ด๋ถ€์—์„œ ๊ฐ์ฒด๋ฅผ ์บ์ŠคํŒ… ํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜์˜€์œผ๋‚˜, ์บ์ŠคํŒ… ๊ณผ์ •์„ ๋‚ด๋ถ€์—์„œ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ€๊ฒฝ
๊ธฐ๋Šฅ NumberFormat ํด๋ž˜์Šค ๊ธฐ๋Šฅ ํ–ฅ์ƒ ๊ธฐ์กด ์ˆซ์ž Format ํด๋ž˜์Šค(NumberFormat) ๋‚ด Method ์ถ”๊ฐ€(getCompactNumberInstance)
๊ธฐ๋Šฅ DateTimeFormatter ํด๋ž˜์Šค ๊ธฐ๋Šฅ ํ–ฅ์ƒ ๊ธฐ์กด ๋‚ ์งœ Format ํด๋ž˜์Šค(DateTimeFormatter) ๋‚ด ํŒจํ„ด Method ํ˜•์‹ ์ถ”๊ฐ€("B")
๊ธฐ๋Šฅ ๋ด‰์ธ(Sealed) ํด๋ž˜์Šค ๋ฌด๋ถ„๋ณ„ํ•œ ์ƒ์†์„ ๋ง‰๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ๋“ฑ์žฅํ•œ ๊ธฐ๋Šฅ์œผ๋กœ ์ง€์ •ํ•œ ํด๋ž˜์Šค ์™ธ ์ƒ์†์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์ง€์ •ํ•œ ํด๋ž˜์Šค ์™ธ ์ƒ์† ๋ถˆ๊ฐ€๋Šฅ
๊ธฐ๋Šฅ Stream.toList() ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๊ธฐ์กด, Stream์„ List๋กœ ๋ณ€ํ™˜ ์‹œ Collectors์—์„œ ๊ธฐ๋Šฅ์„ ์ฐพ์•„ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด Java17 ๋ถ€ํ„ฐ๋Š” Collectorsํ˜ธ์ถœ ์—†์ด toList()๋งŒ์œผ๋กœ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅ

์ฐธ์กฐ : https://techblog.gccompany.co.kr/%EC%9A%B0%EB%A6%AC%ED%8C%80%EC%9D%B4-jdk-17%EC%9D%84-%EB%8F%84%EC%9E%85%ED%95%9C-%EC%9D%B4%EC%9C%A0-ced2b754cd7

์ด์   ์ €ํฌ๊ฐ€ Java 17์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ…์ŠคํŠธ ๋ธ”๋ก


JDK 15๋ถ€ํ„ฐ ์ง€์›ํ•˜๋Š” ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. "(ํฐ ๋”ฐ์˜ดํ‘œ) 3๊ฐœ๋ฅผ ์•ž๋’ค๋กœ ๋ถ™์—ฌ์ฃผ๊ฒŒ ๋œ๋‹ค๋ฉด ์ผ๋ฐ˜ ๋ฌธ์ž์—ด๋ณด๋‹ค ํ›จ์”ฌ ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด jpql ๋˜๋Š” native query, json ๋“ฑ์˜ ๋ฌธ์ž์—ด์„ ํ›จ์”ฌ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Override
@Query("""
    SELECT pf FROM PetFood pf
       JOIN FETCH pf.brand b
       JOIN FETCH pf.primaryIngredients pi
""")
List<PetFood> findPetFoods();

Record Data Class ์ถ”๊ฐ€


Record Data Class๋Š” JDK 14 ๋ฒ„์ „๋ถ€ํ„ฐ ๊ณต๊ฐœ๋œ Immutable(๋ถˆ๋ณ€) ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ƒˆ๋กœ์šด ์œ ํ˜•์˜ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. Record์˜ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • getter/setter, toString, equals&hashcode ๋ฉ”์„œ๋“œ๊ฐ€ ์ž์ฒด์ ์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.
  • ๋ถˆ๋ณ€ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๊ฐ’์€ ์ƒ์„ฑ์ž๋กœ ์ƒ์„ฑ๋œ๋‹ค.
  • Data Transfer Object์™€ ๊ฐ™์€ ์šฉ๋„๋กœ ํ™œ์šฉํ•˜๊ธฐ ๋งค์šฐ ํŽธ๋ฆฌํ•˜๋‹ค.

Record Data Class๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

public record CreateReviewRequest(
        @NotNull(message = "Null์ด ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’์ธ์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”.")
        Long petFoodId,

        @Max(5)
        @Min(1)
        Integer rating,

        @Size(max = 255, message = "255์ž ๋ฏธ๋งŒ์˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.")
        String comment,

        @NotBlank(message = "Null ๋˜๋Š” ๊ณต๋ฐฑ์ด ํฌํ•จ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’์ธ์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”.")
        String tastePreference,

        @NotBlank(message = "Null ๋˜๋Š” ๊ณต๋ฐฑ์ด ํฌํ•จ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’์ธ์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”.")
        String stoolCondition,
        List<String> adverseReactions
) {

    public Review toEntity(Pet pet, PetFood petFood) {
        return Review.builder()
                .pet(pet)
                .petFood(petFood)
                .rating(rating())
                .comment(comment)
                .tastePreference(TastePreference.from(tastePreference()))
                .stoolCondition(StoolCondition.from(stoolCondition()))
                .build();
    }

}

Stream toList ๋ฉ”์„œ๋“œ ์ง€์›


Stream.toList() ๊ธฐ๋Šฅ์€ JDK 16๋ถ€ํ„ฐ ๊ณต๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ collect(Collectors.toList()) ๋ฌธ๋ฒ•์„ toList() ๋ฉ”์„œ๋“œ ํ•˜๋‚˜๋กœ ์ค„์—ฌ์„œ ๋” ๊ฐ„๊ฒฐํ•ด์ง‘๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

public record BreedsResponses(List<BreedsResponse> breeds) {

    public static BreedsResponses from(List<Breeds> breeds) {
        return new BreedsResponses(
                breeds.stream()
                        .map(breed -> new BreedsResponse(breed.getId(), breed.getName()))
                        .toList()
        );
    }

    public record BreedsResponse(long id, String name) {

    }

}

ํ•œ๊ฐ€์ง€ ๊ฑธ๋ฆฌ๋Š”์ ์€ collect(Collectors.toList())์™€ toList()์˜ ๋ฐ˜ํ™˜ ๊ฐ’ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, ์ „์ž๋Š” ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ›„์ž๋Š” ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ €ํฌ ํŒ€์—์„œ๋Š” persentation ๋ ˆ์ด์–ด๊ฐ€ ๋ฐ˜ํ™˜๋œ ์ปฌ๋ ‰์…˜์— ๋Œ€ํ•ด์„œ ๊ฐ’์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š๋„๋ก ๋…ผ์˜๊ฐ€ ์ด์–ด์กŒ๊ธฐ์—, ๊ธฐ์กด์˜ collect(Collectors.toUnmodifiableList())๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ• ์ง‘์‚ฌ์˜๊ณ ๋ฏผ

  1. ์ง‘์‚ฌ์˜ ๊ณ ๋ฏผ ์„œ๋น„์Šค ์†Œ๊ฐœ
  2. ์‚ฌ์šฉ์ž ์Šคํ† ๋ฆฌ ๋ฐ ๊ธฐ๋Šฅ๋ช…์„ธ
  3. ํŒ€ ๊ทธ๋ผ์šด๋“œ ๋ฃฐ
  4. Git Branch ์ „๋žต ๋ฐ Git Commit ์ปจ๋ฒค์…˜
  5. ์ง‘์‚ฌ์˜ ๊ณ ๋ฏผ ๋ฐฐํฌ ์ž๋™ํ™” ํŒŒ์ดํ”„๋ผ์ธ

Team Zipgo

  1. ํ‚ฅ์˜คํ”„ ๋ฏธํŒ… - ์šฐ๋ฆฌ์˜ ์‹œ์ž‘!
  2. ๋‚˜ ์‚ฌ์šฉ์„ค๋ช…์„œ
  3. 5์ฐจ ๋ฐ๋ชจ ํ˜‘์—… ๊ฒฝํ—˜ ๊ณต์œ 

ํ”„๋ก ํŠธ์—”๋“œ

๋ฐฑ์—”๋“œ

Clone this wiki locally