์ž๋ฐ” ์ŠคํŠธ๋ฆผ์„ ์ด์šฉํ•œ ์œ ์šฉํ•œ ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•

ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์€ java์˜ stream์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๊ฐ€๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌ ํ•ด๋†“์€ ๋ฌธ์„œ๋กœ

์œ ์šฉํ•œ ์ผ€์ด์Šค๋ฅผ ๋ฐœ๊ฒฌํ•  ๋•Œ๋งˆ๋‹ค ๊ณ„์† ์—…๋ฐ์ดํŠธํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

 

 

1. String๋ฐฐ์—ด์„ int๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

int[] intArray = Stream.of(stringArray).mapToInt(Integer::parseInt).toArray();

2. Integer๋ฐฐ์—ด์„ String๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

String[] stringArray = Stream.of(arr).map(String::valueOf).toArray(String[]::new);

3. String๋ฐฐ์—ด์„ String๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

String[] stringArray = new String[]{"!", "@", "#"};
List<String> stringList = Stream.of(stringArray).collect(Collectors.toList());

4. String๋ฆฌ์ŠคํŠธ๋ฅผ String๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

List<String> stringList = new ArrayList<>(Arrays.asList("!","@","#"));
String[] stringArray = stringList.toArray(new String[0]);

5. stream().map()์„ ์ด์šฉํ•œ ๊ฐ์ฒด(object) ๋ณ€ํ™˜ํ•˜๊ธฐ

// ๋ณ€ํ™˜ ์ „ ํด๋ž˜์Šค
@Getter
class Soldier {
  String name;
  int age;

  public Soldier(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

// ๋ณ€ํ™˜ ๋Œ€์ƒ ํด๋ž˜์Šค
@Getter
class King {
  String name;
  int age;
  public King(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

// ์ƒ˜ํ”Œ Soldier stream ์ƒ์„ฑ
Stream<Soldier> s = Stream.of(new Soldier("A", 18), new Soldier("B", 22), new Soldier("C", 19));

// map()์„ ์ด์šฉํ•˜์—ฌ ๋ณ€ํ™˜ํ•˜๊ธฐ
List<King> k = s.map(soldier -> new King(soldier.getName(), soldier.getAge()))
		.collect(Collectors.toList());

 

6. filter()๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•„ํ„ฐ๋งํ•˜๊ธฐ

// ๋ณ€ํ™˜ ์ „ ํด๋ž˜์Šค
@Getter
class Soldier {
  String name;
  int age;

  public Soldier(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

// ๋ณ€ํ™˜ ๋Œ€์ƒ ํด๋ž˜์Šค
@Getter
class King {
  String name;
  int age;
  public King(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

// ์ƒ˜ํ”Œ Soldier stream ์ƒ์„ฑ
Stream<Soldier> s = Stream.of(new Soldier("Arthur", 18), new Soldier("Belisarius", 22), new Soldier("Caesar", 19));

// filter()๋ฅผ ์ด์šฉํ•˜์—ฌ Arthur๋งŒ ๊ณจ๋ผ์„œ map()์„ ์ด์šฉํ•˜์—ฌ King์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
List<King> k = s.filter(soldier -> "Arthur".equals(soldier.getName()))
    .map(soldier -> new King(soldier.getName(), soldier.getAge()))
    .collect(Collectors.toList());

 

7. min() / max() ๋ฅผ ์ด์šฉํ•˜์—ฌ comparator ๊ธฐ์ค€ ์ตœ์†Œ / ์ตœ๋Œ€ ๊ฐ’์„ ๊ฐ–๋Š” ๊ฐ์ฒด ์ถ”์ถœํ•˜๊ธฐ

    @Getter
    @Setter
    class Datapoint {
        private Double maximum;
    }
    
    public void sortStream() {
        List<Datapoint> datapoints = new ArrayList<>();
        int max = 10;
        datapoints.add(new Datapoint().withMaximum(Math.random() * max));
        datapoints.add(new Datapoint().withMaximum(Math.random() * max));
        datapoints.add(new Datapoint().withMaximum(Math.random() * max));
        log.debug("data points: {}", datapoints);

        // ๊ฐ์ฒด ๋ชฉ๋ก ์ค‘ ํŠน์ • ํ•„๋“œ(maximum)๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ตœ๋Œ€๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด ์ถ”์ถœ (stream.max() ์ด์šฉ)
        Datapoint datapointMax = datapoints.stream()
                .max(Comparator.comparing(Datapoint::getMaximum))
                .orElse(new Datapoint().withMaximum(0D));
        log.debug("data point MAX: {}", datapointMax);

        // ๊ฐ์ฒด ๋ชฉ๋ก ์ค‘ ํŠน์ • ํ•„๋“œ(maximum)๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ตœ์†Œ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด ์ถ”์ถœ (stream.min() ์ด์šฉ)
        Datapoint datapointMin = datapoints.stream()
                .min(Comparator.comparing(Datapoint::getMaximum))
                .orElse(new Datapoint().withMaximum(0D));
        log.debug("data point MIN: {}", datapointMin);
    }
    
    
    
    // ์‹คํ–‰ ๋กœ๊ทธ
    data points: [{Maximum: 5.471390124016474,}, {Maximum: 2.7559360370945916,}, {Maximum: 0.5778257233234019,}]
    data point MAX: {Maximum: 5.471390124016474,}
    data point MIN: {Maximum: 0.5778257233234019,}