Notice
Recent Posts
Recent Comments
Link
passion and relax
[JAVA] 배열(Array)을 Set으로 변경 본문
자바에서 Array를 Set으로 변경하는 방법들
ⓞ 변경하고자 하는 배열
String[] arrayFruit = {"apple", "banana", "kiwi", "apple"};
① 다이아몬드 연산자 이용
Set<String> setFruit = new HashSet<>(Arrays.asList(arrayFruit));
② Set.copyOf()
Set<String> setFruit = Set.copyOf(Arrays.asList(arrayFruit));
③ Collections.addAll()
Set<String> setFruit = new HashSet<String>();
Collections.addAll(setFruit, arrayFruit);
④ 구글 commons 이용
import com.google.common.collect.Sets;
Set<String> setFruit = Sets.newHashSet(arrayFruit);
⑤ Collectors 이용
import java.util.stream.Collectors;
Set<String> setFruit = Arrays.stream(arrayFruit).collect(Collectors.toSet());
⑥ Stream 이용
import java.util.stream.Stream;
Set<String> setFruit = Stream.of(arrayFruit).collect(Collectors.toCollection(HashSet::new));
⑦ 아파치 commons 이용
import org.apache.commons.collections4.CollectionUtils;
Set<String> setFruit = new HashSet<String>();
CollectionUtils.addAll(setFruit, arrayFruit);
[다운로드] https://commons.apache.org/proper/commons-collections/download_collections.cgi
'프로그래밍' 카테고리의 다른 글
[JAVA] 03. 네 변수를 알라 (원시 변수와 레퍼런스) (0) | 2024.05.20 |
---|---|
[JAVA] 02. 객체마을로의 여행 (객체에 대해 알아봅시다) (1) | 2024.05.20 |
[JAVA] 01. 껍질을 깨고 (간단한 소개) (0) | 2024.05.20 |
[Java] Java는 "참조에 의한 전달" 일까? "값에 의한 전달" 일까? (0) | 2024.03.18 |
[Java] Set을 List로 변경 (0) | 2024.03.17 |