Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I've seen the discussion at thisthis question regarding how a class that implements from an interface would be instantiated. In my case, I'm writing a very small program in Java that uses an instance of TreeMap, and according to everyone's opinion there, it should be instantiated like:

Map<X> map = new TreeMap<X>();

In my program, I'm calling the function map.pollFirstEntry(), which is not declared in the Map interface (and a couple others who are present in the Map interface too). I've managed to do this by casting to a TreeMap<X> everywhere I call this method like:

someEntry = ((TreeMap<X>) map).pollFirstEntry();

I understand the advantages of the initialization guidelines as described above for large programs, however for a very small program where this object would not be passed to other methods, I would think it is unnecessary. Still, I'm writing this sample code as part of a job application, and I don't want my code to look badly nor cluttered. What would be the most elegant solution?

EDIT: I would like to point out that I'm more interested in the broad good coding practices instead of the application of the specific function TreeMap. As some of the answers have already pointed out (and I've marked as answered the first one to do so), the higher abstraction level possible should be used, without losing functionality.

I've seen the discussion at this question regarding how a class that implements from an interface would be instantiated. In my case, I'm writing a very small program in Java that uses an instance of TreeMap, and according to everyone's opinion there, it should be instantiated like:

Map<X> map = new TreeMap<X>();

In my program, I'm calling the function map.pollFirstEntry(), which is not declared in the Map interface (and a couple others who are present in the Map interface too). I've managed to do this by casting to a TreeMap<X> everywhere I call this method like:

someEntry = ((TreeMap<X>) map).pollFirstEntry();

I understand the advantages of the initialization guidelines as described above for large programs, however for a very small program where this object would not be passed to other methods, I would think it is unnecessary. Still, I'm writing this sample code as part of a job application, and I don't want my code to look badly nor cluttered. What would be the most elegant solution?

EDIT: I would like to point out that I'm more interested in the broad good coding practices instead of the application of the specific function TreeMap. As some of the answers have already pointed out (and I've marked as answered the first one to do so), the higher abstraction level possible should be used, without losing functionality.

I've seen the discussion at this question regarding how a class that implements from an interface would be instantiated. In my case, I'm writing a very small program in Java that uses an instance of TreeMap, and according to everyone's opinion there, it should be instantiated like:

Map<X> map = new TreeMap<X>();

In my program, I'm calling the function map.pollFirstEntry(), which is not declared in the Map interface (and a couple others who are present in the Map interface too). I've managed to do this by casting to a TreeMap<X> everywhere I call this method like:

someEntry = ((TreeMap<X>) map).pollFirstEntry();

I understand the advantages of the initialization guidelines as described above for large programs, however for a very small program where this object would not be passed to other methods, I would think it is unnecessary. Still, I'm writing this sample code as part of a job application, and I don't want my code to look badly nor cluttered. What would be the most elegant solution?

EDIT: I would like to point out that I'm more interested in the broad good coding practices instead of the application of the specific function TreeMap. As some of the answers have already pointed out (and I've marked as answered the first one to do so), the higher abstraction level possible should be used, without losing functionality.

Tweeted twitter.com/#!/StackProgrammer/status/598522708201218048
removed request for references
Source Link
jimijazz
  • 241
  • 1
  • 6

I've seen the discussion at this question regarding how a class that implements from an interface would be instantiated. In my case, I'm writing a very small program in Java that uses an instance of TreeMap, and according to everyone's opinion there, it should be instantiated like:

Map<X> map = new TreeMap<X>();

In my program, I'm calling the function map.pollFirstEntry(), which is not declared in the Map interface (and a couple others who are present in the Map interface too). I've managed to do this by casting to a TreeMap<X> everywhere I call this method like:

someEntry = ((TreeMap<X>) map).pollFirstEntry();

I understand the advantages of the initialization guidelines as described above for large programs, however for a very small program where this object would not be passed to other methods, I would think it is unnecessary. Still, I'm writing this sample code as part of a job application, and I don't want my code to look badly nor cluttered. What would be the most elegant solution?

EDIT: I would like to point out that I'm more interested in the broad good coding practices instead of the application of the specific function TreeMap. As some of the answers have already pointed out (and I've marked as answered the first one to do so), the higher abstraction level possible should be used, without losing functionality. This makes sense, so I would like to know if there are any references to this within the documentation, so the answers are not to be judged as "opinion-based".

I've seen the discussion at this question regarding how a class that implements from an interface would be instantiated. In my case, I'm writing a very small program in Java that uses an instance of TreeMap, and according to everyone's opinion there, it should be instantiated like:

Map<X> map = new TreeMap<X>();

In my program, I'm calling the function map.pollFirstEntry(), which is not declared in the Map interface (and a couple others who are present in the Map interface too). I've managed to do this by casting to a TreeMap<X> everywhere I call this method like:

someEntry = ((TreeMap<X>) map).pollFirstEntry();

I understand the advantages of the initialization guidelines as described above for large programs, however for a very small program where this object would not be passed to other methods, I would think it is unnecessary. Still, I'm writing this sample code as part of a job application, and I don't want my code to look badly nor cluttered. What would be the most elegant solution?

EDIT: I would like to point out that I'm more interested in the broad good coding practices instead of the application of the specific function TreeMap. As some of the answers have already pointed out, the higher abstraction level possible should be used, without losing functionality. This makes sense, so I would like to know if there are any references to this within the documentation, so the answers are not to be judged as "opinion-based".

I've seen the discussion at this question regarding how a class that implements from an interface would be instantiated. In my case, I'm writing a very small program in Java that uses an instance of TreeMap, and according to everyone's opinion there, it should be instantiated like:

Map<X> map = new TreeMap<X>();

In my program, I'm calling the function map.pollFirstEntry(), which is not declared in the Map interface (and a couple others who are present in the Map interface too). I've managed to do this by casting to a TreeMap<X> everywhere I call this method like:

someEntry = ((TreeMap<X>) map).pollFirstEntry();

I understand the advantages of the initialization guidelines as described above for large programs, however for a very small program where this object would not be passed to other methods, I would think it is unnecessary. Still, I'm writing this sample code as part of a job application, and I don't want my code to look badly nor cluttered. What would be the most elegant solution?

EDIT: I would like to point out that I'm more interested in the broad good coding practices instead of the application of the specific function TreeMap. As some of the answers have already pointed out (and I've marked as answered the first one to do so), the higher abstraction level possible should be used, without losing functionality.

added 451 characters in body
Source Link
jimijazz
  • 241
  • 1
  • 6

I've seen the discussion at this question regarding how a class that implements from an interface would be instantiated. In my case, I'm writing a very small program in Java that uses an instance of TreeMap, and according to everyone's opinion there, it should be instantiated like:

Map<X> map = new TreeMap<X>();

In my program, I'm calling the function map.pollFirstEntry(), which is not declared in the Map interface (and a couple others who are present in the Map interface too). I've managed to do this by casting to a TreeMap<X> everywhere I call this method like:

someEntry = ((TreeMap<X>) map).pollFirstEntry();

I understand the advantages of the initialization guidelines as described above for large programs, however for a very small program where this object would not be passed to other methods, I would think it is unnecessary. Still, I'm writing this sample code as part of a job application, and I don't want my code to look badly nor cluttered. What would be the most elegant solution?

EDIT: I would like to point out that I'm more interested in the broad good coding practices instead of the application of the specific function TreeMap. As some of the answers have already pointed out, the higher abstraction level possible should be used, without losing functionality. This makes sense, so I would like to know if there are any references to this within the documentation, so the answers are not to be judged as "opinion-based".

I've seen the discussion at this question regarding how a class that implements from an interface would be instantiated. In my case, I'm writing a very small program in Java that uses an instance of TreeMap, and according to everyone's opinion there, it should be instantiated like:

Map<X> map = new TreeMap<X>();

In my program, I'm calling the function map.pollFirstEntry(), which is not declared in the Map interface (and a couple others who are present in the Map interface too). I've managed to do this by casting to a TreeMap<X> everywhere I call this method like:

someEntry = ((TreeMap<X>) map).pollFirstEntry();

I understand the advantages of the initialization guidelines as described above for large programs, however for a very small program where this object would not be passed to other methods, I would think it is unnecessary. Still, I'm writing this sample code as part of a job application, and I don't want my code to look badly nor cluttered. What would be the most elegant solution?

I've seen the discussion at this question regarding how a class that implements from an interface would be instantiated. In my case, I'm writing a very small program in Java that uses an instance of TreeMap, and according to everyone's opinion there, it should be instantiated like:

Map<X> map = new TreeMap<X>();

In my program, I'm calling the function map.pollFirstEntry(), which is not declared in the Map interface (and a couple others who are present in the Map interface too). I've managed to do this by casting to a TreeMap<X> everywhere I call this method like:

someEntry = ((TreeMap<X>) map).pollFirstEntry();

I understand the advantages of the initialization guidelines as described above for large programs, however for a very small program where this object would not be passed to other methods, I would think it is unnecessary. Still, I'm writing this sample code as part of a job application, and I don't want my code to look badly nor cluttered. What would be the most elegant solution?

EDIT: I would like to point out that I'm more interested in the broad good coding practices instead of the application of the specific function TreeMap. As some of the answers have already pointed out, the higher abstraction level possible should be used, without losing functionality. This makes sense, so I would like to know if there are any references to this within the documentation, so the answers are not to be judged as "opinion-based".

Post Migrated Here from stackoverflow.com (revisions)
Source Link
jimijazz
  • 241
  • 1
  • 6
Loading