본문 바로가기
JAVA

[JAVA] ArrayList Stream 이용 , string join 예제 입니다.

by Hwoarang757 2024. 5. 6.
package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.stream.Collectors;

public class TestCls {

	public static void main(String[] args) {
		ArrayList<HashMap<String,String>> testArray = new ArrayList<>();


		testArray.add(new HashMap<>() {{
			put("name", "홍길동");
			put("id" , "user1");
			put("empno" , "12345678");
		}} );

		testArray.add(new HashMap<>() {{
			put("name", "김철수");
			put("id" , "user2");
			put("empno" , "12345679");
		}} );

		testArray.add(new HashMap<>() {{
			put("name", "김영미");
			put("id" , "user1");
			put("empno" , "12345670");
		}} );


		String joinStr = testArray.stream().map(a -> a.get("name")).collect(Collectors.joining(","));

		System.out.println(joinStr);


	}

}