Monday 9 September 2019

How to make a new List in Java

Using Eclipse Collections you can create a List like this:


List list1 = Lists.mutable.empty();
List list2 = Lists.mutable.of("One", "Two", "Three");

If you want an immutable list:


ImmutableList list3 = Lists.immutable.empty();
ImmutableList list4 = Lists.immutable.of("One", "Two", "Three");

You can avoid auto-boxing by using primitive lists. Here's how you'd create int lists:


MutableIntList list5 = IntLists.mutable.empty();
MutableIntList list6 = IntLists.mutable.of(1, 2, 3);
ImmutableIntList list7 = IntLists.immutable.empty();
ImmutableIntList list8 = IntLists.immutable.of(1, 2, 3);

There are variants for all 8 primitives.


MutableLongList longList       = LongLists.mutable.of(1L, 2L, 3L);
MutableCharList charList = CharLists.mutable.of('a', 'b', 'c');
MutableShortList shortList = ShortLists.mutable.of((short) 1, (short) 2, (short) 3);
MutableByteList byteList = ByteLists.mutable.of((byte) 1, (byte) 2, (byte) 3);
MutableBooleanList booleanList = BooleanLists.mutable.of(true, false);
MutableFloatList floatList = FloatLists.mutable.of(1.0f, 2.0f, 3.0f);
MutableDoubleList doubleList = DoubleLists.mutable.of(1.0, 2.0, 3.0);

Note: I am a committer for Eclipse Collections.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...