Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623610
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:24:18+00:00 2026-05-18T23:24:18+00:00

Background Create a Map that can be sorted by value. Problem The code executes

  • 0

Background

Create a Map that can be sorted by value.

Problem

The code executes as expected, but does not compile cleanly:

http://pastebin.com/bWhbHQmT

public class SortableValueMap<K, V> extends LinkedHashMap<K, V> {
  ...
  public void sortByValue() {
      ...
      Collections.sort( list, new Comparator<Map.Entry>() {
          public int compare( Map.Entry entry1, Map.Entry entry2 ) {
            return ((Comparable)entry1.getValue()).compareTo( entry2.getValue() );
          }
      });
  ...

The syntax for passing Comparable as a generic parameter along to the Map.Entry<K, V> (where V must be Comparable?) — so that the (Comparable) typecast shown in the warning can be dropped — eludes me.

Warning

Compiler’s cantankerous complaint:

SortableValueMap.java:24: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable

   return ((Comparable)entry1.getValue()).compareTo( entry2.getValue() );

Question

How can the code be changed to compile without any warnings (without suppressing them while compiling with -Xlint:unchecked)?

Related

  • TreeMap sort by value
  • Sort a Map<Key, Value> by values (Java)
  • http://paaloliver.wordpress.com/2006/01/24/sorting-maps-in-java/

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:24:18+00:00Added an answer on May 18, 2026 at 11:24 pm

    Declare the V type to extend the Comparable<V> interface. That way, you can remove the cast of the Map.Entry objects down to (Comparable) and use the inferred type instead:

    public class SortableValueMap<K, V extends Comparable<V>>
                 extends LinkedHashMap<K, V> {
    

    ….

        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            public int compare(Map.Entry<K, V> entry1, Map.Entry<K, V> entry2) {
                return entry1.getValue().compareTo(entry2.getValue());
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm working with a multi-threaded program (using pthreads) that currently create a background thread
Background: I'm trying to create a pure D language implementation of functionality that's roughly
Background I have a web app that will create an image from user input.
My background idea is to create a software layer that merges all the things
Background: I'm using MEF to help compose an Excel 2007 Add-In (VSTO) that can
What is the preferred way to create a background task for a Rails application?
Background I am trying to create a copy of a business object I have
Background In Visual Studio 2008 Create a new Windows Forms Application, this will create
Background I have been asked by a client to create a picture of the
I want to create some up and down buttons using the standard button background

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.