Into the Unknown

A blog about a lesser known Eclipse Collections method

Photo by Mario Dobelmann on Unsplash

What am I getting into now?

Eclipse Collections has a method named that was added in the 8.0 release. The signature of is defined as follows on the interface.

The method into on RichIterable

This method can be used to transfer the contents of any “into” a target collection. The following examples are collections that are “unknown” as Eclipse Collections converter methods, but can be used just fine with .

public void intoTheUnknown()
var integers = Interval.oneTo(10);

var into1 = integers.into(new CopyOnWriteArrayList<>());
Assertions.assertEquals(integers, into1);

var into2 = integers.into(new CopyOnWriteArraySet<>());
Assertions.assertEquals(integers.toSet(), into2);

var into3 = integers.into(new ArrayDeque<>());

var into4 = integers.into(new LinkedList<>());
Assertions.assertEquals(integers, into4);

var into5 = integers.into(new Stack<>());
Assertions.assertEquals(integers, into5);

var checkedList =
Collections.checkedList(new ArrayList<>(), Integer.class);
var into6 = integers.into(checkedList);
Assertions.assertEquals(integers, into6);

var checkedSet =
Collections.checkedSet(new HashSet<>(), Integer.class);
var into7 = integers.into(checkedSet);
Assertions.assertEquals(integers.toSet(), into7);

The method is also useful when you want to drain multiple source collections into the same target collection.

public void intoList()
var target = Interval.oneTo(3).into(new ArrayList<>());
Assertions.assertEquals(Interval.oneTo(10), target);

That’s it. That’s the blog. I hope you enjoyed this short technical Java w/ Eclipse Collections blog.

I am a Project Lead and Committer for the Eclipse Collections OSS project at the Eclipse Foundation. Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.