๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[JAVA] Array -> List, Set ๋ณ€ํ™˜ ๋ฐฉ๋ฒ• Arrays.asList() Arrays ํด๋ž˜์Šค์˜ asList() ๋ฉ”์†Œ๋“œ๋Š” list ๊ฐ์ฒด๋กœ collection์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฐ ํŽธ๋ฆฌํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ด์ค€๋‹ค. ์ฐธ๊ณ ๋กœ Java 8 ๊ธฐ์ค€์œผ๋กœ ์•„์ง java์—๋Š” set, map์˜ literal์ด ์—†๋‹ค. ๋‹ค์Œ์€ String ๋ฐฐ์—ด์„ List์™€ Set์œผ๋กœ ๊ฐ๊ฐ ๋ณ€ํ™˜ํ•˜๋Š” ์˜ˆ์ด๋‹ค. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Set wordsSet = new HashSet(); String sentence = "once upon a time in hollywood"; String[] arrayWords = sentence.split(" "); // Array -> List List wordsList = Arrays.asList(a.. 2020. 1. 15.
JAVA 8 ๋ณ€๊ฒฝ ์‚ฌํ•ญ - interface์˜ default ํ‚ค์›Œ๋“œ์™€ static ๋ฉ”์†Œ๋“œ Java์—์„œ ๊ธฐ์กด์˜ interface๋Š” ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋งŒ์„ ๋ฉค๋ฒ„๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ Java8๋ถ€ํ„ฐ default ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ interface์— ๋ฉ”์†Œ๋“œ๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. default ํ‚ค์›Œ๋“œ 1 2 3 4 5 6 7 8 9 10 public interface ICalculator { int add(int x, int y); int sub(int x, int y); default int mul(int x, int y) { return x * y; } } Colored by Color Scripter cs ๋ฉ”์†Œ๋“œ๋ฅผ default ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์„ ์–ธํ•จ์œผ๋กœ์จ ๋ฉ”์†Œ๋“œ์˜ body, ์ฆ‰ ๊ตฌํ˜„๋ถ€๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. ์œ„ ์˜ˆ์‹œ๋Š” ์„ธ ๊ฐœ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ฉค๋ฒ„๋กœ ๊ฐ–๋Š” ICalculator interfac.. 2020. 1. 2.