Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion from ArrayList to Collection

I am having difficulty in this conversion. I don't know if there is a syntax error or this is not even possible.

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 don't understand what comes here)

By doing this, I intend to avoid UnsupportedOperationException. Any help appreciated, thanks!

Hey thank you all, i got it! This worked--
Solution:

List<? extends Contact> col = new ArrayList<Contact>(CONTACTS);
like image 763
Prince Avatar asked Feb 05 '12 03:02

Prince


People also ask

How do you turn an array into a collection?

To convert array-based data into Collection based we can use java. util. Arrays class. This class provides a static method asList(T… a) that converts the array into a Collection.


1 Answers

public interface List
extends Collection

You don't need to do anything. Or is there some particular operation you need that the ArrayList doesn't support?

like image 115
Kevin Avatar answered Sep 29 '22 12:09

Kevin