• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to see why declaring static final variable is NOT enough for singleton pattern.

For that I have created following 3 classes:



Running this code results in following exception.
Exception in thread "Thread-620" java.lang.ClassCastException: SingletonCheck cannot be cast to java.lang.Comparable
at java.util.concurrent.ConcurrentSkipListMap.comparable(ConcurrentSkipListMap.java:621)
at java.util.concurrent.ConcurrentSkipListMap.doPut(ConcurrentSkipListMap.java:862)
at java.util.concurrent.ConcurrentSkipListMap.putIfAbsent(ConcurrentSkipListMap.java:1893)
at java.util.concurrent.ConcurrentSkipListSet.add(ConcurrentSkipListSet.java:202)
at test3.ThreadRunner.run(ThreadRunner.java:15)
at java.lang.Thread.run(Thread.java:619)


Could someone help me in solving this error please?
Do I need to implement equals() method?

Regards,
Patrio
 
Patrio Jensen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing from ConcurrentSkipListSet to CopyOnWriteArraySet did the trick. Need to see why it's failing for ConcurrentSkipListSet.
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the error message should be making that pretty clear, and you can confirm this by going to the Javadocs: objects put into a ConcurrentSkipListSet have to implement Comparable. TreeMap, TreeSet, and other sorted collections are the same way. You can generally supply a Comparator instance instead, if your class doesn't want to implement Comparable.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!