1. ν¨μν μΈν°νμ΄μ€
λ¨ νλμ μΆμ λ©μλλ§ μλ μ μλμ΄μλ μΈν°νμ΄μ€ (Java 8λΆν° μ§μ κ°λ₯)
ν¨μλ₯Ό κ°μΌλ‘ μ·¨κΈνκΈ° λλ¬Έμ μ΄λ€ ν¨μλ₯Ό νΈμΆν λ ν¨μ μ체λ₯Ό νλΌλ―Έν°λ‘ μ λ¬ κ°λ₯
public class Example {
public static void main(String[] args){
List<CryptoCurrency> cryptoCurrencies = SampleData.cryptoCurrencies;
Collections.sort(cryptoCurrencies, new Comparator<CryptoCurrency>() {
@Override
public int compare(CryptoCurrency cc1, CryptoCurrency cc2) {
return cc1.getUnit().name().compareTo(cc2.getUnit().name());
}
});
}
}
- CryptoCurrency κ°μ²΄λ₯Ό νν λ¨μμ μ€λ¦μ°¨μμΌλ‘ μ λ ¬νκΈ° μν΄ Comparator μΈν°νμ΄μ€ μ¬μ©
2. λλ€ ννμ
ν¨μν μΈν°νμ΄μ€μ μ μλ μΆμ λ©μλλ₯Ό ννμμΌλ‘ ꡬνν κ²
μΈν°νμ΄μ€μ μ΅λͺ ꡬν ν΄λμ€λ₯Ό κ°κ²°νκ² νννλ λ°©λ²
public class Example {
public static void main(String[] args) {
List<CryptoCurrency> cryptoCurrencies = SampleData.cryptoCurrencies;
Collections.sort(cryptoCurrencies,
(cc1, cc2) -> cc1.getUnit().name().compareTo(cc2.getUnit().name()));
}
}
- ν¨μν μΈν°νμ΄μ€μ μΆμ λ©μλλ₯Ό λλ€ ννμμΌλ‘ μμ±ν΄μ λ©μλμ νλΌλ―Έν°λ‘ μ λ¬νλ€λ μλ―Έλ
λ©μλ μ체λ₯Ό νλΌλ―Έν°λ‘ μ λ¬νλ κ²μ΄ μλλΌ ν¨μν μΈν°νμ΄μ€λ₯Ό ꡬνν ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λλ€ ννμμΌλ‘ μμ±ν΄μ μ λ¬νλ κ² !
β³οΈ λλ€ μΊ‘μ³λ§
- λλ€ ννμ μΈλΆμμ μ μλ μμ λ³μλ₯Ό λλ€ ννμμ μ¬μ©νλ κ²
- λλ€ ννμμμ μ¬μ©λλ μμ λ³μλ final λλ final κ°μ ν¨λ ₯μ μ§λ μΌ ν¨
3. λ©μλ λ νΌλ°μ€
λ©μλμ μ΄λ¦κ³Ό ꡬλΆμ(::)λ₯Ό μ¬μ©νμ¬ λλ€ννμμ λ κ°κ²°νκ² μμ±
β³οΈ ClassName :: static method
public class Example {
public static void main(String[] args) {
List<CryptoCurrency> cryptoCurrencies = SampleData.cryptoCurrencies;
cryptoCurrencies.stream()
.map(cc -> cc.getName())
// .map(name -> StringUtils.upperCase(name))
.map(StringUtils::upperCase)
.forEach(name -> System.out.println(name));
}
}
β³οΈ ClassName :: instance method
public class Example {
public static void main(String[] args) {
List<CryptoCurrency> cryptoCurrencies = SampleData.cryptoCurrencies;
cryptoCurrencies.stream()
.map(cc -> cc.getName())
// .map(name -> name.toUpperCase())
.map(String::toUpperCase)
.forEach(name -> System.out.println(name));
}
}
β³οΈ object :: instance method
public class Example {
public static void main(String[] args) {
List<CryptoCurrency> cryptoCurrencies = SampleData.cryptoCurrencies;
int btcPrice = cryptoCurrencies.stream()
.filter(cc -> cc.getUnit() == CurrencyUnit.BTC)
.findFirst()
.get()
.getPrice();
int amount = 2;
PaymentCalculator calculator = new PaymentCalculator();
cryptoCurrencies.stream()
.filter(cc -> cc.getUnit() == CurrencyUnit.BTC)
.map(cc -> new ImmutablePair(cc.getPrice(), amount())
// .map(pair -> calculator.getTotalPayment(pair))
.map(calculator::getTotalPayment)
.forEach(System.out::println);
}
}
β³οΈ ClassName :: new π μμ±μ
public class Example {
public static void main(String[] args) {
List<CryptoCurrency> cryptoCurrencies = SampleData.cryptoCurrencies;
int amount = 2;
Optional<PaymentCalculator> optional =
cryptoCurrencies.stream()
.filter(cc -> cc.getUnit() == CurrencyUnit.BTC)
.map(cc -> new ImmutablePair(cc.getPrice(), amount))
// .map(pair -> new PaymentCalculator(pair))
.map(PaymentCalculator::new)
.findFirst();
}
}
4. ν¨μ λμ€ν¬λ¦½ν°
ν¨μν μΈν°νμ΄μ€μ νλΌλ―Έν° νμκ³Ό λ¦¬ν΄ κ°μ νν νμ κ°λ₯
β³οΈ Predicate
- T -> boolean
- T νμ μ λλ€ νλΌλ―Έν°μ boolean νμ μ κ°μ 리ν΄νλ Predicateμ ν¨μ λμ€ν¬λ¦½ν°
- filter λ©μλμμ μ£Όλ‘ μ¬μ©λ¨
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
...
...
}
public class Example {
public static void main(String[] args){
List<CryptoCurrency> cryptoCurrencies = SampleData.cryptoCurrencies;
List<CryptoCurrency> result = filter(cryptoCurrencies, cc -> cc.getPrice() > 500_000);
}
private static List<CryptoCurrency> filter(List<CryptoCurrency> cryptoCurrencies,
Predicate<CryptoCurrency> p) {
List<CryptoCurrency> result = new ArrayList<>();
for(CryptoCurrency cc : cryptoCurrencies) {
if(p.test(cc)) {
result.add(cc);
}
}
return result;
}
}
- filter λ©μλλ νλΌλ―Έν°λ‘ Predicateμ κ°μ§λ©° filter λ©μλ λ΄λΆμμ μ΄ Predicateμ μ¬μ©νμ¬ test λ©μλμ λ¦¬ν΄ κ°μ΄ trueμΈ λ°μ΄ν°λ§ νν°λ§ν¨
β³οΈ Consumer
- T -> void
- Consumerμ μ μλ μΆμ λ©μλμ νλΌλ―Έν°κ° T νμ μ΄κ³ , λ¦¬ν΄ κ°μ΄ μμ
- μ λ¬λ°μ λ°μ΄ν°λ‘ μ΄λ€ μ²λ¦¬λ₯Ό νμ§λ§ κ²°κ³Ό κ°μ λ°νν νμλ μμ
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
...
...
}
public class Example {
public static void main(String[] args){
List<CryptoCurrency> cryptoCurrencies = SampleData.cryptoCurrencies;
List<CryptoCurrency> filtered =
filter(cryptoCurrencies,
cc -> cc.getUnit() == CryptoCurrency.CurrencyUnit.BTC ||
cc.getUnit() == CryptoCurrency.CurrencyUnit.ETH);
addBookmark(filtered, cc -> saveBookmark(cc));
}
private static List<CryptoCurrency> filter(List<CryptoCurrency> cryptoCurrencies,
Predicate<CryptoCurrency> p) {
List<CryptoCurrency> result = new ArrayList<>();
for(CryptoCurrency cc : cryptoCurrencies) {
if(p.test(cc)) {
result.add(cc);
}
}
return result;
}
// νν μ¦κ²¨μ°ΎκΈ°
private static void addBookmark(List<CryptoCurrency> cryptoCurrencies,
Consumer<CryptoCurrency> consumer) {
for(CryptoCurrency cc : cryptoCurrencies) {
consumer.accept(cc);
}
}
}
.β³οΈ Function
- T -> R
- ꡬνν΄μΌλλ μΆμ λ©μλκ° νλμ νλΌλ―Έν°λ₯Ό κ°μ§λ©° λ¦¬ν΄ κ°μΌλ‘ R νμ μ κ°μ λ°ν
- ν¨μ λ΄μμ μ΄λ€ μ²λ¦¬ κ³Όμ μ κ±°μΉ νμ κ·Έ κ²°κ³Όλ‘ νΉμ νμ μ κ°μ λ°ννλ ν¨μ μν
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
...
...
}
public class Example {
public static void main(String[] args){
List<CryptoCurrency> cryptoCurrencies = SampleData.cryptoCurrencies;
List<CryptoCurrency> filtered =
filter(cryptoCurrencies,
cc -> cc.getUnit() == CryptoCurrency.CurrencyUnit.BTC ||
cc.getUnit() == CryptoCurrency.CurrencyUnit.ETH);
int totalPayment = calculatePayment(filtered, cc -> cc.getPrice()*2);
}
private static List<CryptoCurrency> filter(List<CryptoCurrency> cryptoCurrencies,
Predicate<CryptoCurrency> p) {
List<CryptoCurrency> result = new ArrayList<>();
for(CryptoCurrency cc : cryptoCurrencies) {
if(p.test(cc)) {
result.add(cc);
}
}
return result;
}
// ννλ³ μ΄ κ΅¬λ§€ λΉμ© κ³μ°
private static int calculatePayment(List<CryptoCurrency> cryptoCurrencies,
Function<CryptoCurrency, Integer> f) {
int totalPayment = 0;
for(CryptoCurrency cc : cryptoCurrencies) {
totalPayment += f.apply(cc);
}
return totalPayment;
}
}
β³οΈ Supplier
- () -> T
- μΆμ λ©μλκ° νλΌλ―Έν°λ₯Ό κ°μ§ μμΌλ©° λ¦¬ν΄ κ°μΌλ‘ T νμ μ κ°λ§ λ°ν
- μ΄λ€ κ°μ΄ νμν λ λ°μ΄ν°λ₯Ό μ 곡νλ μ©λλ‘ μ¬μ©
@FunctionalInterface
public interface Supplier<T> {
T get();
}
public class Example {
public static void main(String[] args) {
String mnemonic = createMnemonic();
System.out.println(mnemonic);
}
public static String createMnemonic() {
return Stream
.generate(() -> getMnemonic())
.limit(12)
.collect(Collectors.joining(""));
}
private static String getMnemonic(){
List<String> menemonic = Arrays.asList(...);
Collections.shuffle(mnemonic);
return mnemonic.get(0);
}
}
β³οΈ Bixxxxx
- ν¨μν μΈν°νμ΄μ€μμ ꡬνν΄μΌ νλ μΆμ λ©μλμ μ λ¬νλ νλΌλ―Έν°κ° νλ λ μΆκ°λμ΄ λ κ°μ νλΌλ―Έν°λ₯Ό κ°μ§λ ν¨μν μΈν°νμ΄μ€
'Spring > Spring WebFlux' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[π Reactive] 6. Sinksμ Scheduler (0) | 2024.12.23 |
---|---|
[π Reactive] 5. Reactor (0) | 2024.12.19 |
[π Reactive] 3. Blocking I/Oμ Non-Blocking I/O (0) | 2024.12.17 |
[π Reactive] 2. Reactive Streams (0) | 2024.12.16 |
[π Reactive] 1. Reactive μμ€ν κ³Ό Reactive νλ‘κ·Έλλ° (0) | 2024.12.16 |