simi kaur
simi kaur

Reputation: 1277

Can not convert from element type Object to Session

private static final Set sessions = Collections.synchronizedSet(new HashSet());

I am storing sessions in a Set type reference variable called sessions s shown above.

Now, I want to iterate over these:

for(Session s : sessions){}

However, I get a Type Mismatch error at it that says

Can not convert from element type Object to Session

How do I fix this ?

Upvotes: 0

Views: 124

Answers (1)

Boris the Spider
Boris the Spider

Reputation: 61158

TL;DR: DON'T USE RAWTYPES

You are using a raw Set, this means that the compiler can only know that your Set contains Object.

You need to specify the generic type of your collection:

Set<Session> sessions = Collections.synchronizedSet(new HashSet<>());

For more information, read this.

Upvotes: 3

Related Questions