๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[JAVA] ์ŠคํŠธ๋ฆผ(Stream) ๋ณธ๋ฌธ

Question

[JAVA] ์ŠคํŠธ๋ฆผ(Stream)

Shinjio 2023. 7. 27. 09:09
  • Java 8๋ถ€ํ„ฐ ์ง€์› ์‹œ์ž‘
  • ์ปฌ๋ ‰์…˜์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์—˜๋ฆฌ๋จผํŠธ๋“ค์„ ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฉด์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ ํŒจํ„ด
  • ** ์ปฌ๋ ‰์…˜ : ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ, ๊ทธ๋ฃน (ex. List, Set, Queue, Map ๋“ฑ)
  • ๋žŒ๋‹ค์‹๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด ์ปฌ๋ ‰์…˜์— ๋“ค์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ ๊ฐ€๋Šฅ
  • (๊ธฐ์กด for ๋˜๋Š” foreach๋ฌธ ์‚ฌ์šฉ)

 

1. ์ƒ์„ฑํ•˜๊ธฐ : ์ŠคํŠธ๋ฆผ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

 

1.1. ๋ฐฐ์—ด ์ŠคํŠธ๋ฆผ : Arrays.stream(๋ฐฐ์—ด)

 

์ •์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด array์™€ ์ •์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ,

array์— n์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€๋ฅผ returnํ•˜๋„๋ก!

import java.util.Arrays;

class Solution {
    public int solution(int[] array, int n) {
        int answer = 0;
        answer = (int)Arrays.stream(array).filter(x -> x==n).count();
        return answer;
    }
}

 

 

1.2. ์ปฌ๋ ‰์…˜ ์ŠคํŠธ๋ฆผ : Collection.stream()

 

1.3. Stream.builder() : Stream.<์ž๋ฃŒํ˜•>builder()

.add๋ฅผ ํ†ตํ•ด ์ง์ ‘์ ์œผ๋กœ ์›ํ•˜๋Š” ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ

 

1.4. Stream.generate() : Stream.generate(()->).limit()

generate ์•ˆ์— ๋žŒ๋‹ค๋กœ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ 

์ด ๋•Œ ์ƒ์„ฑ๋˜๋Š” ์ŠคํŠธ๋ฆผ์€ ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š๊ณ  ๋ฌดํ•œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ์ œํ•œํ•ด์•ผ ํ•จ

 

1.5. Stream.iterate() : Stream.iterate(์ดˆ๊ธฐ๊ฐ’, n->n+2).limit()

์ดˆ๊ธฐ๊ฐ’๊ณผ ํ•ด๋‹น๊ฐ’์„ ๋‹ค๋ฃจ๋Š” ๋žŒ๋‹ค๋ฅผ ์ด์šฉํ•ด ์ŠคํŠธ๋ฆผ์— ๋“ค์–ด๊ฐˆ ์š”์†Œ๋ฅผ ๋งŒ๋“ฆ

 

1.6. ๊ธฐ๋ณธ ํƒ€์ž…ํ˜• ์ŠคํŠธ๋ฆผ : IntStream.range(), LongStream.range() ...

 

 

2. ๊ฐ€๊ณตํ•˜๊ธฐ : ํ•„ํ„ฐ๋ง(filtering), ๋งตํ•‘(mapping) ๋“ฑ

 

2.1. Filtering 

์ŠคํŠธ๋ฆผ ๋‚ด ์š”์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ํ‰๊ฐ€ํ•ด์„œ ๊ฑธ๋Ÿฌ๋‚ด๋Š” ์ž‘์—…

 

Stream<String> stream = 
  names.stream()
  .filter(name -> name.contains("a"));
// [Elena, Java]
class Solution {
    public String solution(String s) {
        String answer = "";
        
        List<Integer> upper = s.chars().filter(item -> item <= 90).boxed().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        List<Integer> lower = s.chars().filter(item -> item > 90).boxed().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

        for(int item : lower) {
        	answer += (char)item;
        }
        
        for(int item : upper) {
        	answer += (char)item;
        }
     
        return answer;
    }
}

 

 

2.2. Mapping

์ŠคํŠธ๋ฆผ ๋‚ด ์š”์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ํŠน์ • ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜

 

Stream<String> stream = 
  names.stream()
  .map(String::toUpperCase);
// [ERIC, ELENA, JAVA]

 

2.3. Sorting

 

List<String> lang = 
  Arrays.asList("Java", "Scala", "Groovy", "Python", "Go", "Swift");

lang.stream()
  .sorted()
  .collect(Collectors.toList());
// [Go, Groovy, Java, Python, Scala, Swift]

lang.stream()
  .sorted(Comparator.reverseOrder())
  .collect(Collectors.toList());
// [Swift, Scala, Python, Java, Groovy, Go]

 

 

class Solution {
    public long solution(long n) {
        long answer = 0;

        List<Long> list = new ArrayList<Long>();
        
        while(n>0) {
        	list.add(n%10);
        	n /= 10;
        }
       
       list = list.stream().sorted().collect(Collectors.toList());
       list.stream().forEach(System.out::println);
     
       int mul = 1;
        for(long item : list) {
        	answer += item * mul;
        	mul *= 10;
        }
        
        return answer;
    }
}

 

์—ญ์ˆœ ์ •๋ ฌ์‹œ์—๋Š”

 

list = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

 

2.4. Iterating

์ŠคํŠธ๋ฆผ ๋‚ด ์š”์†Œ๋“ค ๊ฐ๊ฐ์— ๋Œ€ํ•œ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰

๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์Œ → ์ค‘๊ฐ„์— ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณผ ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

int sum = IntStream.of(1, 3, 5, 7, 9)
  .peek(System.out::println)
  .sum();

 

3. ๊ฒฐ๊ณผ๋งŒ๋“ค๊ธฐ 

 

3.1. Calculating : count(), sum(), min(), max(), average()

์ตœ์†Œ, ์ตœ๋Œ€, ํ•ฉ, ํ‰๊ท ๋“ฑ ๊ธฐ๋ณธํ˜• ํƒ€์ž…์œผ๋กœ ๊ฒฐ๊ณผ ์ œ๊ณต

 

3.2. Reduction

 

3.3. Collecting 

Collector ํƒ€์ž…์˜ ์ธ์ž๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌ

- toList() : ์ž‘์—…ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜

- joining() : ์ž‘์—…ํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ•˜๋‚˜์˜ ์ŠคํŠธ๋ง์œผ๋กœ ์ด์–ด ๋ถ™์ž„

 

 

์ฝ”๋“œํ•ด์„

public List<OrdersResDto> findAllOrders() {
		List<Orders> orders = ordersJpaRepository.findAll();
		
		if (orders == null || orders.isEmpty()) {
			throw new IllegalArgumentException("์กฐํšŒ๋œ ์ฃผ๋ฌธ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค");
		}
		
		List<OrdersResDto> result = orders.stream().map(obj -> new OrdersResDto(obj)).collect(Collectors.toList());
		
		return result;
	}

 

List ํƒ€์ž…์˜ orders์˜ ๊ฐ ์—˜๋ฆฌ๋ฉ˜ํŠธ๋ฅผ ๊บผ๋‚ด์„œ (obj) OrdersResDto๋กœ ๋ณ€ํ™˜ ํ›„ Stream์˜ ๊ฒฐ๊ณผ๋ฅผ List ํƒ€์ž…์œผ๋กœ ๋ฆฌํ„ด

 

 

 

์ฐธ๊ณ ์ž๋ฃŒ 
1. https://futurecreator.github.io/2018/08/26/java-8-streams/
2. https://hbase.tistory.com/171