慕妹3242003
首先将Character数组转换为List,然后使用HashSet<>()构造函数转换为SetList<Character> chars = Arrays.asList(new Character[] {'a', 'e', 'i', 'o', 'u', 'y'});
Set<Character> charSet = new HashSet<>(chars);
System.out.println(charSet);或者你可以直接使用Arrays.asListSet<Character> charSet = new HashSet<>(Arrays.asList('a','e','i','o','u','y'));从jdk-9开始,有一些Set.of方法可用于创建不可变对象Set<Character> chSet = Set.of('a','e','i','o','u','y');您还可以使用以下命令创建不可修改的 SetCollectionsSet<Character> set2 = Collections.unmodifiableSet(new HashSet<Character>(Arrays.asList(new Character[] {'a','e','i','o','u'})));通过使用Arrays.streamCharacter[] ch = new Character[] {'a', 'e', 'i', 'o', 'u', 'y'};
Set<Character> set = Arrays.stream(ch).collect(Collectors.toSet());