Stream().map()

·2022년 11월 8일
0

Function: 함수형 인터페이스, 추상메소드: apply
map(): 해당 리턴타입만 출력하게 해줌

  • 예시
    ArrayList<Student> as = new ArrayList<Student>();
    //
    //
    Function<Student, String> f = new Function<Student, String>() {
    	@Override
    	public String apply(Student t) {
    		return t.getName();
    	}
    };
    as.stream().map(f).forEach(t->System.out.println(t));
  
- HashMap<K, V>을 Function의 리턴타입으로 받을 수 있다.
>```java
ArrayList<Student> as = new ArrayList<Student>();
//
//
Function<Student, HashMap<String, Object>> f = new Function<Student, HashMap<String, Object>>(){
	@Override
	public HashMap<String, Object> apply(Student t){
    //HashMap에 담긴 값 불러옴
    	HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("name", t.getName());
        map.put("num", t.getNum());
        return map;
    }
};
as.stream().map(f).forEach(t->System.out.println(t));
profile
웹개발입문자

0개의 댓글