상세 컨텐츠

본문 제목

스트림 외울거

자바

by esoesmio 2023. 3. 30. 14:02

본문

Stream<String> s = Pattern.compile(",").splitAsStream("1,2,3,4");
s.forEach(System.out::println);
//Stream<String> s = Stream.generate(()->"hello");
//s.forEach(System.out::println);
//무한히 hello 찍는거;

//        String[] array = new String[]{"a", "b", "c"};
//        Stream<String> stream1 = Arrays.stream(array);
//        stream1.forEach(System.out::println);
//        System.out.println(stream1);


//Stream<Integer> stream = Stream.iterate(100,n->n+10).limit(5);
//stream.forEach(System.out::println);
//
//
//
//Stream<Integer>stream3 = IntStream.range(1,10).limit(3).boxed();
//stream3.forEach(System.out::println);

//IntStream stream4 = "hellowworld".chars();
//        stream4.forEach(System.out::println);

//IntStream stream = "helloworld".chars();
//stream.forEach(System.out::println);
Stream<String> stream1 = Stream.of("apple","banana");
Stream<String> stream2 = Stream.of("cat", "off");

Stream<String> cc = Stream.concat(stream1, stream2);
cc.filter(a->a.contains("p"))
        .filter(a->a.contains("p"))
        .forEach(System.out::println);



//스트링 병합

 

List<List<String>> ta = Arrays.asList(Arrays.asList("1,2,3"),Arrays.asList("4","1","5"));

        List<String> fl = ta.stream()
                .flatMap(Collection::stream)
                .peek(System.out::println)
                .collect(Collectors.toList());
        System.out.println(fl);

 

//2차원리스트를 평평하게.

int sum = IntStream.range(1,10).sum();
//count, max, min, average

        int evensum = IntStream.range(1,5)
                .filter(a->(a%2)==0)
                .sum();

    }
Set<Integer> evennumber = IntStream.range(1,100).boxed()
        .filter(a->(a%2)==0)
        .collect(Collectors.toSet());


1부터 100 짝수만 셋에 넣기

List<String> fruit = Arrays.asList("banana", "apple");
String f = fruit.stream()
        .collect(Collectors.joining(",","<",">"));

        System.out.println(f);

리스트의 요소 스트링으로

 

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<String> strings = Arrays.asList("apple", "banana", "carrot", "date", "eggplant");

        List<String> filteredStrings = strings.stream()
            .filter(s -> s.length() >= 5)
            .collect(Collectors.toList());

        System.out.println(filteredStrings);
    }
}

 

이 예제에서는 strings 리스트를 스트림으로 변환하고, filter 메서드를 사용하여 길이가 5 이상인 문자열만 필터링합니다. 그리고 collect 메서드를 사용하여 필터링된 문자열 요소들을 새로운 리스트

 

List<String> kk = Arrays.asList("apple", "banana", "cuninian");
List<Integer> gg = kk.stream()
        .filter(a->a.length()>5)
        .map(String::length)
        .collect(Collectors.toList());

///렝스가 6 이상인녀석만 숫자로 변경해서 리스트에 넣어라
public class homework22 {
    public static void main(String[] args) {

        List<String> list = Arrays.asList("1","2","3");

        List<Integer>g = list.stream()
                .map(homework22::parseint)
                .collect(Collectors.toList());


        System.out.println(g);
    }


    public static int parseint(String s){


        return Integer.parseInt(s);
    }

}

 스트림 람다에 스테틱 메소드를 넣어서

 

List<Integer> integerList = Arrays.asList(20,30,50,88,100);
int[] mapToInt = integerList.stream().mapToInt(x->x).toArray(); // Integer -> int(primitive type)

Stream.of("a1","a2","a3")
	.map( s-> s.substring(1) )
        .mapToInt(Integer::parseInt)
        .max()
        .ifPresent(System.out::println); // String -> Int

 

maptoINt 아주중요한거

 

 

 

관련글 더보기

댓글 영역