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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:19:34+00:00 2026-06-09T01:19:34+00:00

Say, you’re given a List<KVPair> , where each KVPair has a String key ,

  • 0

Say, you’re given a List<KVPair>, where each KVPair has a String key, String Value and .equals() method that does things right.

How would you confirm that each of the elements in the list is like the other, or see if at least one is not?

In other words, if we have

KVPair kvp1 = new KVPAir("key", "value");
KVPair kvp2 = new KVPAir("key", "value");

List<KVPair> l = new ArrayList<KVPair>();
l.add(kvp1);
l.add(kvp2);

The one approach i can think of is, sort the list first and iterate until next is not like previous.

Is there a simpler, cleaner way to find the same?

  • 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-09T01:19:37+00:00Added an answer on June 9, 2026 at 1:19 am

    Here is a generic solution:

    Don’t forget to do the empty check. First time you add element to the set, it will always be added

    public static boolean isListOfSameElements(List<? extends Object> l) {
        Set<Object> set = new HashSet<Object>(l.size());
        for (Object o : l) {
            if (set.isEmpty()) {
                set.add(o);
            } else {
                if (set.add(o)) {
                    return false;
                }
            }
        }
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
Say you have the collection of key/value pair. for e.g. : - dictionary<string,string> myObjectToBeCreated
Say that you have a XHTML document in English but it has accented characters
Say I have an ASMX web service, MyService. The service has a method, MyMethod.
Say I have a SSI script that uses exec, or a PHP script that
Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten
Say that I have a singleton class (Downloader) responsible for downloading and persisting files.
Say I have a LINQ-to-XML query that generates an anonymous type like this: var
Say my web app has two spring cron jobs scheduled to run every minute/hour.
Say, the field of Race in my table has values like W,A (white and

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.