0
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 ?

simi kaur
  • 1,257
  • 3
  • 15
  • 19

1 Answers1

3

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.

Community
  • 1
  • 1
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166