Convert from ArrayList collection to collection

I am having difficulty with this conversion. I do not know if there is a syntax error, or is it even impossible.

I need to convert from -

private static final List<Contact> CONTACTS = Arrays.asList(
        new Contact("text1", "name1"),
        new Contact("text2", "name2"),
        new Contact("text3", "name3"));

To -

Collection c = new ArrayList(Arrays.asList(--?--))

-? - → (I do not understand what is happening here)

Thus, I intend to avoid an UnsupportedOperationException. Any help appreciated, thanks!

Hi everyone, I get it! It worked -
Solution:

List<? extends Contact> col = new ArrayList<Contact>(CONTACTS);
+5
source share
4 answers

I am updating it as the answer I was looking for. Thank you all for your answers!

List<? extends Contact> col = new ArrayList<Contact>(CONTACTS);
+1
source
public interface List
extends Collection

You don’t have to do anything . Or do you need some specific operation that ArrayListdoes not support?

+9
source

, , :

List<Contact> CONTACTS = new ArrayList<String>();
// fill CONTACTS
Collection<Contact> c = CONTACTS;

Collection - List, List, Collection.

+4

Doing this job:

private static final Collection<String> c = new ArrayList<String>(
                                                Arrays.asList("a", "b", "c"));

So I would suggest something like:

private static final Collection<Contact> = new ArrayList<Contact>(
                       Arrays.asList(new Contact("text1", "name1")
                                     new Contact("text2", "name2")));
+1
source

All Articles