12

I Need a java function which converts from java.util.List to java.util.Set and vice versa, independent of type of objects in the List/Set.

Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
Arjun
  • 6,501
  • 10
  • 32
  • 34

4 Answers4

23

Like List.addAll and Set.addAll?

Erik
  • 88,732
  • 13
  • 198
  • 189
8

Most of the class of the java collection framework have a constructor that take a collection of element as a parameter. You should use your prefered implementation ton do the conversion for exameple (with HashSet and ArrayList):

public class MyCollecUtils {

    public static <E> Set<E> toSet(List<E> l) {
        return new HashSet<E>(l);
    }

    public static <E> List<E> toSet(Set<E> s) {
        return new ArrayList<E>(s);
    }
}
Nicolas
  • 24,509
  • 5
  • 60
  • 66
  • 1
    As you already mentioned: most take a collection as a parameter to their c-tor. So why not use that too: `toSet(Collection c)` and `toSet(Collection c)`? – Bart Kiers Mar 16 '11 at 09:10
  • In a general case, actually, you won't even build function for it, just use the constructors. As we are in a specific case, where the question is conversion from List to Set and vice-versa, I prefer to specify the kind of collection attended. – Nicolas Mar 16 '11 at 09:13
  • This code is so simple and basic that maybe it doesn't deserve a function at all. Util.toSet(col) is not really better than new HashSet(col). You can then choose the actual implementation of your class, and can convert from any collection type to any other type. – Nicolas Bousquet Mar 16 '11 at 09:18
  • Nicolas: it is exactly what i've written. – Nicolas Mar 16 '11 at 09:21
6
public static <E> Set<E> getSetForList(List<E> lst){
  return new HashSet<E>(lst);//assuming you don't care for duplicate entry scenario :)
}

public static <E> List<E> getListForSet(Set<E> set){
  return new ArrayList<E>(set);// You can select any implementation of List depending on your scenario
}
Laurent Pireyn
  • 6,735
  • 1
  • 29
  • 39
jmj
  • 237,923
  • 42
  • 401
  • 438
4

Instead of one function you can have two function to implement this functionality:

// Set to List
public List setToList(Set set) {
    return new ArrayList(set);
}

// List to Set
public Set listToSet(List list) {
    return new HashSet(list);
}

In a single function:

public Collection convertSetList(Collection obj) {
    if (obj instanceof java.util.List) {
         return new HashSet((List)obj);
    } else if(obj instanceof java.util.Set) {
         return new ArrayList((Set)obj);
    }    
    return null;
}

Example: (updated)

public class Main {
    public static void main(String[] args) {
        Set s = new HashSet();
        List l = new ArrayList();

        s.add("1");s.add("2");s.add("3");
        l.add("a");l.add("b");l.add("c");

        Collection c1 = convertSetList(s);
        Collection c2 = convertSetList(l);

        System.out.println("c1 type is : "+ c1.getClass());
        System.out.println("c2 type is : "+ c2.getClass());        
    }

    public static Collection convertSetList(Collection obj) {
        if (obj instanceof java.util.List) {
            System.out.println("List!");
            return (Set)new HashSet((List) obj);
        } else if (obj instanceof java.util.Set) {
            System.out.println("Set!");
            return (List)new ArrayList((Set) obj);
        } else {
            System.out.println("Unknow type!");
            return null;
        }
    }
}
Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84
  • Thanks, I removed my -1, but I still don't think it's wise to do it like you suggest in `convertSetList`: since neither a `List` or `Set` is returned (but a `Collection`), there will need to be some casting if the reference to either the `List` or `Set` is needed. Also, when something other than a `List` or `Set` is passed, `null` is returned (not good, IMO). Lastly there still is no use of generics in your code, making it Java 1.4.2 code (from ages ago!) :) – Bart Kiers Mar 16 '11 at 09:26
  • the above piece of code working for me. as per the user's requirement he wants to a function. Obviously he needs to cast or do some validation. thats why written like that. correct me if am wrong. thanks. – Mohamed Saligh Mar 16 '11 at 09:44