Skip to Main Content

Is it incorrect work Collectors.toSet() in JDK 8?

skytraceJan 5 2014 — edited Jan 8 2014

If I run this code:

public class Main {

    public static void main(String[] args) {

        Collection<String> set = new ArrayList<String>();

        set.add("b");

        set.add("a");

        set.add("c");

        set.add("c");

        set.stream().collect(Collectors.toSet());

        System.out.println(set);

    }

}

I will get [b,a,c,c]. But if I right, previous code should be short form of expression:

public class Main {

    public static void main(String[] args) {

        Collection<String> set = new ArrayList<>();

        set.add("b");

        set.add("a");

        set.add("c");

        set.add("c");

        Collection<String> set2 = new HashSet<>(set);

        System.out.println(set2);

    }

}

And here, I got [a,b,c]

So, in example 1. Is it incorrect result or I do something wrong?

Comments
Post Details
Added on Jan 5 2014
5 comments
2,770 views