如何将字符数组转换为集合?

huangapple go评论73阅读模式
英文:

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&lt;Character&gt; repeat = new HashSet&lt;Character&gt;(Arrays.asList(arr));
System.out.println(repeat);

The error is:

error: no suitable constructor found for HashSet(List&lt;char[]&gt;)

答案1

得分: 2

Arrays.asList(arr)并不会给你一个List&lt;Character&gt;,而是会给你一个List&lt;char[]&gt;,这是不正确的,因为你的HashSet构造函数期望的是Collection&lt;Character&gt;类型。正是这个冲突导致了编译失败。

修复的方法是创建一个List&lt;Character&gt;,然后逐个添加元素,或者更简单的方法是直接使用集合本身:

Set&lt;Character&gt; repeat = new HashSet&lt;&gt;();
for(char c: arr)
    repeat.add(c);

有许多替代方法,但归根结底都是将字符数组的元素复制到集合中,可以通过列表或其他方式实现。

英文:

Arrays.asList(arr) does not give you a List&lt;Character&gt; that you would use as Collection&lt;Character&gt; in your call to the HashSet constructor.

It gives List&lt;char[]&gt;, which would be an incorrect value as the expected Collection&lt;Character&gt; type. It's this conflict that's making your compilation fail.

The way to fix it is by creating a List&lt;Character&gt; and adding elements to it one by one, or, even simpler, to do that straight with the set itself:

Set&lt;Character&gt; repeat = new HashSet&lt;&gt;();
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&lt;Character&gt; list = line.chars().mapToObj(c -&gt; (char) c).collect(Collectors.toList());
        Set&lt;Character&gt; 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 = &quot;abcdeeadfc&quot;;

HashSet&lt;Character&gt; repeat = line.codePoints()
        .mapToObj(ch -&gt; (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#charsIntStream#mapToObj 以了解更多信息。

英文:

Java-9 solution:

Set&lt;Character&gt; repeat = line.chars()
						.mapToObj(ch -&gt; (char) ch)
						.collect(Collectors.toSet());

Check String#chars and IntStream#mapToObj to learn more about them.

huangapple
  • 本文由 发表于 2020年10月16日 13:40:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定