본문 바로가기

개발/자바12

java List.of에는 null이 포함될 수 없다. Java List.of() 에는 null이 요소로 포함될 수 없다.List.of()의 설명을 살펴보면 'Throws: NullPointerException - if an element is null' 리스트 요소가 null인 경우 NullPointerException을 발생한다고 명시되어있다. 위의 of()에서 호출하고 있는 ListN() 메서드의 구현을 살펴보면, Objects.requreNonNull()로 null 체크를 하고 있다. list.contains(null) 수행 시 NPE가 발생한다.추가로 List.of()로 생성된 불변리스트에 contains(null) 수행 시에도 NPE가 발생한다.이것도 코드 구현을 따라가보면 원인을 알 수 있다.contains() 내에서 indexOf() 라는 메서.. 2024. 5. 28.
JMH benchmark 동기 대량의 데이터를 쪼개어 처리할 일이 있어, stream 보다는 parallelStream을 사용해보려고 했다. 당연히 parallelStream이 훨씬 빠르겠거니 하고 테스트를 해본 결과 stream이 더 빠른 결과가 나왔다. 대체 어떤 상황인지 이해가 되지 않아 구글링을 해보니, 해당 글을 발견했다. 짧게 수행한 벤치마킹은 JIT 컴파일러가 동작하는 중간에 혹은 동작하기 전에 끝나버리기 때문에, 최대 처리량 측정하지 않게 된다. 즉, 내가 테스트한 결과가 정확하지 않다는 것이다. JVM에서 성능테스트를 하기란 쉽지 않기 때문에 JMH같이 잘 만들어진 벤치마킹 프레임워크로 테스트하는 것을 추천하여 접하게 됐다. 적용 gradle plugins { id "me.champeau.jmh" version .. 2023. 3. 12.
Java Atomic Atomic 이란? Java에서 동시성을 보장해주는 자료형이다. Java docs에서 AtomicInteger에 대해 아래와 같이 설명하고 있다. An int value that may be updated atomically. (생략) An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer. However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes. 원자적으.. 2022. 12. 3.
Java Closure Closure란? MDN에서는 Closure를 아래와 같이 설명하고 있다. A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). 클로저는 주변 상태에 대한 참조와 함께 묶인(포함된) 함수의 조합입니다. 네..? 무슨 말인지 잘 모르겠다. 간단한 예시로 closure에 대해 이해해보자. public int lambda() { BiFunction function = (x, y) -> x + y; return function.apply(1, 2); } public int closure() { int z = 3; BiF.. 2022. 12. 3.