英文:
How do I convert an array of characters to a set?
问题
String line = scan.nextLine();
char[] arr = line.toCharArray();
System.out.println(Arrays.toString(arr));
HashSet<Character> repeat = new HashSet<Character>(Arrays.asList(arr));
System.out.println(repeat);
错误信息是:
错误:找不到适用的构造函数以 HashSet(List<char[]>)
英文:
How do I solve this error converting an array into a set?
String line = scan.nextLine();
char[] arr = line.toCharArray();
System.out.println(Arrays.toString(arr));
HashSet<Character> repeat = new HashSet<Character>(Arrays.asList(arr));
System.out.println(repeat);
The error is:
error: no suitable constructor found for HashSet(List<char[]>)
答案1
得分: 2
Arrays.asList(arr)并不会给你一个List<Character>,而是会给你一个List<char[]>,这是不正确的,因为你的HashSet构造函数期望的是Collection<Character>类型。正是这个冲突导致了编译失败。
修复的方法是创建一个List<Character>,然后逐个添加元素,或者更简单的方法是直接使用集合本身:
Set<Character> repeat = new HashSet<>();
for(char c: arr)
    repeat.add(c);
有许多替代方法,但归根结底都是将字符数组的元素复制到集合中,可以通过列表或其他方式实现。
英文:
Arrays.asList(arr) does not give you a List<Character> that you would use as Collection<Character> in your call to the HashSet constructor.
It gives List<char[]>, which would be an incorrect value as the expected Collection<Character> type. It's this conflict that's making your compilation fail.
The way to fix it is by creating a List<Character> and adding elements to it one by one, or, even simpler, to do that straight with the set itself:
Set<Character> repeat = new HashSet<>();
for(char c: arr)
	repeat.add(c);
There are many alternative approaches, but it boils down to copying elements from the char array to the set, via a list or not.
答案2
得分: 0
@nebneb-5 试试这个 -
    public class Test {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String line = scan.nextLine();
            //char[] arr = line.toCharArray();
            List<Character> list = line.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
            Set<Character> repeat = list.stream().collect(Collectors.toSet());
            System.out.println(repeat);
        }
    }
英文:
@nebneb-5 Try this -
public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String line = scan.nextLine();
        //char[] arr = line.toCharArray();
        List<Character> list = line.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
        Set<Character> repeat = list.stream().collect(Collectors.toSet());
        System.out.println(repeat);
    }
}
答案3
得分: 0
你可以使用 String.codePoints 方法来实现这个目的:
String line = "abcdeeadfc";
HashSet<Character> repeat = line.codePoints()
        .mapToObj(ch -> (char) ch)
        .collect(Collectors.toCollection(HashSet::new));
System.out.println(repeat); // [a, b, c, d, e, f]
<sup>另请参阅:如何将字符串添加到字符数组?</sup>
英文:
You can use String.codePoints method for this porpose:
String line = "abcdeeadfc";
HashSet<Character> repeat = line.codePoints()
        .mapToObj(ch -> (char) ch)
        .collect(Collectors.toCollection(HashSet::new));
System.out.println(repeat); // [a, b, c, d, e, f]
<sup>See also: How do I add String to a char array?</sup>
答案4
得分: 0
Java-9 解决方案:
Set<Character> repeat = line.chars()
                        .mapToObj(ch -> (char) ch)
                        .collect(Collectors.toSet());
查阅 String#chars 和 IntStream#mapToObj 以了解更多信息。
英文:
Java-9 solution:
Set<Character> repeat = line.chars()
						.mapToObj(ch -> (char) ch)
						.collect(Collectors.toSet());
Check String#chars and IntStream#mapToObj to learn more about them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论