使用Java中的集合和if else语句以优化方式。

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

using collection and if else statement in java in optimize way

问题

以下是经过翻译的代码部分:

我有下面的代码它按预期工作我想优化它可能使用流的方法

List<List<String>> colorsList = List.of(
   List.of("red","maroon"),
   List.of("blue, skyblue"),
   List.of("pink"));
   
int var1 = 5;
List<String> listReturn = new ArrayList<>();

for(List<String> innerList: colorsList){
    if(var1 == 5){
        if(innerList.contains("blue")){
            listReturn.addAll(innerList);
        }
    }else if(var1 == 10){
        if(innerList.contains("pink") || innerList.contains("red")){
            listReturn.addAll(innerList);
        }
    }
}

希望这能帮助您理解代码。如果您有任何其他问题,请随时提问。

英文:

I have a below code which is working as expected and I want to optimize it, possibly if using methods of stream.

List&lt;List&lt;String&gt;&gt; colorsList = List.of(
   List.of(&quot;red&quot;,&quot;maroon&quot;),
   List.of(&quot;blue, skyblue&quot;),
   List.of(&quot;pink&quot;));

int var1 = 5;
List&lt;String&gt; listReturn = new ArrayList&lt;&gt;();

for(List&lt;String&gt; innerList: colorsList){
	if(var1 == 5){
		if(innerList.contains(&quot;blue&quot;)){
			listReturn.addAll(innerList);
		}
    }else if(var1 == 10){
		if(innerList.contains(&quot;pink&quot;) || innerList.contains(&quot;red&quot;)){
			listReturn.addAll(innerList);
		}
	}
  }
}

Basically I am returning a single List<String> listReturn combining 1 or more List<String> from original colorsList based on conditions.
Is there a better and optimized way to write above?

答案1

得分: 1

你可以参考以下的代码,你还应该检查colorsList是否为空

List&lt;List&lt;String&gt;&gt; colorsList = List.of(
        List.of(&quot;red&quot;,&quot;maroon&quot;),
        List.of(&quot;blue&quot;, &quot;skyblue&quot;),
        List.of(&quot;pink&quot;));

int var1 = 5;
List&lt;String&gt; listReturn = new ArrayList&lt;&gt;();

if (5 == var1) {
    colorsList.stream()
            .filter(innerList -&gt; innerList.contains(&quot;blue&quot;))
            .forEach(listReturn::addAll);
} else if (10 == var1) {
    colorsList.stream()
            .filter(innerList -&gt; innerList.contains(&quot;pink&quot;) || innerList.contains(&quot;red&quot;))
            .forEach(listReturn::addAll);
}
英文:

You can refer to the following code, you should also check colorsList is null or not

List&lt;List&lt;String&gt;&gt; colorsList = List.of(
        List.of(&quot;red&quot;,&quot;maroon&quot;),
        List.of(&quot;blue&quot;, &quot;skyblue&quot;),
        List.of(&quot;pink&quot;));

int var1 = 5;
List&lt;String&gt; listReturn = new ArrayList&lt;&gt;();

if (5 == var1) {
    colorsList.stream()
            .filter(innerList -&gt; innerList.contains(&quot;blue&quot;))
            .forEach(listReturn::addAll);
} else if (10 == var1) {
    colorsList.stream()
            .filter(innerList -&gt; innerList.contains(&quot;pink&quot;) || innerList.contains(&quot;red&quot;))
            .forEach(listReturn::addAll);
}

答案2

得分: 0

如果我想编写这段代码,我将首先创建一个颜色列表(colorsList)的流,然后进行过滤。如果过滤条件为真,该项将被添加到输出中。
接下来,我们使用flatMap()将嵌套的列表展平为一个字符串的单一流。

List<String> listReturn = colorsList.stream()
        .filter(innerList -> var1 == 5 && innerList.contains("blue")
                || var1 == 10 && (innerList.contains("pink") || innerList.contains("red")))
        .flatMap(Collection::stream)
        .collect(Collectors.toList());
英文:

If I want to write this code, I will first create a stream of colorsList and then filter it. If the filter condition is true, the item will be added to the output.
Next, we use flatMap() to flatten the nested lists into a single stream of strings.

List&lt;String&gt; listReturn = colorsList.stream()
        .filter(innerList -&gt; var1 == 5 &amp;&amp; innerList.contains(&quot;blue&quot;)
                || var1 == 10 &amp;&amp; (innerList.contains(&quot;pink&quot;) || innerList.contains(&quot;red&quot;)))
        .flatMap(Collection::stream)
        .collect(Collectors.toList());

答案3

得分: 0

你可以将谓词提取出来,这样你就不必重复遍历和过滤列表的代码:

List<List<String>> colorsList = List.of(
        List.of("red", "maroon"),
        List.of("blue", "skyblue"),
        List.of("pink"));

int var1 = 5;

// switch expression
Predicate<List<String>> predicate = switch (var1) {
    case 5 -> innerList -> innerList.contains("blue");
    case 10 -> innerList -> innerList.contains("pink") || innerList.contains("red");
    default -> innerList -> true; // 如果不是5或10,接受全部
};

List<String> listReturn = colorsList.stream()
               .filter(predicate).flatMap(List::stream).toList();

希望这有帮助!

英文:

You could factor out the predicate, this way you don't have to repeat the code of going over and filtering the list:

List&lt;List&lt;String&gt;&gt; colorsList = List.of(
        List.of(&quot;red&quot;,&quot;maroon&quot;),
        List.of(&quot;blue, skyblue&quot;),
        List.of(&quot;pink&quot;));

int var1 = 5;

// switch expression
Predicate&lt;List&lt;String&gt;&gt; predicate = switch (var1) {
    case 5 -&gt; innerList -&gt; innerList.contains(&quot;blue&quot;);
    case 10 -&gt; innerList -&gt; innerList.contains(&quot;pink&quot;) || innerList.contains(&quot;red&quot;);
    default -&gt; innerList -&gt; true; // if something else than 5 or 10 accept all
};

List&lt;String&gt; listReturn = colorsList.stream()
               .filter(predicate).flatMap(List::stream).toList();

huangapple
  • 本文由 发表于 2023年5月15日 11:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250736.html
匿名

发表评论

匿名网友

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

确定