[Java] stream API 와 Optional
·
Programming Language/Java
실무에서 자주 발생하는 상황에 stream API와 Optional 적용하기 백엔드 실무에서 자주 발생하는 일데이터에서 데이터를 조회한다.특정한 조건을 만족하는 데이터가 있는가?없다 -> 예외 발생!있다 -> 다음 작업 실행    for + if로 처리하기 vs stream API + Optional로 처리하기list 에서 1234 를 찾는 과정을 살펴보겠다.  for + if로 처리하기 public class ForAndIfFilterExampleMain { public static void main(String[] args) { int[] integerArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; List integerList = Arrays...
[Java] stream API를 예제와 함께 알아보자.
·
Programming Language/Java
stream APIpublic class ForIterationExampleMain { public static void main(String[] args) { List integerList = new ArrayList(); integerList.add(10); integerList.add(20); integerList.add(30); integerList.add(40); integerList.add(50); integerList.add(60); integerList.add(70); for (int i = 0; i -> stream API 를 사용하지 않을 때. for 의 iterator..
[Java] if문이 너무 많다면, 리팩토링을 해보자.
·
Programming Language/Java
너무 많은 if, else 는 읽기 어려운 코드가 된다if 문이 많은 코드에서 어떻게 if 문을 제거할지 한번 생각해보자.   public class CalculateCommand { private CalculateType calculateType; private int num1; private int num2; public CalculateCommand(CalculateType calculateType, int num1, int num2) { this.calculateType = calculateType; this.num1 = num1; this.num2 = num2; } public CalculateType getCalculateT..
[Java] Optional - nullPointException을 잘 다뤄보자.
·
Programming Language/Java
Optional 은 null 값을 잘 처리하기 위한 클래스입니다.   without Optional public class SimpleNpeExampleMain { public static void main(String[] args) { String string = getNullString(); System.out.println("string=" + string); System.out.println(string.toUpperCase()); } private static String getNullString() { return null; }} 근데 이렇게 당연하게 null 이 터지는 경우는 실제 상황에서 없을 것이다.좀 더 현실적인 경..
[Java] Object 클래스와 주요 메서드 - equals(), hashCode(), toString()
·
Programming Language/Java
Object 클래스란?모든 클래스는 Object 클래스의 자손 클래스이다.public class SomeObject {}public class SombeObject extends Object {} 따라서, extends Object 가 생략되어 있는거라고 생각하면 된다.    Object 클래스의 주요 메서드들 clone(), equals(), finalize(), getClass(), hashCode(), notify(), notifyAll(), toString(), wait() 이 중에서도 특히 자주 사용하는 세 가지 메서드를 알아보려한다-> equals(), hashCode(), toString()    equals()동일성 : 비교 대상이 실제로 '똑같은' 대상이어야 함 ( = 둘은 실제로는 하나..