서론 Arrays.sort에서 기본형 배열을 사용할 때 주의점을 소개합니다. 본론 Comparator Arrays.sort를 사용할 때 기본형 배열 int[], char[], long[] 등을 사용한다면 Comparator를 만들어 사용할 수 없습니다. 그래서 아래와 같은 에러가 발생합니다. error: no suitable method found for sort(int[],(a,b)->b - atable method found for sort(int[],(a,b)->b - a`table method found for sort(int[],(a,b)->b - atable method found for sort(int[],(a,b)->b - a`) Arrays.sort(nums, (a, b) -> b - a..
서론 string을 변환하기 위한 시도들을 모아보았습니다. 본론 Array String을 char[]로 만들기 위해서는 내부의 toCharArray()메서드를 이용하면 됩니다. public char[] toCharArray() // String -> char[] stream Arrays.stream()으로는 char[]를 stream으로 변환할 수 없습니다. String 클래스에서는 이를 Intstream으로 변환해주는 메서드가 있습니다. 이 뒤에 map 등을 이용해 char로 변환해서 사용할 수 있습니다. public IntStream chars() //String -> IntStream someString.chars().map(c -> (char)c); 결론 복잡하지만 재미있는 stream 사용입니다.
서론 java stream의 reduce를 사용하는 방법을 소개합니다. 본론 reduce reduce에서는 identity와 lambda expression 두 가지를 매개변수로 받아 작업을 수행합니다. identity는 계산을 수행하기 전 초기 값으로 각 계산의 결과가 입력으로 재사용됩니다. lambda expression에서는 identity와 stream의 값을 받을 수 있는 매개변수가 2개인 람다식을 작성하면 됩니다. someList.stream().reduce(, ) 사용 예시 int[] nums = {1, 2, 3, 4, 5, 6, 7} // nums 배열의 모든 값들의 곱을 구하는 방법 return Arrays.steram(nums).reduce(1, (total, num) -> (total..
서론 Spring boot 프로젝트에서 Swagger를 추가하는 방법을 소개합니다. 본론 spring boot 3.x인 경우에 해당합니다. 원문을 확인하시고 싶으시다면 여기를 확인하세요. 의존성 추가 gradle을 사용하는 경우 dependencies에 아래와 같이 의존성을 추가합니다. dependencies{ ... implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0' ... } maven을 사용하는 경우 dependency를 추가해줍니다. org.springdoc springdoc-openapi-starter-webmvc-ui 2.3.0 기본 설정 application.properties를 사용하는 경우 아래와..
서론 JPA를 사용하려고 할 때 dataSource 관련 문제가 생길때 사용할 수 있는 방법입니다. 본론 환경 spring boot: 3.2.2 테스트 코드 아래 코드와 같이 Autowired된 dataSource 하나만을 잘 구현해주면 되겠습니다. import lombok.extern.log4j.Log4j2; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import j..
서론 spring boot에서 log4j 사용을 위해 Lombok annotation을 사용하려고 하면 package lombok.extern.log4j does not exist에러가 발생 할 때 해결방법입니다. 본론 필자는 spring boot 3.2.2 에서 해당 문제가 발생하였고, 해결하였습니다. build.gradle 의존성에서 test관련 문구가 삽입되어 있는지 확인합니다. 아래 문구가 없으면 추가해줍니다. dependencies { ... testCompileOnly 'org.projectlombok:lombok' testAnnotationProcessor 'org.projectlombok:lombok' ... } log4j2 import lombok.extern..
서론 Collection에서 제공하는 API를 이용해서 내용물의 순서를 랜덤으로 설정하는 방법을 소개합니다. 본론 Collections 클래스의 shuffle 메서드를 사용하여 Collection의 내용물의 순서를 랜덤으로 바꿔줍니다. 기존 데이터가 변경되는 것이기 때문에 기존 순서를 유지해야 한다면 복제해서 사용해야 합니다. List students = Arrays.asList("Foo", "Bar", "Baz", "Qux"); Collections.shuffle(students); 출처: https://www.baeldung.com/java-shuffle-collection 결론 Random 클래스를 이용해서 순서를 섞는 방법을 사용하려고 했는데, 이미 구현된 메서드가 있어서 정말 편하네요. 너무 좋..
서론 웹 소켓을 사용한 랜덤 자리배치 시스템을 만들고 있었는데, 방을 만들어서 제공하는 기능이 필요해졌습니다. 그래서 사용자가 방 별로 구독하는 방법을 찾던 중 다음과 같은 방법을 찾아 소개합니다. 본론 @DestinationVariable Annotation that indicates a method parameter should be bound to a template variable in a destination template string. Supported on message handling methods such as @MessageMapping. 출처: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springfra..