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

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.