[Java] String Comma(,) to List, list 중복 제거 :: Java 8 Stream

동민·2021년 12월 10일
0

import

import java.util.stream.Collectors;
import java.util.stream.Stream;

String to List(Integer)

String linkedNo = "123, 123, 456, 432, 567, 123";

List<Integer> list = Stream.of(linkedNo.split(","))
	.map(String::trim)
	.map(Integer::parseInt)
	.collect(Collectors.toList());

String to List(String)

String linkedNo = "123, 123, 456, 432, 567, 123";

List<String> list = Stream.of(linkedNo.split(","))
	.map(String::trim)
	.collect(Collectors.toList());

중복 제거

list = list.stream().distinct().collect(Collectors.toList());

String to List(Integer) + 중복 제거

String linkedNo = "123, 123, 456, 432, 567, 123";

return Stream.of(linkedNo.split(","))
	.map(String::trim)
	.map(Integer::parseInt)
    	.distinct()
	.collect(Collectors.toList());
profile
BE Developer

0개의 댓글