본문 바로가기

Java8

Object List multiple field compare and remove class Product { private String code; private String name; private String category; private long price; } List products = Arrays.asList(); //코드,이름 카테고리 3개의 필드가 중복되는 상품 제거. Collection removes = products .stream() .collecti(toMap( p -> Arrays.asList(p.getCode(),p.getName(),p.getCategory()), Function.identity(),(p1,p2) -> p1)) .values(); 더보기
자바 - 시작_종료 시간 사이 체크 /** * 현재 시간이 시작,종료시간 사이에 있는지 체크 하도록 한다. * 현재 시간이 시작,종료 시간 사이일 경우 true 아닐 경우 false * @param start * @param end * @param now * @return */ private static boolean checkTimeBetween(LocalTime start, LocalTime end, LocalTime now) { if (start.isAfter(end)) { return !now.isBefore(start) || !now.isAfter(end); } else { return !now.isBefore(start) && !now.isAfter(end); } } 더보기
spring boot exception 처리를 위한 enum 클래스 활용 오류 발생시 json 형태의 커스텀한 객체를 던지고 싶다.예외처리시 오류 코드와 오류 메시지는 enum 클래스를 활용한다. 케이스별 예외에 대한 정의를 enum 으로 정의 한다. public enum ServiceError { BIND_ERROR(){ @Override public ResultError getResultError() { String message = getMessage(); return ResultError.builder() .httpStatus(HttpStatus.BAD_REQUEST) .resultCode("40000") .resultMessage(StringUtils.isEmpty(message) ? "Binding Error." : message) .build(); } }, EMA.. 더보기
java 8 remove duplicate object in List 자바 8으로 객체 리스트의 중복 데이터 제거하기 @Test public void testaa() { List list = Arrays.asList( new test("test1","email1","option1"), new test("test1","email1","option2"), new test("test1","email1","option3"), new test("test1","email1","option3") ); System.out.println(list.size()); List list2 = new ArrayList(); //note: getOption 으로 비교 list2.addAll(list.stream().collect(Collectors.toConcurrentMap(test::getOpt.. 더보기
Java 8 - Functional interface 사용방법 public class CommonFunc { // 내부 구현이 정해지지 않은 인터페이스 함수를 선언 @FunctionalInterface public interface intFunc { public int calc(int a, int b); } @FunctionalInterface public interface strFunc { public String strAppender(String s1, String s2); } }@Test public void test1(){ // 하나의 변수에 하나의 함수를 매핑 하는 람다식 // 인터페이스를 실제로 어떻게 사용할지를 아래 람다식으로 구현 하도록 한다. CommonFunc.intFunc add = (int a, int b) -> a+b; int result =.. 더보기
Java 8 ArrayList sort 아래와 같이 객체 클래스가 존재 한다고 했을때해당 객체를 담고 있는 리스트를 생성 했다고 하자.이때 리스트에서 해당 객체들을 시간순으로 (descending) 하려고 한다.java8 에서 제공하는 stream 을 사용하여 간단하게 정렬 하는 방법을 사용해봄@Datapublic class MyClass{private Date time;private message; }List resultList = new ArrayList();resultList.stream().sorted(Comparator.comparing(StatisticsDTO::getTime).reversed()).collect(Collectors.toList()); 더보기
Java8 stream - list compare 자바8 에서 제공하는 스트림을 사용하여 리스트에서 데이터 비교할 경우 쉽게 처리할 수 있는 방법 List AAAList = Array.asList();int findInt = 10; List findList = AAAList.stream().filter(A->findInt==A.getValue).collect(Collectors.toList()); AAA 타입의 리스트가 존재 할 경우 해당 리스트에서 finInt값을 가진 객체들을 추출하여 새로운 리스트를 만들 경우 사용. 더보기