자바) List (23-05-26)
velog에서 이전한 글 입니다. List List list = Arrays.asList(new Integer[] {1,2,3}); List list2 = Arrays.asList(new int[]{1, 2, 3}); Stream.of랑 비슷한 느낌으로 원시 타입은 배열을 그대로 가져간다. List a1 = Arrays.asList(1,2,3); List a2 = List.of(1,2,3); a1.add(4); //UnsupportedOperationException a2.add(4); //UnsupportedOperationException //아래처럼 작성하자. List a = new ArrayList(Arrays.asList(1,2,3)); a.add(4); 둘 다 Immutable List 를 생..
2023.07.13
자바) enum, 열거형 (23-05-24)
velog에서 이전한 글 입니다. enum public class Test { enum Direction { EAST("East", 10), WEST("West", -10), SOUTH("South", -20), NORTH("North", 20); private final String label; private final int val; Direction(String label, int val) { this.label = label; this.val = val; } public String label() { return label; } public int val() { return val; } private static final Map BY_LABEL = Stream.of(values()).collec..
2023.07.13
자바) Stream.of() (23-05-24)
velog에서 이전한 글 입니다. Stream.of Stream stream = Stream.of(new int[3]); Stream stream1 = Stream.of(new char[3]); public static Stream of(T t) { return StreamSupport.stream(new Streams.StreamBuilderImpl(t), false); } 위의 Stream.of는 StreamSupport.stream과 이어진다. Stream carStream = Stream.of(new Car[3]); Stream stringStream = Stream.of(new String[3]); @SafeVarargs @SuppressWarnings("varargs") // Creating ..
2023.07.13
자바) Stream.of/ Arrays.stream/ Integer <-> int (23-05-18)
velog에서 이전한 글 입니다. Stream Stream.of/Arrays.stream int[] a = {1,2,3}; int[] b = {4,5}; String a = "hi my name is"; String[] b = a.split(" +"); Stream b1 = Stream.of(b); //참고 IntStream stream = Arrays.stream(a); Stream a1 = Stream.of(a); int[] c2 = Stream.of(a, b).flatMapToInt((v)->{ System.out.println(Arrays.toString(v)); return Arrays.stream(v); }).toArray(); System.out.println(Arrays.toString(c..
2023.07.13
자바) 람다식/ 메서드 참조/ stream (23-05-18)
velog에서 이전한 글 입니다. 람다식 메서드 참조 //생성자를 호출하는 람다식도 메서드 참조로 변환할 수 있다. Supplier s = () -> new MyClass(); Supplier s = MyClass::new; //배열 생성의 경우 Function f = x -> new int[x]; Function f = int[]::new; //매개변수의 개수에 따라 알맞은 함수형 인터페이스를 사용한다. BiFunction bf = (i, s) -> new MyClass(i,s); BiFunction bf = MyClass::new; 스트림 public static void main(String[] args) { ArrayList t = new ArrayList(); t.add(3); t.add(5);..
2023.07.13
자바) 함수형 인터페이스/ util.function (23-05-16)
velog에서 이전한 글 입니다. 람다식 @FunctionalInterface interface MyFunction { void run(); // public abstract void run(); } class Main { static void execute(MyFunction f) { // 매개변수의 타입이 MyFunction인 메서드 f.run(); } static MyFunction getMyFunction() { // 반환 타입이 MyFunction인 메서드 MyFunction f = () -> System.out.println("f3.run()"); return f; } public static void main(String[] args) { // 람다식으로 MyFunction의 run()을 구..
2023.07.13
자바) generics/ enum/ annotation (23-05-15)
velog에서 이전한 글 입니다. Generics 와일드 카드 ArrayList
2023.07.13
자바) regex/ generics (23-05-11)
velog에서 이전한 글 입니다. 23.5.2 ~ 23.5.19 사전 캠프 기간의 TIL 기록입니다! TIL: Today I Learned 정규표현식(regex) public class Main { public static void main(String[] args) { String t = "^[0-9]*$"; String res = "1597asd123".replaceAll(t, "yes"); System.out.println(res); String res2 = "".replaceAll(t, "yes"); System.out.println(res2); String res3 = "1235512".replaceAll(t, "yes"); System.out.println(res3); } } 결과 1597as..
2023.07.13
자바) Map반복/ List ImmutableCollections (23-05-10)
velog에서 이전한 글 입니다. 23.5.2 ~ 23.5.19 사전 캠프 기간의 TIL 기록입니다! TIL: Today I Learned pwd public class Main { public static void main(String[] args) { String currentPath = Paths.get("").toAbsolutePath().toString(); String currentPath2 = new File("").getAbsolutePath(); System.out.println(System.getProperty("user.dir")); System.out.println(currentPath); System.out.println(currentPath2); } } 결과 ...\IdeaPro..
2023.07.13