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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:50:58+00:00 2026-06-16T18:50:58+00:00

I have two LinkedHashMap<String, MyType> where keys can be common (say key from first

  • 0

I have two LinkedHashMap<String, MyType> where keys can be common (say key from first map can be “key” and key from second map can be also “key”. I need to compare this two maps and when keys are equal I want create new object where firsMapEntry.getValue() secondMapEntry.getValue() are attributes of new object [ new MyObject(firsMapEntry.getValue(), secondMapEntry.getValue()) ]. Maps are in Collection so I iterate through collections.

Here’s my source:

firstIterator = firsCollection.iterator();
secondIterator = secondCollection.iterator();

    while (firstIterator.hasNext() && secondIterator.hasNext()) {
        Map<String, MyType1> firsMap = firstIterator.next();
        Map<String, List<MyType2>> secondMap = secondIterator
                .next();

        firstMapIterator = firstMap.entrySet().iterator();
        secondMapIterator = secondMap.entrySet().iterator();

        while (firstMapIterator.hasNext() && secondMapIterator.hasNext()) {
            Map.Entry<String, MyType1> firstEntry = ((Map.Entry<String, MyType1>) firstMapIterator.next());
            Map.Entry<String, List<MyType2>> secondEntry = ((Map.Entry<String, List<MyType2>>) secondMapIterator.next());


           // Here I was trying to compare keys but unsuccessfuly

          // if keys are equal create new object

        My Object object = new MyObject(firsEntry.getValue(),
                    secondEntry.getValue());
                      Map<String, MyObject> map = new LinkedHashMap<String,MyObject(); map.put("key", object);

        }
    }

Return type of method is List<Map<String, MyObject>>.
I don’t know if my method is effecient, but how to compare two keys and create new object from values. Every help will be appreciated. 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-06-16T18:51:00+00:00Added an answer on June 16, 2026 at 6:51 pm

    Map.Entry has a method called getKey() , use this to get the key.

    Since your key is a String use , Object.equals() to compare them , do not use ‘==’ ,so:

        if(firstEntry .getKey().equals(secondEntry .getKey())){
        //they are equal
        }
    else{
        //they are different
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two columns say Main and Sub . (they can be of same
I have two tables, lets say table1 and table2 with common columns, id and
Ive come across a nice problem recently, I have a Map LinkedHashMap<String, List<MyCustomObject>> I
you have two LinkedHashMaps Map m1 = new LinkedHashMap(); m1.put(1,One); m1.put(3,Three); Map m2 =
I have two classes (MVC view model) which inherits from one abstract base class.
Have two tables say ABC and XYZ and contain one column which data will
I have two tables, say teacher and student. I want to build a third
I have two date fields that I need to join on. The first is
I have two models, one called Notes and one called Comments. Comments can be
I have two columns, the first column will have the name of a object,

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.