3

I have five lists List<String>. I want to get a single array which contains all elements from those lists, eliminating any repartitions. What could be the best way to do this?

`Edit: Can someone also comment on the performance of HashSet in comparision to List. I am concerned about performance, as I am doing this job while calculating the data to be displayed on a webpage. And no. of elements in the set would be high around 300-400, what parameters whould be suitable for the Set?

My elements in set, would be of this type: <HColumn<String, String>>

5 Answers 5

11
Set<String> set = new HashSet<String>();
set.addAll(list1);
...
set.addAll(list5);
String[] str = set.toArray(new String[0]);
Sign up to request clarification or add additional context in comments.

4 Comments

In this solution the order of elements is random
However the thing that worries me when movng from List to Set is that removal of duplicates is not too costly in terms of performance. I need superfast operations since I am using this to generate the data for high traffic webpages.
Hey Johan, can you leave a comment on my performance concerns wrt to Set. I have added to my question. Thanks!!!
Removing duplicates from one list is expensive; doing it in many lists even more. This solution does offers a performant way to remove duplicates. Unless your lists spans > 10k elements I wouldn't worry about performance issues.
3

You can add the list contents into a Set.

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

You can then get an array back by calling Set.toArray().

A standard set won't preserve order, but duplicates will be eliminated.

Comments

2

Create HashSet and call addAll with your Lists

Comments

1

Same question was asked to me in interview, but combine two vectors without repeating common object and without using collection framework methods.

package com.pra.sss;
/*
 * Vector v1 and V2 contain some common objects
 * Create vector V3 which will contain all V1 and V2 
 * and common objects only once 
 * without using colletion framework functions 
 */import java.util.Vector;

public class VectorTest {
public static void main(String[] args) {
   Vector<String> v1 = new Vector<String>();
   Vector<String> v2 = new Vector<String>();
   Vector<String> v3 = new Vector<String>();
   /*Vector v1*/
   v1.add("A");
   v1.add("B");
   v1.add("C");
   v1.add("D");
   /*Vector v2*/
   v2.add("W");
   v2.add("B");
   v2.add("C");
   v2.add("Z");

   System.out.println("V1"+v1);
   System.out.println("V2"+v2);

   for(String ss : v1) {
       v3.add(ss);
   }

   for (int i = 0; i < v1.size(); i++) {
       for (int j = i; j < v2.size(); j++) {
           if(v1.get(i).equals(v2.get(j))) {
               break;
           }
           else{
               v3.add(v2.get(j));
               break;
           }
    }
  }
   System.out.println("v3:"+v3);
}
}

/*
output
V1[A, B, C, D]
V2[W, B, C, Z]
v3:[A, B, C, D, W, Z]
*/

Comments

0

The best way to merge lists depends if you want have the same order of elements or not. i.d. elements 1st list before elements 2nd.

In @Johan Sjöberg solution it is used HashSet. The elements are returned in no particular order. If you want save the same order better use collection with predictable iteration order e.g. LinkedHashSet.

1 Comment

in my case order is not important.. Thanks anyways!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.