将类型为Integer的链接列表转换为JAVA中的String类型

时间:2021-01-10 07:16:38

I have a List of Integers but I would like to take that List and convert it to a HashSet.

我有一个整数列表,但我想采取该列表并将其转换为HashSet。

For example my list is as follows:

例如我的列表如下:

1234
5678
1234
7627
4328

But I would like to take that list and convert the string of integers to a HashSet so it doesn't include repeats. What is the best way to accomplish this?

但我想采取该列表并将整数字符串转换为HashSet,因此它不包括重复。完成此任务的最佳方法是什么?

My list is defined as

我的清单定义为

static List<Integer> list;

And my HashSet is defined as

而我的HashSet定义为

static HashSet<String> set = new HashSet<String>(list);

My error is that I can't convert from int to string so what can I do to solve this?

我的错误是我无法从int转换为字符串所以我该怎么做才能解决这个问题?

3 个解决方案

#1


0  

Assuming you are using an ArrayList as the instantiated form of List<>:

假设您使用ArrayList作为List <>的实例化形式:

    for (Integer value : list) {
        set.add(value.toString());
    }

This will iterate through your List and take each integer, convert it to a String, and add that value to your HashSet.

这将迭代您的List并获取每个整数,将其转换为String,并将该值添加到您的HashSet。

#2


1  

One way is to use streams:

一种方法是使用流:

Set<String> set = list.stream()
    .map(Object::toString)
    .collect(Collectors.toSet());

First, you stream the list. Then, each element is converted to string and finally all elements are collected to a set. By default, Collectors.toSet() creates a HashSet, though this is not guaranteed by the specification.

首先,您流式传输列表。然后,将每个元素转换为字符串,最后将所有元素收集到一个集合中。默认情况下,Collectors.toSet()会创建一个HashSet,但规范并不能保证这一点。

If you want a guaranteed HashSet, you could use Collectors.toCollection(HashSet::new):

如果你想要一个保证的HashSet,你可以使用Collectors.toCollection(HashSet :: new):

Set<String> set = list.stream()
    .map(Object::toString)
    .collect(Collectors.toCollection(HashSet::new));

#3


1  

using Java 8 streams:

使用Java 8流:

set = list.stream().map(e -> e.toString()).collect(Collectors.toCollection(HashSet::new));

DEMO

or without using streams

或不使用流

for(Integer i : list) set.add(Integer.toString(i));

#1


0  

Assuming you are using an ArrayList as the instantiated form of List<>:

假设您使用ArrayList作为List <>的实例化形式:

    for (Integer value : list) {
        set.add(value.toString());
    }

This will iterate through your List and take each integer, convert it to a String, and add that value to your HashSet.

这将迭代您的List并获取每个整数,将其转换为String,并将该值添加到您的HashSet。

#2


1  

One way is to use streams:

一种方法是使用流:

Set<String> set = list.stream()
    .map(Object::toString)
    .collect(Collectors.toSet());

First, you stream the list. Then, each element is converted to string and finally all elements are collected to a set. By default, Collectors.toSet() creates a HashSet, though this is not guaranteed by the specification.

首先,您流式传输列表。然后,将每个元素转换为字符串,最后将所有元素收集到一个集合中。默认情况下,Collectors.toSet()会创建一个HashSet,但规范并不能保证这一点。

If you want a guaranteed HashSet, you could use Collectors.toCollection(HashSet::new):

如果你想要一个保证的HashSet,你可以使用Collectors.toCollection(HashSet :: new):

Set<String> set = list.stream()
    .map(Object::toString)
    .collect(Collectors.toCollection(HashSet::new));

#3


1  

using Java 8 streams:

使用Java 8流:

set = list.stream().map(e -> e.toString()).collect(Collectors.toCollection(HashSet::new));

DEMO

or without using streams

或不使用流

for(Integer i : list) set.add(Integer.toString(i));