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>>

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294

5 Answers5

11
Set<String> set = new HashSet<String>();
set.addAll(list1);
...
set.addAll(list5);
String[] str = set.toArray(new String[0]);
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
  • In this solution the order of elements is random – lukastymo Feb 20 '11 at 13:47
  • 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. – Rajat Gupta Feb 20 '11 at 14:12
  • Hey Johan, can you leave a comment on my performance concerns wrt to Set. I have added to my question. Thanks!!! – Rajat Gupta Feb 20 '11 at 14:23
  • 2
    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. – Johan Sjöberg Feb 20 '11 at 14:27
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.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
2

Create HashSet and call addAll with your Lists

Sergey Vedernikov
  • 7,609
  • 2
  • 25
  • 27
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]
*/
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.

lukastymo
  • 26,145
  • 14
  • 53
  • 66