Convert Iterable to Set - Java java.util

Java examples for java.util:Iterable Convert

Description

Convert Iterable to Set

Demo Code


//package com.java2s;

import java.util.Iterator;

import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static <T> Set<T> toSet(final Iterable<T> iterable) {
        final Set<T> set = new TreeSet<T>();
        for (Iterator<T> i = iterable.iterator(); i.hasNext();) {
            set.add(i.next());/*from  w  ww  .j a v a  2  s  .c  o m*/
        }
        return set;
    }
}

Related Tutorials