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

  • Home
  • SEARCH
  • 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 1023151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:34:19+00:00 2026-05-16T11:34:19+00:00

How to move a particular HashMap entry to the last position? For example, I

  • 0

How to move a particular HashMap entry to the last position?

For example, I have HashMap values like this:

HashMap<String,Integer> map = new HashMap<String,Integer>();

// map = {Not-Specified 1, test 2, testtest 3};

"Not-Specified" may come in any position. it may come first or in the middle of the map. But I want to move the "Not-Specified" to the last position.

How can I do that?

  • 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-16T11:34:20+00:00Added an answer on May 16, 2026 at 11:34 am

    To answer your question in one sentence:

    Per default, Maps don’t have a last entry, it’s not part of their contract.


    And a side note: it’s good practice to code against interfaces, not the implementation classes (see Effective Java by Joshua Bloch, Chapter 8, Item 52: Refer to objects by their interfaces).

    So your declaration should read:

    Map<String,Integer> map = new HashMap<String,Integer>();
    

    (All maps share a common contract, so the client need not know what kind of map it is, unless he specifies a sub interface with an extended contract).


    Possible Solutions

    Sorted Maps:

    There is a sub interface SortedMap that extends the map interface with order-based lookup methods and it has a sub interface NavigableMap that extends it even further. The standard implementation of this interface, TreeMap, allows you to sort entries either by natural ordering (if they implement the Comparable interface) or by a supplied Comparator.

    You can access the last entry through the lastEntry method:

    NavigableMap<String,Integer> map = new TreeMap<String, Integer>();
    // add some entries
    Entry<String, Integer> lastEntry = map.lastEntry();
    

    Linked maps:

    There is also the special case of LinkedHashMap, a HashMap implementation that stores the order in which keys are inserted. There is however no interface to back up this functionality, nor is there a direct way to access the last key. You can only do it through tricks such as using a List in between:

    Map<String,String> map = new LinkedHashMap<String, Integer>();
    // add some entries
    List<Entry<String,Integer>> entryList =
        new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
    Entry<String, Integer> lastEntry =
        entryList.get(entryList.size()-1);
    

    Proper Solution:

    Since you don’t control the insertion order, you should go with the NavigableMap interface, i.e. you would write a comparator that positions the Not-Specified entry last.

    Here is an example:

    final NavigableMap<String,Integer> map = 
            new TreeMap<String, Integer>(new Comparator<String>() {
        public int compare(final String o1, final String o2) {
            int result;
            if("Not-Specified".equals(o1)) {
                result=1;
            } else if("Not-Specified".equals(o2)) {
                result=-1;
            } else {
                result =o1.compareTo(o2);
            }
            return result;
        }
    
    });
    map.put("test", Integer.valueOf(2));
    map.put("Not-Specified", Integer.valueOf(1));
    map.put("testtest", Integer.valueOf(3));
    final Entry<String, Integer> lastEntry = map.lastEntry();
    System.out.println("Last key: "+lastEntry.getKey()
             + ", last value: "+lastEntry.getValue());
    

    Output:

    Last key: Not-Specified, last value: 1

    Solution using HashMap:

    If you must rely on HashMaps, there is still a solution, using a) a modified version of the above comparator, b) a List initialized with the Map’s entrySet and c) the Collections.sort() helper method:

        final Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("test", Integer.valueOf(2));
        map.put("Not-Specified", Integer.valueOf(1));
        map.put("testtest", Integer.valueOf(3));
    
        final List<Entry<String, Integer>> entries =
            new ArrayList<Entry<String, Integer>>(map.entrySet());
        Collections.sort(entries, new Comparator<Entry<String, Integer>>(){
    
            public int compareKeys(final String o1, final String o2){
                int result;
                if("Not-Specified".equals(o1)){
                    result = 1;
                } else if("Not-Specified".equals(o2)){
                    result = -1;
                } else{
                    result = o1.compareTo(o2);
                }
                return result;
            }
    
            @Override
            public int compare(final Entry<String, Integer> o1,
                final Entry<String, Integer> o2){
                return this.compareKeys(o1.getKey(), o2.getKey());
            }
    
        });
    
        final Entry<String, Integer> lastEntry =
            entries.get(entries.size() - 1);
        System.out.println("Last key: " + lastEntry.getKey() + ", last value: "
            + lastEntry.getValue());
    
    }
    

    Output:

    Last key: Not-Specified, last value: 1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.