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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:42:05+00:00 2026-05-13T23:42:05+00:00

Here is what I have- How I can include multiple keys and their values

  • 0

Here is what I have-

How I can include multiple keys and their values in comparison? Right now I am only using employeeId but I wanted to include departmentId and other in my comparison for sorting…

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class Tester {

    boolean flag = false ;


    public static void main(String args[]) {
        Tester tester = new Tester() ;
        tester.printValues() ;
    }

    public void printValues ()
    {

        List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>() ;
        HashMap<String,Object> map = new HashMap<String,Object>();


        map = new HashMap<String,Object>();
        map.put("employeeId", new Integer(1234)) ;
        map.put("departmentId", new Integer(110)) ;
        map.put("someFlag", "B") ;
        map.put("eventTypeId", new Integer(11)) ;
        map.put("startDate", new Date() ) ;
        map.put("endDate", new Date() ) ;
        list.add(map);


        map = new HashMap<String,Object>();
        map.put("employeeId", new Integer(456)) ;
        map.put("departmentId", new Integer(100)) ;
        map.put("someFlag", "B") ;
        map.put("eventTypeId", new Integer(11)) ;
        map.put("startDate", new Date() ) ;
        map.put("endDate", new Date() ) ;
        list.add(map);


        map = new HashMap<String,Object>();
        map.put("employeeId", new Integer(1234)) ;
        map.put("departmentId", new Integer(10)) ;
        map.put("someFlag", "B") ;
        map.put("eventTypeId", new Integer(17)) ;
        map.put("startDate", new Date() ) ;
        map.put("endDate", new Date() ) ;
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("employeeId", new Integer(1234)) ;
        map.put("departmentId", new Integer(99)) ;
        map.put("someFlag", "B") ;
        map.put("eventTypeId", new Integer(11)) ;
        map.put("startDate", new Date() ) ;
        map.put("endDate", new Date() ) ;
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("employeeId", new Integer(1234)) ;
        map.put("departmentId", new Integer(100)) ;
        map.put("someFlag", "B") ;
        map.put("eventTypeId", new Integer(11)) ;
        map.put("startDate", new Date() ) ;
        map.put("endDate", new Date() ) ;
        list.add(map);



        map = new HashMap<String,Object>();
        map.put("employeeId", new Integer(567)) ;
        map.put("departmentId", new Integer(200)) ;
        map.put("someFlag", "P") ;
        map.put("eventTypeId", new Integer(12)) ;
        map.put("startDate", new Date()  ) ;
        map.put("endDate", new Date() ) ;
        list.add(map);

        Collections.sort ( list , new HashMapComparator2 () ) ;

        for( int i = 0 ; i < list.size() ; i ++ ) {
            System.out.println(list.get(i));    
        }

        System.out.println("======================================");    


        flag = true ; // desc
        Collections.sort ( list , new HashMapComparator2 () ) ;

        for( int i = 0 ; i < list.size() ; i ++ ) {
            System.out.println(list.get(i));    
        }

    }

    public class HashMapComparator2 implements Comparator
    {
        public int compare ( Object object1 , Object object2 )
        {
            if ( flag == false )
            {


                Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "employeeId" ) ;
                Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "employeeId" ) ;

                return obj1Value.compareTo ( obj2Value ) ;
            }
            else
            {
                Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "employeeId" ) ;
                Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "employeeId" ) ;

                return obj2Value.compareTo ( obj1Value ) ;
            }
        }
    }


}
  • 1 1 Answer
  • 1 View
  • 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-13T23:42:06+00:00Added an answer on May 13, 2026 at 11:42 pm

    The easiest is using the CompareToBuilder from commons-lang.
    Your example would look like this:

    Map<String, Object> map1 = (Map<String, Object>) object1;
    Map<String, Object> map2 = (Map<String, Object>) object2;
    if ( flag == false ) {
        return new CompareToBuilder()
            .append(map1.get("employeeId"), map2.get("employeeId"))
            .append(map1.get("departmentId"), map2.get("departmentId"))
            .toComparison();
    }
    else {
        return new CompareToBuilder()
            .append(map2.get("employeeId"), map1.get("employeeId"))
            .append(map2.get("departmentId"), map1.get("departmentId"))
            .toComparison();
    }
    

    Or something like that. Anyway, I would definitely recommend that you use Genrics in your comparators, as suggested by Daniil.

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

Sidebar

Ask A Question

Stats

  • Questions 532k
  • Answers 532k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The thing is, a view doesn't point to one branch.… May 17, 2026 at 12:03 am
  • Editorial Team
    Editorial Team added an answer I just found the answer - referer_uri() returns the value… May 17, 2026 at 12:03 am
  • Editorial Team
    Editorial Team added an answer It returns $child. This is because $child is first added… May 17, 2026 at 12:03 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm currently using PHP to include multiple css (or js) files into a single
I am sorry about the amount of code here. I have tried to show
I have a PHP MVC framework with multiple 'applications' under this system the organization
I have multiple dl elements on a page. At the end of each one
In Qt's undo framework , you can have a stack of QUndoCommand instances. Each
Here's my scenario ... I will be receiving a stream of individual messages from
I am starting with Grails and want to have one page with multilanguage content.
I currently have a project where we are dealing with 30million+ keywords for PPC
I am working with a project and I have come across an obstacle and
Sorry for what seems like a silly question: But I've never, ever worked with

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.