• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

new Comparator<K>() isn't Comparator an interface

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
this is probably not a new question . This is someone's code that I am trying to grasp.
I do not get how is it possible to use
.

1) Comparator is an interface and I should not be able to create an instance of interface?
2) why should it return 1

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the comments int hat code:
//START of anonymous class

That bit in the curly braces is defining an anonymous class that implements the Comparator interface's method.

If you look at the files produced when it's compiled you'll see a <YourClassName>.class file, which is the compiled version of your class, but also a <YourClassName>$1.class, which is the anonymous class.
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch, Ines. Did Dave's explanation make sense to you? This is an idiom you will probably see a lot of in old code, and it is confusing, so it's a good thing to fully understand it.
 
Marshal
Posts: 80099
413
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ines ilovebears wrote: . . . This is someone's code that I am trying to grasp. . . .

So at least you are not to blame for this:-

. . .

 
Ines Mew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell, Dave, Knute

Thanks for your answers..I am still not clear on the whole topic of anonymous classes and generics but I feel like I am getting some progress. On a scale from 1 to 10 I went
from 0 to 2.. or maybe 1

How about this line:
1) new TreeMap(Comparator<T>) it creates an empty map ->this new map is ORDERED based on the rules I specified in
my Comparator -> then I take my original UNORDERED map and put it into this new map. Correct?

2) on line 56

I am trying to add another entry to my ORDERED map. I get NPE. Why?

 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic