Is there a shorthand way (may be guava or any lib) to initialize a Java List like this?
List list = MagicListUtil.newArrayList(firstElement, moreElementsList);
Is there a shorthand way (may be guava or any lib) to initialize a Java List like this?
List list = MagicListUtil.newArrayList(firstElement, moreElementsList);
Lists.asList(...)
String first = "first";
String[] rest = { "second", "third" };
List<String> list = Lists.asList(first, rest);
Iterables
, use FluentIterable.of(...).append(...).toList()
:String first = "first";
List<String> rest = Arrays.asList("second", "third");
List<String> list = FluentIterable.of(first).append(rest).toList();
Even though, it's way more verbose, but still...
String first = "first";
String[] rest = { "second", "third" };
List<String> list = Stream.concat(Stream.of(first), Arrays.stream(rest))
.collect(Collectors.toList());
String first = "first";
List<String> rest = Arrays.asList("second", "third");
List<String> list = Stream.concat(Stream.of(first), rest.stream())
.collect(Collectors.toList());
Yeah you can simply use the java.util.Arrays class for single and multiple elements.
List<String> strings = Arrays.asList("first", "second", "third");
You can use the java.util.Collections for a list with a single element.
List<String> strings = Collections.singletonList("first");
If you wanna copy list, you can do it by constructor like this
List<Float> oldList = new ArrayList<>();
List<Float> newList = new ArrayList<>(oldList);