[JAVA8] 중복제거 하기

import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; public class Distinct { public static void main(String[] args) { System.out.println("defaultDistinctWithOnlyString"); List names = Arrays.asList("aaa", "bbb", "ccc", "ddd", "aaa", "ccc", "aaa", "aaa"); List distinctList = names.stream().distinct().collect(Collectors.toList()); distinctList.forEach(System.out::println); // result : aaa , bbb, ccc , ddd // 다음의 경우에는 기대와는 다른 결과가 나올 수 있다. Person p1 = new Person(); Person p2 = new Person(); Person p3 = new Person(); Person p4 = new Person(); Person p5 = new Person(); p1.setName("aaa"); p1.setAge(20); p1.setHeight(175); p2.setName("bbb"); p2....

[JAVA8] 람다식을 이용하여 Sorting 하기

public class SortVO { // 숫자 비교를 하기위해 int 가 아닌 Integer로 선언 Integer no; String name; public Integer getNo(){ return no; } public void setNo( Integer no ){ this.no = no; } public String getName(){ return name; } public void setName( String name ){ this.name = name; } } public class LambdaSortingTest { public static void main( String[] args ){ SortVO st1 = new SortVO(); SortVO st2 = new SortVO(); SortVO st3 = new SortVO(); SortVO st4 = new SortVO(); SortVO st5 = new SortVO(); SortVO st6 = new SortVO(); st1.setNo(111); st2.setNo(212); st3.setNo(3232); st4.setNo(443); st5.setNo(5); st6.setNo(622); List list = new ArrayList (); list.add(st1); list.add(st2); list.add(st3); list.add(st4); list.add(st5); list.add(st6); System.out.println(" >"); list = list.stream().sorted(( e1, e2 ) -> e1.getNo().compareTo(e2.getNo())).collect(Collectors.toList()); list.forEach(e -> System.o...

[Tomcat] SE[VERE: IOException while loading persisted sessions: java.io.EOFException

이미지
젠킨스를 이용하여 자동빌드를 하던 도중 갑자기 connection refuse 에러가 발생. 카탈리나 로그를 확인해 보니 아래와 같은 에러가 발생하며 배포가 되지 않았다. SEVERE: IOException while loading persisted sessions: java.io.EOFException Tomcat 디렉토리를 clean 해주지 못하는 경우 발생. 문제가 발생한 app 경로를 찾아가 SESSION.ser 파일을 삭제해주면 깔끔하게 해결된다. ${catalina.home}/work/Catalina/localhost/<app>/SESSION.ser    나의 경우는  ${catalina.home}/work/Catalina/localhost/manager에 있는 SESSION.ser 파일을  삭제해 주어 해결하였다.  refs :  http://stackoverflow.com/questions/12348170/what-is-wrong-with-my-config-severe-ioexception-while-loading-persisted-sessio

[JAVA8] 람다식을 이용한 Batch Insert 구현하기

Java 8 에서 람다식을 이용한 Batch Insert 구현하기   조회한 데이터의 사이즈가 커서 일정 단위로 잘라 넣어야 할 일이 생겼다. 람다식을 이용한 메서드를 만들어 사용하였으며, 간결하다고 생각되어 해당 메서드를  공유해 본다. public static <T>Stream <List<T>>   batches (List<T> source, int length) {   if (length <= 0) throw new IllegalArgumentException("length = " + length);   int size = source.size();   if (size <= 0) return Stream.empty();   int fullChunks = (size - 1) / length;   return IntStream.range(0, fullChunks + 1).mapToObj(           n -> source.subList(n * length, n == fullChunks ? size : (n + 1) * length)); }  (코드 스타일이 적용이 되지 않아 실제 구현된 메서드는 텍스트로 표현) // 사용 예) public static void main(String[] args) { List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); System.out.println("By 3:"); batches(list, 3).forEach(System.out::println); System.out.println("By 4:"); batches(list, 4).forEach(System.out::println); } ...

[JAVA8] HashMap에 getOrDefault 와 putIfAbsent 의 차이

getOrDefault : key 값이 없다면 입력시 설정한 default 값을 반환 putIfAbsent : key 값이 없다면 입력 된 key와 value 를 입력, 해당 key가 존재하면 입력 되었던 값 반환 HashMap hm = new HashMap (); // 1. 비어있는 HashMap에서 get 호출 System.out.println("key가 없을 때 : " + hm.get("key")); // 결과 : key가 없을 때 : null // 2. 비어있는 HashMap에서 getOrDefault 호출 System.out.println("getOrDefault() : " + hm.getOrDefault("key", "car")); // 결과 : getOrDefault : car // 3. key, value 입력 System.out.println("putIfAbsent() : " + hm.putIfAbsent("key", "ship")); // 결과 : putIfAbsent : null // 이전값이 존재하지 않기 때문에 null 반환 // 4. putIfAbsent으로 key, value 입력 후 동일한 key 값 재호출 System.out.println("putIfAbsent() : " + hm.putIfAbsent("key", "bike")); // 결과 : putIfAbsent : ship System.out.println("values in map : "+hm.toString()); // 결과 : {key=ship} // getOrDefault은 해당 key가 없을 경우 값을 set은 하지 않고 정해진 default 값만 리턴 하기 떄문에 car 없음 // putIfAbsent는 이...

[HTTPS] Hostname in cerificate didn't match?

특정 업체의 API를 호출하던 중 아래와 같은 에러가 발생 Hostname in certificate didn't match? SSLPeerUnverifiedException   위의 에러발생 이유는 해당 업체의 인증서가 나에게 등록 되어있지 않기 때문이다. 해당 업체의 인증서를 등록하여 해결하는 방법도 있지만, 나는 데이터가 원하는데로 전달되는지만 확인하면 되었기에 인증서를 무시하고 요청하는 방식을 사용하였다. pom.xml 에 httpclient 추가 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId> httpclient </artifactId> <version>4.5.1</version> <type>jar</type> <scope>compile</scope> </dependency> 인증서 무시를 위해 사용한 코드 @Test public void shouldAcceptUnsafeCerts() throws Exception{ HttpClient httpclient = httpClientTrustingAllSSLCerts(); final String uri = "YOUR URI"; HttpGet httpGet = new HttpGet(uri); HttpResponse response = httpclient.execute(httpGet); System.out.println(EntityUtils.toString(response.getEntity())); } private HttpClient httpClientTrustingAllSSLCerts() throws HttpClientBuilder builder = HttpClie...

[Android] Android 로깅 시 isLoggable() 메서드 사용

아래와 같이 isLoggable 이라는 메서드를 사용하여 원하는 로그를 찍고 싶을 경우 system property 값을 원하는 로그 레벨로 변경해 주거나 local.prop 파일을 생성하여 /data/local.prop 으로 위치시켜야 한다. public static void LOGD(final String tag, String message, Throwable cause) {         if (LOGGING_ENABLED){             if (Log. isLoggable (tag, Log.DEBUG)) {                 Log.d(tag, message, cause);             }         }   } system property 값을 변경해 주는 방법은 다음과 같다. adb shell setprop log.tag.<tag> <log level> myLog 라는 tag 를 debug 레벨에서 찍고 싶다면, console 창에서 아래와 같이 써주면 된다. adb shell setprop lgo.tag. myLog DEBUG * 공식사이트에 나와있는 isLoggable 메서드 관련 설명 public static boolean  isLoggable   ( String  tag, int level) Added in  API level 1 Checks to see whether or not a log for the specified tag is loggable at the specified level. The default level of any tag ...