Easiest Way to Convert a List to a Set in Java

Easiest way to convert a List to a Set in Java

Set<Foo> foo = new HashSet<Foo>(myList);

Converting list to set using java 8

The advantage is that it then becomes easier (or more accurately, requires fewer syntactical changes) to perform other functional operations at the same time.

Say later on you just wanted the even numbers in the set, you could then do that trivially with a filter:

Set<Integer> myset = mylist.stream()
.filter(p -> p%2==0)
.collect(Collectors.toSet());

If you'd done it the traditional way, then you'd either need to convert it to the above syntax, or write an additional loop to go through and pull out only the values that you wanted.

(This doesn't mean it's always better - there may be cases where you want to discourage someone changing the code at a later date to filter out values, in which case you could argue the traditional way is still preferable.)

Simplest way to convert a list to a set based on a boolean property?

For something this simple just write a loop and be done with it.

If this is a pattern you're going to implement over and over:

(untested so there may be a typo or two)

public interface Selector<T> {
public boolean select(T t); }

public <T> Set<T> grep(Collection<T> coll, Selector<T> sel) {
Set<T> result = new Set<T>;
for (T item : coll)
if (sel.select(T))
result.add(T);
return result;
}

And the invocation

List<SomeClasss> input = ...;
Set<SomeClass> output = grep(
input,
new Selector<SomeClass> {
public boolean select(SomeClass item) {
return some-boolean-function-of-item; // decide which items to keep here
}
}
);

Implementations of Selector know how to examine objects and decide if they should be kept.

Easiest way to convert a List to a ConcurrentSet? - Java

You can do a conversion like this.

public static <E> Set<E> toConcurrentSet(Collection<E> coll) {
Set<E> set = Collections.newSetFromMap(new ConcurrentHashMap<>());
set.addAll(coll);
return set;
}

Java 8 collections streaming - Convert list to Set, transforming result

You have to use flatMap

input.stream()
.map(a -> a.getIds())
.flatMap(ids -> ids.stream())
.collect(Collectors.toSet());

This will produce flat Set.

cast a List to a Collection

List<T> already implements Collection<T> - why would you need to create a new one?

Collection<T> collection = myList;

The error message is absolutely right - you can't directly instantiate an interface. If you want to create a copy of the existing list, you could use something like:

Collection<T> collection = new ArrayList<T>(myList);

Convert Set to List without creating new List

You can use the List.addAll() method. It accepts a Collection as an argument, and your set is a Collection.

List<String> mainList = new ArrayList<String>();
mainList.addAll(set);

EDIT: as respond to the edit of the question.

It is easy to see that if you want to have a Map with Lists as values, in order to have k different values, you need to create k different lists.

Thus: You cannot avoid creating these lists at all, the lists will have to be created.

Possible work around:

Declare your Map as a Map<String,Set> or Map<String,Collection> instead, and just insert your set.



Related Topics



Leave a reply



Submit