Java 11 New Features

1. String ๋ฉ”์„œ๋“œ

strip() - ๋ฒ—๊ธฐ๋‹ค

๋ฌธ์ž์—ด์˜ ๊ณต๋ฐฑ์„ ์•ž/๋’ค/ ์•ž๋’ค ์ œ๊ฑฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        String andrew = " ANDREW ";
        System.out.println(andrew.strip()); // "ANDREW"
        System.out.println(andrew.stripLeading()); // "ANDREW "
        System.out.println(andrew.stripTrailing()); // " ANDREW"
				System.out.println(andrew.stripTrailing().stripLeading()); // "ANDREW"
    }

isBlank()

String์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜, ๊ณต๋ฐฑ๋งŒ์„ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ true๋ฅผ ๋ฐ˜ํ™˜, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋นˆ๋‹ค.

String emptyWhiteSpace = "";
System.out.println(andrew.isBlank());
System.out.println(andrew.isEmpty());

โ€ป isEmpty vs isBlank ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋‚˜์š”?

์–ผํ•๋ณด๋ฉด ๊ธฐ์กด์— String ํด๋ž˜์Šค์˜ isEmpty๋ž‘ ๋ณ„ ์ฐจ์ด๊ฐ€ ์—†์–ด ๋ณด์ด์ง€๋งŒ, ํ•œ ์นธ ๋„์–ด์“ด ๋ฌธ์ž์—ด์„ ๋‹ด๋Š” ๋ณ€์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด๋ด…์‹œ๋‹ค.

String emptyStr = " ";
System.out.println(emptyStr.isBlank()); //true
System.out.println(emptyStr.isEmpty()); // false

JDK11์—์„œ isBlank() ๋ฉ”์„œ๋“œ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋น„์–ด์žˆ๋Š”, ๊ณต๋ฐฑ์„ ํฌํ•จ ํ•˜๋Š” ๊ฒฝ์šฐ (๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์กด์žฌ ํ•˜๋”๋ผ๋„ ๋น„์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—) ๊ฐ’์ด true๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ , isEmpty() ๋ฉ”์„œ๋“œ๋Š” ๋„์–ด์“ฐ๊ธฐ ์ž์ฒด๋ฅผ ๋นˆ ๋ฌธ์ž์—ด๋กœ ์ธ์‹ํ•˜๊ธฐ๋•Œ๋ฌธ์— isEmpty()๊ฐ€ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

lines()

์ด ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด ์ŠคํŠธ๋ฆผ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

var lines = "hello world\nI'm developer";
System.out.println(lines);
//hello world
//I'm developer

System.out.println(lines.lines().collect(Collectors.toList()));
// [hello world, I'm developer]

repeat(n)

n๋ฒˆ ๋ฌธ์ž์—ด์˜ concat(์—ฐ๊ฒฐํ•˜๋‹ค)

String hello = "hello";
System.out.println(hello.repeat(3)); //hellohellohello

2.Files ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์„œ๋“œ

writeString

Files์˜ static ๋ฉ”์„œ๋“œ๋กœ ์ •์˜๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ปจํ…์ธ ๋ฅผ file๋กœ ์“ฐ๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž๋Š” ์ง€์ •๋œ charset์„ ์‚ฌ์šฉํ•˜์—ฌ byte๋กœ ์ธ์ฝ”๋”ฉ๋˜๋ฉฐ ๊ธฐ๋ณธ๊ฐ’์€ UTF-8 charset์ž…๋‹ˆ๋‹ค.

ReadString

Files.writeString(Path.of("test.txt"), "hello");
System.out.println(Files.readString(Path.of("test.txt")));

isSameFile

System.out.println(Files.isSameFile(Path.of("test.txt"), Path.of("1.txt"))); //false
System.out.println(Files.isSameFile(Path.of("test.txt"), Path.of("test.txt"))); //true

์ฒซ๋ฒˆ์งธ, file๋ช…๋งŒ ๋‹ค๋ฅด๊ณ  ์•ˆ์— ๋‚ด์šฉ์€ ๊ฐ™์€๋ฐ ๊ฐ’์ด false๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๋ฆ„์„ ํ†ตํ•ด์„œ๋งŒ ๊ฐ™์€ ํŒŒ์ผ์ธ์ง€ ๋น„๊ต ํ•˜๋Š” ๋“ฏ ๋ณด์ž…๋‹ˆ๋‹ค.

3. Optional.isEmpty()

Optionalํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Optional<String> name = Optional.empty();
System.out.println(name.isEmpty()); //JDK11 //true
System.out.println(name.isPresent()); //JDK8 //false

์‚ฌ์‹ค, isPresent ๋ฉ”์„œ๋“œ ํ•˜๋‚˜๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, true, false ํ•œ ๊ฐ’์— ๋”ฐ๋ผ์„œ, ๋ฉ”์„œ๋“œ ๋ช…์ด ์ง๊ด€์ ์ด์ง€ ์•Š์•„์„œ ์ถ”๊ฐ€ํ•œ๊ฒŒ ์•„๋‹๊นŒ ์ƒ๊ฐํ•ด๋ด…๋‹ˆ๋‹ค..

4. TimeUnit covert(Duration duration)

๊ธฐ๊ฐ„(duration)์„ ์œ ๋‹›์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. Duration.toNanos() ์™€๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค. toNanos()๋ฉ”์„œ๋“œ๋Š” ์ˆซ์ž๊ฐ€ overflow๋ ์‹œ, ArithmeticException์„ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค .

TimeUnit timeUnit = TimeUnit.DAYS;
System.out.println(timeUnit.convert(Duration.ofHours(24)));
System.out.println(timeUnit.convert(Duration.ofHours(36)));// ํ•˜๋ฃจ๊ฐ€ ์ง€๋‚˜๋„, ๋ฐ˜ํ™˜์€ ํ•˜๋ฃจ๋กœ ๋ฐ˜ํ™˜

System.out.println(timeUnit.convert(Duration.ofMinutes(60 * 24)));
System.out.println(timeUnit.convert(Duration.ofMinutes(60 * 24 + 10)));

์—ฐ๊ด€ ํฌ์ŠคํŠธ