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 8080131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:19:27+00:00 2026-06-05T16:19:27+00:00

Unlike HashMap, order matters in LinkedHashMap. And the order here is insertion order. Let

  • 0

Unlike HashMap, order matters in LinkedHashMap. And the order here is insertion order.

Let say I have a LinkedHashMap like the following (ordered from the top to bottom, left part is key, right part is value):

1:”One”

2:”Two”

3:”Three”

4:”Four”

Then I have a list of keys, which contains, let’s say, (3,1).

What I want to do is to loop through the LinkedHashMap in order and pick out the entries whose key is in the list.

So the result I want to have is (1 is still before 3 because that’s the order before filtering):

1:”One”

3:”Three”

Here is my code:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SelectCertainEntriesFromLinkedHashMap {
    public static void main(String args[]) {
    Map<Integer,String> linkedHashMap = new LinkedHashMap<Integer,String>();
    linkedHashMap.put(1, "One");
    linkedHashMap.put(2, "Twe");
    linkedHashMap.put(3, "Three");
    linkedHashMap.put(4, "Four");

    List<Integer> list = new ArrayList<Integer>();
    list.add(3);
    list.add(1);

    Map<Integer,String> selectedlinkedHashMap = new LinkedHashMap<Integer,String>();

    //will this iterator iterate the keys in the order of the map (1, 2, 3, 4)? Or just random order?
    Iterator<Integer> itr = linkedHashMap.keySet().iterator();
    while(itr.hasNext()) {
        Integer key = itr.next();
        if (list.contains(key)) {
            selectedlinkedHashMap.put(key, linkedHashMap.get(key));
            System.out.println(key + ":" + linkedHashMap.get(key));
        }
    }
}
}

The above code return the result I like. But I am not sure if it is guaranteed.

1:”One”

3:”Three”

The question is:
Iterator itr = linkedHashMap.keySet().iterator();
The above line will get a iterator from a set and set is not ordered. So will this cause the keys in random order? if yes, I can’t keep the original order (not guaranteed) of my map after filtering….
Could anybody help me with this?

  • 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-06-05T16:19:28+00:00Added an answer on June 5, 2026 at 4:19 pm

    The iterator returned from keySet().iterator() should return an ordered Set. Documentation from the Map API:

    The Map interface provides three collection views, which allow a map's contents to be 
    viewed as a set of keys, collection of values, or set of key-value mappings. The order of 
    a map is defined as the order in which the iterators on the map's collection views return   
    their elements. Some map implementations, like the TreeMap class, make specific guarantees  
    as to their order; others, like the HashMap class, do not.
    

    So in the LinkedHashMap case I interpret this as saying the iterator will return an ordered Set. It’s true the LinkedHashMap API is not explicit about this but you could just try it out and observe your output.

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

Sidebar

Related Questions

I have a model not unlike the following: class Bike(models.Model): made_at = models.ForeignKey(Factory) added_on
I'm using Git for version control and unlike SVN I have not come across
Unlike the iPhone simulator, the Android emulator doesn't look like an Android device. If
Unlike Java, Perl uses reference count for garbage collection. I have tried searching some
I have 2 unlike tables and a large set of subqueries that have a
A web application has no make file unlike C++ or anything like that. However,
First I need to programmatically get tags (unlike what is done here on stackoverflow)
Unlike in java why c# does not have a supertype of Number for Floats,
I have an app using ListView not unlike Explorer does, but when I set
I have a programmatically generated dijit.form.Select. Unlike most other widgets, the Selects do not

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.