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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:23:23+00:00 2026-06-12T00:23:23+00:00

Given a list of KeyValuePairs, where each pair has a getValue() method, what would

  • 0

Given a list of KeyValuePairs, where each pair has a getValue() method, what would be the fastest way to obtain a List (or Set) of unique Values?

All of the below produce acceptable result. u1 seems to be fastest over an expected list size (about 1000-2000 KVP)

Can we do better (faster)?

private static Set<String> u1(List<_KVPair> pairs) {
    Set<String> undefined = new HashSet<String>();

    for (_KVPair pair : pairs) {
        undefined.add(pair.getValue());
    }

    if (undefined.size() == 1) {
        return new HashSet<String>();
    }
    return undefined;
}

private static List<String> u2(List<_KVPair> pairs) {

    List<String> undefined = new ArrayList<String>();
    for (_KVPair pair : pairs) {
        if (!undefined.contains(pair.getValue())) {
            undefined.add(pair.getValue());
        }
    }

    return undefined;
}

private static List<String> u3(List<_KVPair> pairs) {

    List<String> undefined = new LinkedList<String>();

    Iterator<_KVPair> it = pairs.iterator();
    while (it.hasNext()) {
        String value = it.next().getValue();
        if (!undefined.contains(value)) {
            undefined.add(value);
        }
    }
    return undefined;
}

At about 3600 pairs, ‘u3’ wins. At about 1500 pairs, ‘u1’ wins

  • 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-12T00:23:24+00:00Added an answer on June 12, 2026 at 12:23 am

    First option should be faster. You could possibly make it even faster by sizing the set before using it. Typically, if you expect a small number of duplicates:

    Set<String> undefined = new HashSet<String>(pairs.size(), 1);
    

    Note that I used 1 for the load factor to prevent any resizing.

    Out of curiosity I ran a test (code below) – the results are (post compilation):

    Test 1 (note: takes a few minutes with warm up)

    size of original list = 3,000 with no duplicates:
    set: 8
    arraylist: 668
    linkedlist: 1166

    Test 2

    size of original list = 30,000 – all strings identical:
    set: 25
    arraylist: 11
    linkelist: 13

    That kind of makes sense:

    • when there are many duplicates, List#contains will run fairly fast as a duplicate will be found more quickly and the cost of allocating a large set + the hashing algorithm are penalising
    • when there are no or very few duplicates, the set wins, by a large margin.
    public class TestPerf {
    
        private static int NUM_RUN;
        private static Random r = new Random(System.currentTimeMillis());
        private static boolean random = false; //toggle to false for no duplicates in original list
    
    
        public static void main(String[] args) {
    
            List<String> list = new ArrayList<>();
    
            for (int i = 0; i < 30_000; i++) {
                list.add(getRandomString());
            }
    
            //warm up
            for (int i = 0; i < 10_000; i++) {
                method1(list);
                method2(list);
                method3(list);
            }
    
            NUM_RUN = 100;
            long sum = 0;
            long start = System.nanoTime();
            for (int i = 0; i < NUM_RUN; i++) {
                sum += method1(list);
            }
            long end = System.nanoTime();
            System.out.println("set: " + (end - start) / 1000000);
    
            sum = 0;
            start = System.nanoTime();
            for (int i = 0; i < NUM_RUN; i++) {
                sum += method2(list);
            }
            end = System.nanoTime();
            System.out.println("arraylist: " + (end - start) / 1000000);
    
            sum = 0;
            start = System.nanoTime();
            for (int i = 0; i < NUM_RUN; i++) {
                sum += method3(list);
            }
            end = System.nanoTime();
            System.out.println("linkelist: " + (end - start) / 1000000);
    
            System.out.println(sum);
        }
    
        private static int method1(final List<String> list) {
            Set<String> set = new HashSet<>(list.size(), 1);
            for (String s : list) {
                set.add(s);
            }
            return set.size();
        }
    
        private static int method2(final List<String> list) {
            List<String> undefined = new ArrayList<>();
            for (String s : list) {
                if (!undefined.contains(s)) {
                    undefined.add(s);
                }
            }
            return undefined.size();
        }
    
        private static int method3(final List<String> list) {
            List<String> undefined = new LinkedList<>();
    
            Iterator<String> it = list.iterator();
            while (it.hasNext()) {
                String value = it.next();
                if (!undefined.contains(value)) {
                    undefined.add(value);
                }
            }
            return undefined.size();
        }
    
        private static String getRandomString() {
            if (!random) {
                return "skdjhflkjrglajhsdkhkjqwhkdjahkshd";
            }
            int size = r.nextInt(100);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < size; i++) {
                char c = (char) ('a' + r.nextInt(27));
                sb.append(c);
            }
            System.out.println(sb);
            return sb.toString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a list of potential ID's is there a quick way using a single
I can easily write a predicate to get unique elements from a given list
Given an R list, I wish to find the index of a given list
The original code was somehow complex, i simplify it as: Given: list of class
Given a list a containing vectors of unequal length and a vector b containing
Given a List such as List(1, 2, 3, 4, 5, 6, 7) what is
Given a list like this: num = [1, 2, 3, 4, 5] There are
Given a list of elements like so: int[] ia = new int[] { -4,
Given a list of numbers, how does one find differences between every ( i
Given a list A = [1 2 3 4 5 6] Is there any

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.