BootCamp 29day

GyeongNamΒ·2023λ…„ 12μ›” 25일
0

BootCamp

λͺ©λ‘ 보기
27/49
post-thumbnail

πŸ“… 2023λ…„ 12μ›” 22일

[java 14일차]


29일차: java 심화(7)

streamAPI

데이터와 객체 쀑심이 μ•„λ‹Œ, μž…λ ₯에 λ”°λ₯Έ 좜λ ₯만이 μ‘΄μž¬ν•˜λŠ” ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° 방식

// stream 쀑계연산 : filter, map, sorted, distinct
Arrays.stream(newIntArr).distinct().sum();
Arrays.stream(stArr).distinct().filter(a -> a.length() <= 3).mapToInt(String::length).sum();
Arrays.stream(arrn).sorted().toArray();

// 슀트림 μ†Œλͺ¨ : forEach, reduce
Arrays.stream(arr).forEach(System.out::println);
Arrays.stream(arr).reduce(1,(a,b)->a*b);

// Optional 객체 : 값이 μžˆμ„μˆ˜λ„ 있고 ,없을 μˆ˜λ„ μžˆλ‹€λŠ” 것을 λͺ…μ‹œν•œ νƒ€μž… (java8 이후)
Optional<String> o_answer = Arrays.stream(starr).reduce((x, y)->x+", "+y);
if(o_answer.isPresent()){
	System.out.println(o_answer.get());
}

List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));
System.out.println(list.stream().mapToInt(x->x).min().getAsInt());
System.out.println(list.stream().mapToInt(x->x).max().getAsInt());
System.out.println(list.stream().mapToDouble(x->x).average().getAsDouble());
System.out.println(list.stream().mapToInt(x->x).sum());
System.out.println(list.stream().count());

// Optional 객체에 빈 값을 λͺ…μ‹œμ μœΌλ‘œ λ„£λŠ” 방법 : Optional.empty();
Optional<String> opt1 = Optional.empty();
if(opt1.isPresent()){
    System.out.println(opt1.get().compareTo("abc"));
}else{
	System.out.println("값이 μ—†μŠ΅λ‹ˆλ‹€.");
}

orElse() : 값이 있으면 ν•΄λ‹Ήκ°’ return, μ—†μœΌλ©΄ default 지정값 return
orElseGet() : 값이 있으면 ν•΄λ‹Ήκ°’ return, μ—†μœΌλ©΄ 맀개 λ³€μˆ˜λ‘œ λžŒλ‹€ν•¨μˆ˜ λ˜λŠ” λ©”μ†Œλ“œ μ°Έμ‘°
orElseThrow() : 값이 있으면 ν•΄λ‹Ήκ°’ return, μ—†μœΌλ©΄ μ§€μ •λœ μ˜ˆμ™Έ κ°•μ œ λ°œμƒ

기본 클래슀

Math

// Math.abs() : μ ˆλŒ€κ°’ λ°˜ν™˜
System.out.println(Math.abs(-5));
// floor() : μ†Œμˆ˜μ  λ‚΄λ¦Ό
System.out.println(Math.floor(5.7));
// ceil() : μ†Œμˆ˜μ  올림
System.out.println(Math.ceil(5.7));
// round() : μ†Œμˆ˜μ  반올림
System.out.println(Math.round(5.7));
// max, min
System.out.println(Math.max(5,8));
System.out.println(Math.min(5,8));
// pow(a,b) : 제곱 μ—°μ‚° μˆ˜ν–‰ a의 b승
System.out.println(Math.pow(2,5));
// sqrt() : 제곱근 μ—°μ‚° μˆ˜ν–‰
System.out.println(Math.sqrt(25));

Calendar

Calendar cal = Calendar.getInstance();
// νŠΉμ •ν•œ μˆ«κ°’μ€ get ν•¨μˆ˜μ˜ input κ°’μœΌλ‘œ μ£Όμ–΄ μ›ν•˜λŠ” λ‚ μ§œ 정보λ₯Ό 좜λ ₯ν•  수 있게 ν•΄μ€€λ‹€.
System.out.println(cal.getTime());

// java.time νŒ¨ν‚€μ§€ : Local~ 클래슀
LocalTime localTime = LocalTime.now();
System.out.println(localTime);

LocalDate localDate = LocalDate.now();
System.out.println(localDate);

github java μ‹€μŠ΅ λ‚΄μš©

profile
503 Service Unavailable Error

0개의 λŒ“κΈ€