Monday, August 31, 2015

Primitive arrays to collections in Java

An old way

static List<integer> toList(final int[] a) {
    final List<integer> l = new ArrayList<>(a.length);
    for (final int e : a) {
        l.add(e);
    }
    return l;
}

A new way

static Set<long> toList(final long[] a) {
    return Arrays.stream(a).boxed().collect(Collectors.toSet());
}

No comments:

Post a Comment