2

I'd like to create synchronized ArrayList in JAVA. I've used this code:

static ArrayList<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());

But NetBeans yells:

incompatible types:

required: ArrayList found: List

Any help possible?

5 Answers 5

5

Collections.synchronizedList() returns a List, not an ArrayList. Declare the user list as such:

static List<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());
Sign up to request clarification or add additional context in comments.

3 Comments

Not it says that type "List" does not take any parameters.
@user2721537 It certainly does. Did you import some other List than java.util.List?
indeed: I had java.awt.List Thx ;-)
4
Collections.synchronizedList()

returns a List. Use something like:

static List<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());

Every ArrayList is a List but every List is not an ArrayList.

Comments

3
static List<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());

Should fix the problem.

Comments

0

Collections.synchronizedList returns a new SynchronizedList<User> instance, cast back to List<User>. If your declaration goes

static List<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());

, it will be alright.

Comments

0

List<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());

Worked for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.