πͺπ» μ€νΈλ¦Ό Stream
- λ°μ΄ν°μ νλ¦
- λ°°μ΄κ³Ό 컬λ μ μ ν¨μνμΌλ‘ μ²λ¦¬
- λ°°μ΄ λλ 컬λ μ μΈμ€ν΄μ€μ ν¨μ μ¬λ¬ κ°λ₯Ό μ‘°ν©ν΄μ μνλ κ²°κ³Όλ₯Ό νν°λ§νκ³ κ°κ³΅λ κ²°κ³Όλ₯Ό μ»μ μ μμ
- μ½λμ μμ μ€μ΄κ³ κ°κ²°νκ² ννν μ μμ = μ μ₯κ³΅κ° νμ μμ
- λ°°μ΄, 컬λ μ μ μ μ₯ μμλ₯Ό νλμ© μ°Έμ‘°ν΄μ λλ€μμΌλ‘ μ²λ¦¬ν μ μλλ‘ ν΄μ£Όλ λ°λ³΅μ
- λ€μν λ°μ΄ν° μμ€λ₯Ό λ€λ£¨λ νλΆν λ©μλ μ 곡
π μ€νΈλ¦Ό νΉμ§
1οΈβ£ μ μΈν
- μ μΈν νλ‘κ·Έλλ° ? μ΄λ»κ² 보λ€λ 무μμ μννλμ§ κ΄μ¬μ λλ νλ‘κ·Έλλ°
- μ μΈν λ°©μμΌλ‘ μμ±νλ©΄ λ΄λΆ λμ μ리λ₯Ό λͺ°λΌλ μ½λκ° λ¬΄μ¨ μΌ νλμ§ μ΄ν΄ κ°λ₯
2οΈβ£ λλ€μμΌλ‘ μμ μ²λ¦¬ μ½λλ₯Ό μ 곡
- μ€νΈλ¦Όμ΄ μ 곡νλ λλΆλΆμ μμ μ²λ¦¬ λ©μλλ ν¨μμ μΈν°νμ΄μ€ λ§€κ°νμ μ κ°μ§
- λ§€κ°κ°μΌλ‘ λλ€μ λλ λ©μλ μ°Έμ‘°λ₯Ό λμ ν μ μμ
stream.forEach( x -> {
String id = x.getId();
int pw = x.getPw();
System.out.println("idλ " + id + "pwλ " + pw);
});
3οΈβ£ λ΄λΆ λ°λ³΅μλ₯Ό μ¬μ©νμ¬ λ³λ ¬ μ²λ¦¬κ° μ¬μ
π‘ μΈλΆ λ°λ³΅μ ? κ°λ°μκ° μ½λλ‘ μ§μ 컬λ μ μ μμλ₯Ό λ°λ³΅ν΄μ κ°μ Έμ€λ ν¨ν΄
→ forλ¬Έ, while λ¬Έ λ±
π‘ λ΄λΆ λ°λ³΅μ ? 컬λ μ λ΄λΆμμ μμλ€μ λ°λ³΅μν€κ³ κ°λ°μλ μ²λ¦¬ν΄μΌ ν μ½λλ§ μ 곡νλ ν¨ν΄
→ κ°λ°μλ μμ μ²λ¦¬ μ½λμλ§ μ§μ€ (컬λ μ μ΄ μμ λ°λ³΅ λ΄λΉ)
→ μμλ€μ λΆλ°°μμΌ λ³λ ¬ μ²λ¦¬ μμ κ°λ₯
π‘ λ³λ ¬ μ²λ¦¬ ? ν κ°μ§ μμ μ μλΈ μμ μΌλ‘ λλμ΄ λΆλ¦¬λ μ€λ λμμ λ³λ ¬μ μΌλ‘ μ²λ¦¬
4οΈβ£ μ€κ° μ°μ°κ³Ό μ΅μ’ μ°μ° κ°λ₯
- μ€κ° μ°μ° : λ§€ν, 그루ν, νν°λ§, μ λ ¬
- μ΅μ’ μ°μ° : λ°λ³΅, μΉ΄μ΄ν , νκ· , μ΄ν©, μ΅λ/μ΅μκ°
double sum = list.stream()
.mapToInt(Age:: getAge)
.sum()
.getAsDouble();
System.out.println("ν©κ³: " + sum);
π νμ΄νλΌμΈ κ΅¬μ± (.)
- νμ΄νλΌμΈ ? μ¬λ¬ κ°μ μ€νΈλ¦Όμ΄ μ°κ²°λμ΄ μλ ꡬ쑰
- νμ΄νλΌμΈμμ μ΅μ’ μ°μ°μ μ μΈνκ³ λ λͺ¨λ μ€κ° μ°μ° μ€νΈλ¦Ό !
- μ΅μ’ μ°μ°μ΄ μ€νλμ΄μΌ μ€κ° μ€νΈλ¦Όμμ μ°μ° μμ
ππ» μ€νΈλ¦Ό μμ± λ° μ°μ° κ³Όμ
π μ€νΈλ¦Ό μμ±
Stream<String> Streamλͺ
= list.stream(); (1) 리μ€νΈ μ€νΈλ¦Ό
Stream<String> Streamλͺ
= Arrys.stream(~); (2) λ°°μ΄ μ€νΈλ¦Ό
Stream<String> Streamλͺ
= Stream.of(~); (3) κ°λ³ λ°°μ΄ μ€νΈλ¦Ό
- stream( )μ μ¬μ©νλ©΄ ν΄λΉ 컬λ μ κ°μ²΄λ₯Ό StreamμΌλ‘ λ°ν
- 컬λ μ μΈν°νμ΄μ€μλ stream()μ΄ μ΄λ―Έ μ μλμ΄μμ
1οΈβ£ 컬λ μ μ€νΈλ¦Ό
package Stream;
import java.util.*;
import java.util.stream.Stream;
public class liststream {
public static void main(String[] args) {
List<String> ls = Arrays.asList("One", "Two", "Three");
Stream<String> lsStream = ls.stream();
lsStream.forEach(System.out::println);
}
}
- 컬λ μ νμ (Collection, List, Set)μ κ²½μ° stream λ©μλλ₯Ό μ΄μ©ν΄μ λ§λ€ μ μμ
2οΈβ£ λ°°μ΄ μ€νΈλ¦Ό
package Stream;
import java.util.*;
import java.util.stream.Stream;
public class ArrayStream {
public static void main(String[] args) {
String[] arr = new String[]{"νλ","λ","μ
"};
Stream<String> arrStream = Arrays.stream(arr);
arrStream.forEach(x -> System.out.print(x + " "));
System.out.println();
Stream<String> arrSteam2 = Arrays.stream(arr,1,2);
arrSteam2.forEach(y -> System.out.print(y + " "));
System.out.println();
Stream<Double> st = Stream.of(4.3,2.4,2.9);
st.forEach(System.out::println);
}
}
//κ²°κ³Ό
νλ λ μ
λ
4.3
2.4
2.9
π‘ Array.stream
- Array.stream(arr)μ μ¬μ©ν μ λ μκ³ λΆλΆ λ¬Έμ μΆλ ₯μ μνλ©΄ Arrays.strema(arr,1,2)μ²λΌ μ¬μ©νλ©΄ λ¨
π‘ forEach( )
- ν΄λΉ μ€νΈλ¦Όμ μμλ₯Ό νλμ© μλͺ¨ν΄κ°λ©° μμ°¨μ μΌλ‘ μμμ μ κ·Όνλ λ©μλ
π‘ stream.of
- Stream ν΄λμ€μ of λ©μλλ κ°λ³ λ§€κ°λ³μλ₯Ό μ λ¬λ°μ μ€νΈλ¦Όμ μμ±ν μ μμ
βͺοΈ μ»¬λ μ μΌλ‘λΆν° μ€νΈλ¦Ό μμ±
List<Integer> μ°Έμ‘°λ³μ = Arrays.asList(1,2,3,4,5);
Stream<Integer> st = ls.stream();
βͺοΈ λ°°μ΄λ‘λΆν° μ€νΈλ¦Ό μμ±
Stream<String> stream = Arrays.stream(T[])
Stream<String> stream = Arrays.stream(int[])
Stream<String> stream = Arrays.stream(long[])
Stream<String> stream = Arrays.stream(double[])
Stream<String> stream = Stream.of(T[])
Stream<String> stream = IntStream.of(int[])
Stream<String> stream = LongStream.of(long[])
Stream<String> stream = DoubleStream.of(double[])
βͺοΈ νΉμ λ²μμ μ μλ₯Ό μμλ‘ κ°λ μ€νΈλ¦Ό μμ±
IntStream intstream = IntStream.range(1,5)
IntStream intstream = IntStream.rangeClosed(1,5)
LongStream longstream = LongStream.range(1,5)
LongStream longstream = LongStream.rangeClosed(1,5)
ππ» μ€κ° μ°μ°
- μ€κ°μ°μ°μ μ°μ° κ²°κ³Όλ₯Ό μ€νΈλ¦ΌμΌλ‘ λ°ννκΈ° λλ¬Έμ μ°μν΄μ μ¬λ¬ λ² μνν μ μμ
π νν°λ§
π distinct( )
: μ€νΈλ¦Όμ μμλ€μ μ€λ³΅λ λ°μ΄ν° μ κ±°
π filter( )
: 쑰건μ λ§λ λ°μ΄ν°λ§ μ μ νμ¬ μμ 컬λ μ μ λ§λ¦
List<String> sick = Arrays.asList("λν΅","κ·Όμ‘ν΅","κ³ μ΄","볡ν΅","κ·Όμ‘ν΅");
sick.stream()
.distinct()
.forEach(n -> System.out.println(n));
sick.stream()
.filter( x -> x.startsWith("λ"))
.forEach(x-> System.out.println(x));
π‘ startsWith(체ν¬ν λ¬Έμμ΄)
- 체ν¬ν λ¬Έμμ΄λ‘ μμνλμ§ νμΈ
- a.startsWith("JAVA")μΌ λ, λ¬Έμμ΄μ΄ JAVAλ‘ μμνλ€λ©΄ True !
π‘ endsWith(체ν¬ν λ¬Έμμ΄)
- 체ν¬ν λ¬Έμμ΄λ‘ λλλμ§ νμΈ
- b.endsWith("Programming")μΌ λ λ λ¬Έμμ΄κ³Ό μΌμΉνλ€λ©΄ True !
π λ§€ν
π map( )
: μλ‘μ΄ μ€νΈλ¦Όμ νμ±νλ κ²μΌλ‘ κΈ°μ‘΄μ μ€νΈλ¦Ό μμλ€μ λ체νλ μμ
- μΌλ°μ μΈ Stream κ°μ²΄λ₯Ό μμ StreamμΌλ‘ λ°κΎΈκ±°λ κ·Έ λ°λ μμ μ΄ νμν κ²½μ°μ μ¬μ©
- μ€νΈλ¦Όμ μ€νΈλ¦Όμ λ°ν !!!! return : Stream <Stream>
→ map μ΄μΈμλ mapToInt(), mapToLong, mapToDouble
List<String> coffee = Arrays.asList("americano", "caffe latte" ,"cappuccino");
coffee.stream()
.map(x -> x.toLowerCase())
.forEach(x->System.out.println(x));
π‘ flatMap( )
- μμλ₯Ό λ체νλ 볡μ κ°μ μμλ€λ‘ ꡬμ±λ μλ‘μ΄ μ€νΈλ¦Ό 리ν΄
- μ΄κΈ° μμ±λ μ€νΈλ¦Ό ννκ° λ°°μ΄μΈ κ²½μ°, λͺ¨λ μμλ₯Ό λ¨μΌ μ€νΈλ¦ΌμΌλ‘ λ°ν ν μ μμ
- μ€νΈλ¦Ό λ°ν !!!! return : Stream
π μ λ ¬
π sorted( )
: μ€νΈλ¦Όμ μμλ€μ μ λ ¬
- μ€λ¦μ°¨μ : sorted( )
- λ΄λ¦Όμ°¨μ : sorted(Comparator.reverseOrder( ))
List<String> person = Arrays.asList("Hand","Foot","Body","Face");
person.stream()
.sorted(Comparator.reverseOrder())
.forEach(x->System.out.println(x));
π μ°μ° κ²°κ³Ό νμΈ
π peek( )
: μ€κ° μ°μ° λ©μλλ‘, μμλ₯Ό νλμ© λλ©΄μ μΆλ ₯
- νλμ μ€νΈλ¦Όμ μ¬λ¬ λ² μ¬μ©ν μ μμ
- μ£Όλ‘ μ°μ° μ€κ°μ κ²°κ³Όλ₯Ό νμΈνμ¬ λλ²κΉ νκ³ μ ν λ μ¬μ©
* λλ²κΉ ? μ€λ₯λ λ²κ·Έλ₯Ό μ°Ύμλ΄κ³ μμ νλ μμ
π forEach( )
: μ΅μ’ μ°μ° λ©μλλ‘, μμλ₯Ό νλμ© λλ©΄μ μΆλ ₯
- ν λ²λ§ νΈμΆκ°λ₯ (μ¬νΈμΆνλ €λ©΄ μλ‘μ΄ μ€νΈλ¦Ό μμ±ν΄μΌ ν¨)
ππ» μ΅μ’ μ°μ°
- μ°μ° κ²°κ³Όλ₯Ό ν λ²λ§ μ€ν κ°λ₯
π μ°μ° κ²°κ³Ό νμΈ
π forEach( )
: νμ΄νλΌμΈ λ§μ§λ§μμ μμλ₯Ό νλμ© μ°μ°
- λ¦¬ν΄ κ°μ΄ μλ μ΄λ©μΌ λ°μ‘, μ€μΌμ€λ§ μμ μμλ μ¬μ©λ¨
π λ§€μΉ
π match( )
: μ€νΈλ¦Όμ μμλ€μ΄ νΉμ ν 쑰건μ μΆ©μ‘±νλμ§ κ²μ¬νκ³ μΆμ κ²½μ° μ¬μ©
- ν¨μν μΈν°νμ΄μ€λ₯Ό λ°μ ν΄λΉ 쑰건μ λ§μ‘±νλμ§ κ²μ¬νκ³ true/falseλ‘ λ°ν
1οΈβ£ allMatch( )
: λͺ¨λ μμλ€μ΄ λ§€κ°κ°μΌλ‘ μ£Όμ΄μ§ 쑰건μ λ§μ‘±νλμ§ μ‘°μ¬
2οΈβ£ anyMatch( )
: μ΅μν ν κ°μ μμκ° λ§€κ°κ°μΌλ‘ μ£Όμ΄μ§ 쑰건μ λ§μ‘±νλμ§ μ‘°μ¬
3οΈβ£ noneMatch( )
: λͺ¨λ μμλ€μ΄ λ§€κ°κ°μΌλ‘ μ£Όμ΄μ§ 쑰건μ λ§μ‘±νμ§ μλμ§ μ‘°μ¬
public class matchTest {
public static void main(String[] args) {
int[] num = { 1, 3, 5};
boolean result = Arrays.stream(num).allMatch(x->x%2==1);
System.out.println("νμ μΈκ°μ ? " + result);
result = Arrays.stream(num).anyMatch(x->x%3==0);
System.out.println("3μ λ°°μκ° μλμ¬? "+result);
result = Arrays.stream(num).noneMatch(x-> x%3==0);
System.out.println("3μ λ°°μκ° μλμ? " + result);
}
}
π κΈ°λ³Έ μ§κ³
π sum( ), count( ), average( ), max( ), min( )
: μμλ€μ ν©κ³, μΉ΄μ΄ν , νκ· , μ΅λ, μ΅μ κ° λ±μΌλ‘ μ°μ°νμ¬ νλμ κ°μΌλ‘ μ°μΆ
int[] arr = { 1, 2, 3, 4, 5};
long count = Arrays.stream(arr).count();
long sum = Arrays.stream(arr).sum();
double avg = Arrays.stream(arr).average().getAsDouble();
int max = Arrays.stream(arr).max().getAsInt();
π reduce
: λμ νμ¬ νλλ‘ μμΆνλ λ°©μμΌλ‘ λ€μν μ§κ³ κ²°κ³Όλ¬Όμ λ§λ€ μ μμ
.reduce(0, (a,b) -> a + b )
(1) Accumulator : κ° μμλ₯Ό κ³μ°ν μ€κ° κ²°κ³Όλ₯Ό μμ±νκΈ° μν΄ μ¬μ©
(2) Identity : κ³μ°μ μννκΈ° μλ μ΄κΈ°κ°
(3) Combiner : λ³λ ¬ μ€νΈλ¦Όμμ λλμ΄ κ³μ°λ κ²°κ³Όλ₯Ό νλλ‘ ν©μΉκΈ° μν λ‘μ§
π Collect( )
: μ€νΈλ¦Όμ μμλ€μ λ€λ₯Έ μ’ λ₯μ κ²°κ³Όλ‘ μλ‘κ² μμ§νκ³ μΆμ κ²½μ° μ¬μ©
- Collector λ§€κ° κ°μ μ΄λ€ μμλ₯Ό μ΄λ€ 컬λ μ μ μμ§ν κ²μΈμ§λ₯Ό λνλ
.collect(Collectors.toList())
.collect(Collectors.toSet())
.collect(Collectors.toMap())
.collect(Collectors.toCollection(~))
π€π» Optional <T>
public final class Optional<T> {
private final T vale;
}
: Nullμ΄ μ¬ μ μλ κ°μ κ°μΈλ wrapper ν΄λμ€
- NPE(NullPointerException) : null κ°μΌλ‘ μΈν΄ μλ¬κ° λ°μνλ νμ
- Optionalμ NPEκ° λ°μνμ§ μλλ‘ μ°μ°κ²°κ³Όλ₯Ό κ°μ§κ³ μλ€κ° λ°ν
ππ» λ©μλκ° λ°νν κ²°κ³Ό κ°μ΄ μμμ λͺ νν ννν νμκ° μκ³
nullμ λ°ννλ©΄ μλ¬κ° λ°μν κ°λ₯μ±μ΄ λμ μν©μμ Optional μ¬μ©νλ κ²μ΄ λͺ©μ
1οΈβ£ orElse() / orElseGet() orElseThrow() μ¬μ©
: κ°μ΄ null μΌ λ μ΄λ€μ νμ©νμ¬ λν΄νΈ κ° μ§μ κ°λ₯
.orElse(getId())
.orElseGet(this::getId)
- orElse() : νλΌλ―Έν°λ‘ κ°μ λ°μ & κ°μ΄ 미리 μ‘΄μ¬νλ κ²½μ° μ¬μ©
- orElseGet() : νλΌλ―Έν°λ‘ ν¨μν μΈν°νμ΄μ€λ₯Ό λ°μ & κ°μ΄ 미리 μ‘΄μ¬νμ§ μλ κ²½μ° μ¬μ©
2οΈβ£ of()μ ofNullable()
Optional<String> one = Optional.ofNullable("123");
Optional<String> two = Optinal.of("123");
- of()λ xκ° nullμ΄ μλμ΄ νμ€ν λλ§ μ¬μ©
- ofNullable()μ xκ° nullμΌ μλ μμλλ§ μ¬μ©
3οΈβ£ empty()
Optional<String> three = Optional.empty();
- μ°Έμ‘° λ³μλ₯Ό κΈ°λ³Έκ°μΌλ‘ μ΄κΈ°νν λ μ¬μ©
- Optionalμ Wrapperν΄λμ€ μ΄κΈ° λλ¬Έμ λΉ κ°μ΄ μ¬ μ μμ
4οΈβ£ get()
- Optional κ°μ²΄μ μ μ₯λ κ°μ κ°μ Έμ€λ λ©μλ
- κ°μ΄ null μΌ λ orElse() λ±μ νμ©νμ¬ λν΄νΈ κ° μ§μ κ°λ₯
'Study > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[TIL] μ€λ λ (0) | 2022.05.22 |
---|---|
[TIL] νμΌ μ μΆλ ₯ (0) | 2022.05.21 |
[TIL] λλ€ (0) | 2022.05.20 |
[TIL] μλν μ΄μ (0) | 2022.05.18 |
[TIL] enum (0) | 2022.05.18 |