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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:22:43+00:00 2026-05-25T14:22:43+00:00

Consider the following question on storing values with duplicate keys: Suppose there is a

  • 0

Consider the following question on storing values with duplicate keys:

  1. Suppose there is a class Employee with name, sal and dob as attributes. I want to store the objects of Employee in a Map and the key would be the Employee name. The name can be duplicate.

  2. Also after adding 10 objects in the Map. I want to retrieve the 8th object that was entered.

This is one solution to add objects with duplicate keys but for the 2nd part of the question, this would not work since on displaying the map, all values with the same key will be displayed together.

How would we maintain the order in which the objects were added in this situation? Can we modify equals and hashcode methods to somehow add the elements and then later retrieve them in the order in which they were inserted?

  • 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-25T14:22:44+00:00Added an answer on May 25, 2026 at 2:22 pm

    The requirements are somehow contradictory. At one side several values should be possible for one key, at the other side only one value should be returned for a key. Additionaly, retrievals for a sequence should be possible. I see the nearest approximation in designing a dedicated data structure containing a hash map for fast access based on the name, and a list keeping the order of insertions. The access would be based on the overall sequence number or on the name plus index for the name. The implementation would be according the following lines:

    public class Employee {
        public String name;  public int sal;        
        public Employee() {name = ""; sal = 0;}
        public Employee(String name, int sal) {
            this.name = name; this.sal = sal;
        }
        @Override public String toString() {return "(" + name + "," + sal + ")";}
    }
    
    public class Team {
        private Map<String, ArrayList<Employee>> employees = 
                 new HashMap<String, ArrayList<Employee>>();
        private ArrayList<Employee> order = new ArrayList<Employee>();
    
        public void addEmployee(Employee e) {
            ArrayList<Employee> list = employees.get(e.name);     
            if (list == null) {
                list = new ArrayList<Employee>();
                employees.put(e.name, list); 
            } 
            list.add(e);
            order.add(e);
        }         
        public int getNumEmployees() {return order.size();}
        public Employee getEmployee(int n) {return order.get(n - 1);}       
        public int getNumEmployees(String name) {
            ArrayList<Employee> list = employees.get(name);
            return list == null ? 0 : list.size();
        }
        public Employee getEmployee(String name, int n) {
            ArrayList<Employee> list = employees.get(name);
            return list == null ? null : list.get(n - 1);
        }
    }
    
    // Test:
    Team team = new Team();
    team.addEmployee(new Employee("Bob", 11));
    team.addEmployee(new Employee("Bob", 12));
    team.addEmployee(new Employee("Eve", 13));
    team.addEmployee(new Employee("Eve", 14));
    
    System.out.println("Num all: " + team.getNumEmployees()); 
    System.out.println("3rd: " + team.getEmployee(3));
    System.out.println("Num Bobs: " + team.getNumEmployees("Bob")); 
    System.out.println("2nd Bob: " + team.getEmployee("Bob", 2));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Original Question Consider the following scenario: public abstract class Foo { public string Name
Please pardon me for a n00bish question. Please consider the following code: public class
For my question lets consider the following sample table data: ProductID    ProductName    Price   Category 1                Apple                 5.00       Fruits
Consider the following: print 3 ** 333; #Yields 7.6098802313206e+158 My question is simple: How
Question is should i use properties for my view controllers? Consider the following case:
This is a very basic question, so please bear with me. Consider the following
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following inheritance example: class A {...} class B extends A { ..
Consider the following short code snippet. namespace B { public class Foo { public

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.