Java one liner to create a list of String, add contents of another List<String> and return a List

huangapple go评论84阅读模式
英文:

Java one liner to create a list of String, add contents of another List<String> and return a List

问题

如何创建一个字符串列表,然后将另一个列表的所有内容添加到其中,然后返回该列表?

考虑一个函数:

public List<String> foo() {
  return Arrays.asList("some", "list", "of", "strings", "from", "foo");
}

一个名为MyConstants的类:

public class MyConstants {
  public static final String myConst = "String from Constant File";
}

我正在编写一个名为bar()的函数,需要一个一行代码来返回一个列表:

public List<String> bar() {
    List<String> result = new ArrayList<>(Collections.singleton(MyConstants.myConst));
    result.addAll(foo());
    return result;
}

但是addAll() 方法返回一个布尔值。另外,当我尝试以下代码时:

Arrays.asList(new ArrayList<>(Collections.singleton(MyConstants.myConst)).addAll(foo()));

由于明显的原因,它会返回一个 List<Boolean>

如何创建一个列表,将另一个列表添加到其中,然后返回合并后的列表?

英文:

How to create a List of String then add all the contents of another List to it and then return that list??

Consider a function:

public List&lt;String&gt; foo() {
  return Arrays.asList(&quot;some&quot;, &quot;list&quot;, &quot;of&quot;, &quot;strings&quot;, &quot;from&quot;, &quot;foo&quot;);
}

A Class MyConstants:

public MyConstants {
  public static final String myConst = &quot;String from Constant File&quot;;
}

I am writing a function bar() and need a one liner to return a list:

public List&lt;String&gt; bar() {
    return new ArrayList&lt;String&gt;(Collections.singleTon(MyConstants.myConst)).addAll(foo());
}

But addAll() returns boolean. Also, when I tried

Arrays.asList(return new ArrayList&lt;String&gt;(Collections.singleTon(MyConstants.myConst)).addAll(foo()));

It is List&lt;Boolean&gt; for pretty obvious reasons.

How can I create a list, add another list to it and then return that combined list?

答案1

得分: 1

为什么地球上会有一行的限制呢?Java集合对于流畅的表达并不是很好。但你可以滥用流(Streams):

public List<String> bar() {
    return Stream.concat(Stream.of(MyConstants.myConst), foo().stream()).collect(Collectors.toList());
}

一种不那么糟糕的方法是使用Guava库:

public List<String> bar() {
    return ImmutableList.<String>builder().add(MyConstants.myConst).addAll(foo()).build();
}
英文:

Why on earth is there a constraint of one line? Java collections are not great for fluent expressions. But you can abuse streams:

public List&lt;String&gt; bar() {
    return Stream.concat(Stream.of(MyConstants.myConst), foo().stream()).collect(toList());
}

A less awful way is to use Guava libraries:

public List&lt;String&gt; bar() {
    return ImmutableList.&lt;String&gt;builder().add(MyConstants.myConst).addAll(foo()).build();
}

答案2

得分: 0

多亏了 @gene 的回答,我找到了一个比 Stream 或者 ImmutableList builder 稍微好一些的 apache-commons 方法。在这里是它:

        return ListUtils.union(Arrays.asList(MyConstants.myConst), foo());

英文:

Thanks to @gene for the answer, I figured out apache-commons one lesser awful way other than Stream or ImmutableList builder. Here it is

        return ListUtils.union(Arrays.asList(MyConstants.myConst), foo());

huangapple
  • 本文由 发表于 2020年5月30日 14:48:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/62098865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定