Forum Stats

  • 3,758,944 Users
  • 2,251,479 Discussions
  • 7,870,444 Comments

Discussions

Hashsets and string objects

Suri Chitti
Suri Chitti Member Posts: 1
edited Oct 23, 2018 7:49AM in Java User Groups Discussions

In my code, I am dealing with nested hashsets.  {{a,b},{b,c},{c,e}}


As you can see, inner hashsets contain strings.

At one point in my code, I do not know whether I am dealing with the inner hashset or the outer one.

I am trying to ascertain by using the following line of code: System.out.println(loopIterator3.next() instanceof String);

This line of code seems to generate an error:

prog.java:61: error: incompatible types: HashSet cannot be converted to String
                System.out.println(loopIterator3.next() instanceof String);

When loopIterator3 is indeed traversing an inner hashset, i would expect
it would be taking String values.  Why does the compiler think it is a hashset?
Moreover, why does the compiler think I am trying to convert?

The instanceof operator is used to check and not convert. Right?

I am baffled.  Any thoughts on how to resolve the problem or address my purpose?

Answers