A blog about a lesser known Eclipse Collections method

What am I getting into now?

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

The method into on RichIterable

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

@Test
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<>());
Assertions.assertEquals(integers.toBag(),
Bags.mutable.withAll(into3));

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 into method is also useful when you want to drain multiple source collections into the same target collection.

@Test
public void intoList()
{
var target = Interval.oneTo(3).into(new ArrayList<>());
Interval.fromTo(4,6).into(target);
Interval.fromTo(7,10).into(target);
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.

Java Champion. Creator of the Eclipse Collections OSS Java library (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.