Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- #완전수구하기
- #Java
- 증감연산자
- #이차원배열
- java조건문
- 팩토리얼
- for문 369게임
- 논리연산자
- switch-case문
- 데이터타입
- java
- if문
- 2차원배열
- plusgame
- 소인수분해
- 알고리즘
- 비교연산자
- #알고리즘
- switch문
- 삼항 연산자
- 복합대입연산자
- #java_festival
- 피보나치수열
- else if문
- 별찍기
- 연산자
- 로또 프로그램
- 변수의특징
- 이진수
- JAVA기초
Archives
- Today
- Total
숭어 개발 블로그
[Java] 요소 변환 (매핑) 본문
매핑(mapping) 이란?
- 스트림의 요소를 다른 요소로 변환하는 중간 처리 기능이다.
- 매핑 메소드는 mapXxx(), asDoubleStream(), asLongStream(), boxed(), flatMapXxx() 등이있다.
Ex) Student 스트림을 score 스트림으로 변환하고 점수를 콘솔에 출력
package study0620.Stream_Mapping;
public class Student {
private String name;
private int score;
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}//
package study0620.Stream_Mapping;
import java.util.ArrayList;
import java.util.List;
public class MapExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
// List 컬렉션 생성
List<Student> stuList = new ArrayList<Student>();
stuList.add(new Student("홍길동", 85));
stuList.add(new Student("홍길동", 92));
stuList.add(new Student("홍길동", 87));
// Student 를 score 스트림으로 변환
stuList.stream().mapToInt(t -> t.getScore()).forEach(score -> System.out.println(score));
}//
}//
Ex) 정수 스트림을 실수스트림으로 변환하고 , 기본 타입스트림을 래퍼 스트림으로 변환 하기
package study0620.Stream_Mapping;
import java.util.Arrays;
import java.util.stream.IntStream;
public class MapExample2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { 1, 2, 3, 4, 5 };
IntStream intStream = Arrays.stream(arr);
intStream.asDoubleStream().forEach(d -> System.out.println(d));
System.out.println();
intStream = Arrays.stream(arr);
intStream.boxed().forEach(t -> System.out.println(t.intValue()));
}//
}//
'JAVA > 스트림 요소 처리' 카테고리의 다른 글
[Java] 요소 기본 집계 (0) | 2023.06.21 |
---|---|
[Java] 요소 정렬 (Sorted) (0) | 2023.06.21 |
[Java] 요소 걸러내기(필터링) (0) | 2023.06.21 |
[Java] 리소스로부터 스트림 얻기 (0) | 2023.06.21 |
[JAVA] 스트림 (0) | 2023.06.21 |
Comments