๐ŸŒป JAVA

๐ŸŒป JAVA/์ •๋ฆฌ์ •๋ฆฌ์ •๋ฆฌ

Java9 , Map.of(key, value);

https://www.amitph.com/create-hashmap-in-java/ Map.of ์—๋Š” key์™€ value๋ฅผ ์ตœ๋Œ€ 10๊ฐœ ๊นŒ์ง€ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์›ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด map์„ ์ƒ์„ฑํ•˜๋ฉด ๋œ๋‹ค. Map map = Map.of(1, "first", 2, "second"); ์‹คํ–‰ ๊ฒฐ๊ณผ {"1":{"name":"first"},"2":{"name":"second"}} example import java.util.Map; public class MapOf1 { public static void main(String[] args) { Map map = Map.of(101, "PP", 102, "QQ", 103, "RR"); map.forEach((k, v) -> System.out.println(..

๐ŸŒป JAVA/์ •๋ฆฌ์ •๋ฆฌ์ •๋ฆฌ

java 8, Optional<E> ์‚ฌ์šฉ๋ฒ•

์ž๋ฐ” 8์—์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ Optional ๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์ž. Member member = new MemberRepository(member.findById(id)); if(member != null){ ... }else{ ... } findById ๋ฉ”์†Œ๋“œ๊ฐ€ member ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค๊ณ  ํ•˜๋ฉด, ๋งŒ์•ฝ member๊ฐ€ null์ผ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด ์ด๋ ‡๊ฒŒ ์ฝ”๋”ฉ์„ ํ•ด์•ผํ•œ๋‹ค. findById๊ฐ€ memberํƒ€์ž… ๊ฐ์ฒด ๋ง๊ณ , Optionalํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๊ฒŒ ํ•œ๋‹ค๋ฉด? // ์•ˆ ์ข‹์Œ List members = team.getMembers(); return Optional.ofNullable(members); // ์ข‹์Œ List members = team.getMembers(); return me..

๐ŸŒป JAVA/์ •๋ฆฌ์ •๋ฆฌ์ •๋ฆฌ

[JAVA] ์ •๋ ฌ Comporable , Comparator

String, Integer, Double ๋“ฑ ํด๋ž˜์Šค ๋‚ด๋ถ€์—๋Š” ๋ชจ๋‘ compare ๋ฉ”์†Œ๋“œ๊ฐ€ ๋‚ด์žฅ ๋˜์–ด์žˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. ๋งŒ์•ฝ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค. ์ด int compare ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜์ž๋ฉด, ์ •๋ ฌ์ด ์ง„ํ–‰๋  ๋•Œ ์ž๋ฆฌ๋ฐ”๊ฟˆ(=์ •๋ ฌ) ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋งŒ์•ฝ return๊ฐ’์ด 0์ด๋‚˜ ์Œ์ˆ˜์ด๋ฉด ์ž๋ฆฌ๋ฐ”๊ฟˆ์„ ํ•˜์ง€ ์•Š๊ณ , ์–‘์ˆ˜์ด๋ฉด ์ž๋ฆฌ๋ฐ”๊ฟˆ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋งŒ์•ฝ ์˜ค๋ฆ„์ฐจ์ˆœ์ด ์•„๋‹ˆ๋ผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค. compare ๋ฉ”์†Œ๋“œ ์ฝ”๋“œ @Override public static int compare(int x, int y) { return x < y ? -1 : ((x == y) ? 0 ..

๐ŸŒป JAVA/์ •๋ฆฌ์ •๋ฆฌ์ •๋ฆฌ

์ž๋ฐ” 8, interface default ๋ฉ”์†Œ๋“œ

์ธํ„ฐํŽ˜์ด์Šค์˜ default method ์ธํ„ฐํŽ˜์ด์Šค์˜ default ๋ฉ”์†Œ๋“œ JAVA 8์ด ๋“ฑ์žฅํ•˜๋ฉด์„œ interface์— ๋Œ€ํ•œ ์ •์˜๊ฐ€ ๋ช‡ ๊ฐ€์ง€ ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค. default๋ฉ”์†Œ๋“œ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ defaultํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ๋˜๋ฉด ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ตฌํ˜„๋  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋Š” default๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•  ์ˆ˜ ์žˆ๋‹ค. public interface Calculator { public int plus(int i, int j); public int multiple(int i, int j); default int exec(int i, int j){ //default๋กœ ์„ ์–ธํ•จ์œผ๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. return i + j; } } //Calculator์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ MyCalculatorํด๋ž˜์Šค publ..

๐ŸŒป JAVA/์ •๋ฆฌ์ •๋ฆฌ์ •๋ฆฌ

[Java] URI / URL ์˜ ์ฐจ์ด

URI์™€ URL์˜ ์ฐจ์ด Uniform Resource Identifier (URI) consists of a string of characters used to identify or name a resource on the Internet http://en.wikipedia.org/wiki/URI URI๋Š” ์ธํ„ฐ๋„ท ์ƒ์˜ ์ž์›์„ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ๋ฌธ์ž์—ด์˜ ๊ตฌ์„ฑ์ฏค์œผ๋กœ ํ•ด์„ ๋  ์ˆ˜ ์žˆ๊ฒ ๋‹ค. http://en.wikipedia.org/wiki/URL URI์˜ ํ•œ ํ˜•ํƒœ์ธ URL์€ ์ธํ„ฐ๋„ท ์ƒ์˜ ์ž์› ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. URL๋Š” URI์˜ ํ•œ ํ˜•ํƒœ๋กœ, ๋ฐ”๊ฟ” ๋งํ•˜๋ฉด URI๋Š” URL์„ ํฌํ•จ ํ•˜๋Š” ๊ฐœ๋…์ด๋‹ค. (URI > URL) ์ธํ„ฐ๋„ท ์ƒ์˜ ์ž์›์˜ ์œ„์น˜์™€ ์‹๋ณ„์ž. ์–ธ๋“ฏ ๋ณด๋ฉด ๊ฐ™์€ ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” ๋“ฏ ํ•˜๋‹ค. ํ•˜์ง€๋งŒ &#39;์ž์›์˜ ์œ„..

๐ŸŒป JAVA/์ •๋ฆฌ์ •๋ฆฌ์ •๋ฆฌ

[Java] isNumeric(String s) ๋ฉ”์†Œ๋“œ

public static boolean isNumeric(String s) { try { Double.parseDouble(s); return true; } catch(NumberFormatException e) { return false; } } String s ; boolean chk = isNumeric( s );

iseunghan
'๐ŸŒป JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (11 Page)