11.1 ๊ฐ’์ด ์—†๋Š” ์ƒํ™ฉ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊นŒ?

public class Person {
	private Car car;
    public Car getCar(){ return car;}
}

public class Car {
	private Insurance insurance;
    public Insurance getInsurance(){ return insurance;}
}

public class Insurance{
	private String name;
    public String getName(){ return name;}
}

์œ„์™€ ๊ฐ™์ด ์ž๋™์ฐจ์™€ ์ž๋™์ฐจ ๋ณดํ—˜์„ ๊ฐ–๊ณ  ์žˆ๋Š” ์‚ฌ๋žŒ ๊ฐ์ฒด๋ฅผ ์ค‘์ฒฉ ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๊ณ  ํ•˜์ž.

 

public String getCarInsurance(Person person){
	return person.getCar().getInsurance().getName();
}
  • ์ฐจ๋ฅผ ์†Œ์œ ํ•˜์ง€ ์•Š์€ ์‚ฌ๋žŒ์ด ์žˆ์ง€๋งŒ getCar์„ ํ˜ธ์ถœํ•˜๋ฉด
  • person์ด null์ด๋ผ๋ฉด
  • getInsurance๊ฐ€ null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด

 

11.1.1 ๋ณด์ˆ˜์ ์ธ ์ž์„ธ๋กœ NullPointerException ์ค„์ด๊ธฐ

NullPointerException์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ..

null ํ™•์ธ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€

if (person != null) {
	Car car = person.getCar();
    if (car != null) {
    	...

๋ฐ˜๋ณต ํŒจํ„ด ์ฝ”๋“œ๋ฅผ ๊นŠ์€ ์˜์‹ฌ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ฝ”๋“œ์˜ ๊ตฌ์กฐ๊ฐ€ ์—‰๋ง์ด๋˜๊ณ  ๊ฐ€๋…์„ฑ๋„ ๋–จ์–ด์ง„๋‹ค.

 

 

if (person == null) {
	return "Unknown"
}
Car car = person.getCar();
if (car == null){
	...

์œ„ ์ฒ˜๋Ÿผ ์กฐ๊ธˆ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ if ๋ธ”๋ก์„ ์—†์• ๊ณ  null ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด ์ฆ‰์‹œ "Unknown"์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ถœ๊ตฌ๊ฐ€ ๋งŽ์•„์ ธ ์œ ์ง€๋ณด์ˆ˜์— ์–ด๋ ค์›€์ด ์žˆ๋‹ค.

 

11.1.2 null ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ

  • ์—๋Ÿฌ์˜ ๊ทผ์›์ด๋‹ค.
  • ์ฝ”๋“œ๋ฅผ ์–ด์ง€๋ŸฝํžŒ๋‹ค.
  • ์•„๋ฌด ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.
  • ์ž๋ฐ”์˜ ์ฒ ํ•™์— ์œ„๋ฐฐ๋œ๋‹ค - ๋ชจ๋“  ํฌ์ธํ„ฐ๋ฅผ ์ˆจ๊ฒผ์ง€๋งŒ nullํฌ์ธํ„ฐ๋Š” ์˜ˆ์™ธ๋‹ค.
  • ํ˜•์‹ ์‹œ์Šคํ…œ์— ๊ตฌ๋ฉ์„ ๋งŒ๋“ ๋‹ค. - null์ด ์–ด๋–ค ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋˜์—ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.

 

 

11.2 Optional ํด๋ž˜์Šค ์†Œ๊ฐœ

java.util.Optional<T> ๋ผ๋Š” ์ƒˆ๋กœ์šด ํด๋ž˜์Šค ์ œ๊ณตํ•œ๋‹ค.

Optional์€ ์„ ํƒํ˜• ๊ฐ’์„ ์บก์Šํ™”ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.

 

๊ฐ’์ด ์žˆ์œผ๋ฉด Optional ํด๋ž˜์Šค๋Š” ๊ฐ’์„ ๊ฐ์‹ผ๋‹ค. 

๋ฐ˜๋ฉด ๊ฐ’์ด ์—†์œผ๋ฉด Optional.empty ๋ฉ”์„œ๋“œ๋กœ Optional์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Optional.empty๋Š” Optional์˜ ํŠน๋ณ„ํ•œ ์‹ฑ๊ธ€ํ„ด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋‹ค.

 

public class Person {
	// ์‚ฌ๋žŒ์ด ์ฐจ๋ฅผ ์†Œ์œ ํ–‡์„ ์ˆ˜๋„, ์†Œ์œ ํ•˜์ง€ ์•Š์•˜์„ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ Optional์ •์˜
	private Optional<Car> car;
    public Optional<Car> getCar(){ return car;}
}

public class Car {
	// ์ž๋™์ฐจ๊ฐ€ ๋ณดํ—˜์— ๊ฐ€์ž…๋˜์–ด ์žˆ์„ ์ˆ˜๋„ ์•„๋‹ ์ˆ˜ ๋„ ์žˆ์œผ๋ฏ€๋กœ
	private Optional<Insurance> insurance;
    public Optional<Insurance> getInsurance(){ return insurance;}
}

public class Insurance{
	// ๋ณดํ—˜ํšŒ์‚ฌ์—๋Š” ๋ฐ˜๋“œ์‹œ ์ด๋ฆ„์ด ์žˆ๋‹ค.
	private String name;
    public String getName(){ return name;}
}

 

 

11.3 Optional ์ ์šฉ ํŒจํ„ด

 

11.3.1 Optional ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ

 

  • ๋นˆ Optional

Optional<Car> optCar = Optional.empty();

 

  • null์ด ์•„๋‹Œ ๊ฐ’์œผ๋กœ Optional ๋งŒ๋“ค๊ธฐ

Optional.of๋กœ null์ด ์•„๋‹Œ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” Optional์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

Optional<Car> optCar = Optional.of(car);

car๊ฐ€ null์ด๋ผ๋ฉด ์ฆ‰์‹œ NullPointerException ๋ฐœ์ƒ

 

  • null๊ฐ’์œผ๋กœ Optional ๋งŒ๋“ค๊ธฐ

Optional<Car> optCar = Optional.ofNullable(car);

car์ด null์ด๋ฉด ๋นˆ Optional ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

 

 

11.3.2 ๋งต์œผ๋กœ Optional์˜ ๊ฐ’์„ ์ถ”์ถœํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๊ธฐ

์ด๋ฆ„ ์ •๋ณด์— ์ ‘๊ทผํ•˜๊ธฐ ์ „์— insurance๊ฐ€ null์ธ์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

Optional์€ map ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์›ํ•œ๋‹ค.

 

Optional<Insurance> optInsurance = Optional,.ofNullable(insurance);
Optional<String> name = optInsurance.map(Insurance::getName);

 

+ Recent posts