๐ป Programming/Java
[Java] ์๋ฐ ์คํธ๋ฆผ(stream)์ ์ด์ฉํ ์ ์ฉํ ๋ณํ / ์ถ์ถ ๋ฐฉ๋ฒ๋ค
์๋ฐ ์คํธ๋ฆผ์ ์ด์ฉํ ์ ์ฉํ ๋ณํ ๋ฐฉ๋ฒ
ํด๋น ๊ฒ์๊ธ์ 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,}
'๐ป Programming > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์๋ฐ 2์ฐจ์ ๋ฐฐ์ด ์์ฑ(์ ์ธ ๋ฐ ์ด๊ธฐํ) (0) | 2020.08.31 |
---|---|
์ดํด๋ฆฝ์ค์ ์คํ๋งํด์ฆ(STS) ์ค์นํ๊ธฐ (0) | 2020.07.22 |
[Java] ์๋ฐ ThreadPoolExecutor ๋ฅผ ์ด์ฉํ์ฌ ๋ฉํฐ์ฐ๋ ๋ ๊ตฌํํ๊ธฐ (0) | 2020.04.17 |
[Java] ์๋ฐ ๋ฉํฐ์ฐ๋ ๋ (Multi Thread) (0) | 2020.04.17 |
[Java] ์๋ฐ ๋ฉํฐ์ฐ๋ ๋์์ ๋ก๊ทธํ์ธ์ ์ํ uuid ์ค์ ํ๊ธฐ (0) | 2020.03.13 |