자바 가상 머신 (jvm) 에 있는 어레이로의 설정되었습니다 변환할지 싶다. 이렇게 할 수 있는 몇 가지 명백하네 (즉 수신기마다 루프) 이지만, 뭔가 좀 깔끔한 같은 뭔가 싶다.
java.util.Arrays.asList(Object[] a);
아무 생각 없어?
Set<T> mySet = new HashSet<T>();
Collections.addAll(mySet, myArray);
또한: 와일드링이 lionbridge 어레이입니다 가득 프리미티브를 가능
Jdk 에 <. 아니, 차라리 그냥 기록하십시오 명백하네 '는' 8 랩 (wrap) 및 설정하기 추가 루프 할 수 있는 한 번에.
Jdk >, 대한, 다음과 같은 항목을 매력적인 옵션은 = 8
Arrays.stream(intArray).boxed().collect(Collectors.toSet());
[1]: http://java.sun.com/javase/6/docs/api/java/util/Collections.html # %29 %20t. addall% 28java.util.collection,
우리는 '' 옵션을 사용하면 스트리밍합니다 잘 알려져 있다. 우리는 여러 가지 방법으로 스트리밍합니다 얻을 수 있다.
Set<String> set = Stream.of("A", "B", "C", "D").collect(Collectors.toCollection(HashSet::new));
System.out.println(set);
String[] stringArray = {"A", "B", "C", "D"};
Set<String> strSet1 = Arrays.stream(stringArray).collect(Collectors.toSet());
System.out.println(strSet1);
// if you need HashSet then use below option.
Set<String> strSet2 = Arrays.stream(stringArray).collect(Collectors.toCollection(HashSet::new));
System.out.println(strSet2);
'소스 코드' 가 추가된 요소가 있는 한 명씩 콜레스s.토스트 () 는 '하지만' 될 것으로 보인다 '' 하시세 하시세 사양명세 보장되지 않습니다.
>. 유형에 따라, 또는 가변성, 세리아리자빌레티 보장은 없다고 ". >. 스레드 안전 세트 returned.".
그래서 나중에 옵션을 사용하는 것이 좋습니다. 출력은: ' [A, B, C, D] [A, B, C, D] [A, B, C, D] '
Factory method 설정할 수 있는 '9' 는 java 세실로프 정적임 되돌려줍니다 제공된 요소 또는 어레이입니다.
@SafeVarargs
static <E> Set<E> of(E... elements)
우리는 두 가지 방법으로 불변 설정되었습니다 얻을 수도 있습니다.
세이코프로프 (라이스콜라리스트 (array) '' 1. 라이스윈슬림 스콜레스 (콜레스s.투 모드리피아블리스트 (), '2' (array).
'콜레스s.투 모드리피아블리스트 사용 방법 () 는 자바 가상 머신 (jvm) 에' 세실로프 내부적으로 '9'. 또한 이것 답변 의 내거냐 intel. com/performance/mobile/benchmarks.
[1]: https://docs.oracle.com/javase/9/docs/api/java/util/Set.html # 변경 불가
Eclipse 컬렉션 에서, 다음과 같은 것입니다.
Set<Integer> set1 = Sets.mutable.of(1, 2, 3, 4, 5);
Set<Integer> set2 = Sets.mutable.of(new Integer[]{1, 2, 3, 4, 5});
MutableSet<Integer> mutableSet = Sets.mutable.of(1, 2, 3, 4, 5);
ImmutableSet<Integer> immutableSet = Sets.immutable.of(1, 2, 3, 4, 5);
Set<Integer> unmodifiableSet = Sets.mutable.of(1, 2, 3, 4, 5).asUnmodifiable();
Set<Integer> synchronizedSet = Sets.mutable.of(1, 2, 3, 4, 5).asSynchronized();
ImmutableSet<Integer> immutableSet = Sets.mutable.of(1, 2, 3, 4, 5).toImmutable();
참고: 나는 커미터는 Eclipse 에 대한 컬렉션
빨리. 할 수 있습니다.
// Fixed-size list
List list = Arrays.asList(array);
// Growable list
list = new LinkedList(Arrays.asList(array));
// Duplicate elements are discarded
Set set = new HashSet(Arrays.asList(array));
및 반전하려면
// Create an array containing the elements in a list
Object[] objectArray = list.toArray();
MyClass[] array = (MyClass[])list.toArray(new MyClass[list.size()]);
// Create an array containing the elements in a set
objectArray = set.toArray();
array = (MyClass[])set.toArray(new MyClass[set.size()]);
/**
* Handy conversion to set
*/
public class SetUtil {
/**
* Convert some items to a set
* @param items items
* @param <T> works on any type
* @return a hash set of the input items
*/
public static <T> Set<T> asSet(T ... items) {
return Stream.of(items).collect(Collectors.toSet());
}
}
지금까지 많이 멋지구리해요 답을 이미 대다수는 won& # 39 의 프리미티브를 작동합니까 어레이입니다, t (like 'int []', '긴 []', '챨 [] []', ',' 바이트입니다 etc.)
자바 가상 머신 (jvm) 에 8 잔인하니 상자 어레이에는 수 있습니다.
Integer[] boxedArr = Arrays.stream(arr).boxed().toArray(Integer[]::new);
그럼 사용하여 설정할 변환하시겠습니까 스트리밍합니다:
Stream.of(boxedArr).collect(Collectors.toSet());
가끔 일부 표준 라이브러리를 사용하여 많은 도움이 됩니다. 살펴보는 시도하시겠습니까 아파치 커먼즈 컬렉션. 이 경우에는 문제가 단순히 이 같은 일이 변신했다
String[] keys = {"blah", "blahblah"}
Set<String> myEmptySet = new HashSet<String>();
CollectionUtils.addAll(pythonKeywordSet, keys);
Avamar 은 콜레스티언스티우스 javadoc
private Map<Integer, Set<Integer>> nobreaks = new HashMap();
nobreaks.put(1, new HashSet(Arrays.asList(new int[]{2, 4, 5})));
System.out.println("expected size is 3: " +nobreaks.get(1).size());
출력은
expected size is 3: 1
변경하십시오.
nobreaks.put(1, new HashSet(Arrays.asList( 2, 4, 5 )));
출력은
expected size is 3: 3
String[] strs = {"A", "B"};
Set<String> set = Set.copyOf(Arrays.asList(strs));
['세이코프로프'] [1] 을 반환하는지 불가능한 '세트' 요소를 포함하는 주어진 '수집'.
주어진 ',' 안 'null' 취합은 &, 빨리, 그리고 'null' 요소를 포함할 수 없습니다.
[1]: https://docs.oracle.com/javase/10/docs/api/java/util/Set.html # copyOf (제바스티우스콜레스티온)
'또는' 사용 '콜레스티온티우스 아라위티우스 stanford-postagger-3.0.jar' 에서 '
import static edu.stanford.nlp.util.ArrayUtils.asSet;
or
import static edu.stanford.nlp.util.CollectionUtils.asSet;
...
String [] array = {"1", "q"};
Set<String> trackIds = asSet(array);