如何以函数式的方式替代嵌套循环?

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

How to replace loop in loop in functional way?

问题

我有类似这样的代码:

 List<Account> myAccounts = new ArrayList<>();

 List<String> numbers = new ArrayList<>();

 List<Account> resultList = new ArrayList<>();

 for (String number : numbers) {
     for (Account account : myAccounts) {
         if(number.equals(account.getNumber())){
             resultList.add(account);
         }
     }
 }

我尝试通过循环遍历所有数字并将其与账户数字进行比较,以获取指定的账户与指定的数字。如何以函数式风格实现这个?我不是在问如何将其变成一个函数,而是如何在不必每次都运行两个循环的情况下获得相同的结果。

英文:

I have code like this:

 List&lt;Account&gt; myAccounts = new ArrayList&lt;&gt;();

 List&lt;String&gt; numbers = new ArrayList&lt;&gt;();

 List&lt;Account&gt; resultList = new ArrayList&lt;&gt;();

        for (String number : numbers) {
            for (Account account : myAccounts) {
                if(number.equals(account.getNumber())){
                    resultList.add(account);
                }
            }
        }

I tried get speciefied accounts with specified numbers, but I do this via loop all numbers and compare to accounts numbers. How can I do this in functional style? Im not asking how to make it into a function, but how to get the same results without having to run two loops every time.

答案1

得分: 3

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

List<Account> myAccounts = new ArrayList<>();
List<String> numbers = new ArrayList<>();
List<Account> resultList = myAccounts.stream()
                    .filter(account -> numbers.contains(account.getNumber()))
                    .collect(Collectors.toList());
英文:
List&lt;Account&gt; myAccounts = new ArrayList&lt;&gt;();    
List&lt;String&gt; numbers = new ArrayList&lt;&gt;();    
List&lt;Account&gt; resultList = myAccounts.stream()
                .filter(account -&gt; numbers.contains(account.getNumber()))
                .collect(Collectors.toList());

You can use this. only requirement is that you should be using at least Java8

答案2

得分: 2

请尝试以下方式。

List<Account> results = numbers.stream().flatMap(numb -> myAccounts
                  .stream()
                  .filter(acct -> numb.equals(acct.getNumber()))
                  .collect(Collectors.toList());
英文:

Try it like this.

List&lt;Account&gt; results = numbers.stream().flatMap(numb -&gt; myAccounts
                  .stream()
				  .filter(acct-&gt;numb.equals(acct.getNumber()))
                  .collect(Collectors.toList());

</details>



# 答案3
**得分**: -1

以下是翻译好的内容:

最佳实现方法是将您的账户对象加载到SQLite数据库中。(如果您已经有一个数据库,也可以将其添加到当前数据库中)

https://developer.android.com/training/data-storage/sqlite

一旦您将所有账户对象存储在数据库中,您只需编写一个查询来列出具有匹配号码的账户。

下面是一个更完整的示例,展示了从创建数据库到创建对象,最终到查询对象的整个过程。

https://www.androidauthority.com/creating-sqlite-databases-in-your-app-719366/

<details>
<summary>英文:</summary>

The best way to accomplish this would be to load your Account objects into a sql lite database.  ( Or add it to your current database if you already have one )

    https://developer.android.com/training/data-storage/sqlite

Then once you have all of the Account objects store in the database you could just write a query to list the accounts with matching numbers. 

Here is a more complete example on how to build everything from creating the database to creating the objects and eventually to querying for them.

    https://www.androidauthority.com/creating-sqlite-databases-in-your-app-719366/

</details>



# 答案4
**得分**: -2

用函数式方法,您可以将 for 循环构建为一个方法,如下所示:

```java
public void compareNumbers(List<String> numbers, List<Account> resultList) {
    numbers.forEach(number -> myAccounts.stream()
            .filter(account -> number.equals(account.getNumber()))
            .forEach(resultList::add));
}
英文:

In a functional approach you would construct a method out of your for loop as in:

public void compareNumbers(List&lt;String&gt; numbers, List&lt;Account&gt; resultList){
  for (String number : numbers) {
        for (Account account : myAccounts) {
            if(number.equals(account.getNumber())){
                resultList.add(account);
            }
   }
}

huangapple
  • 本文由 发表于 2020年10月11日 22:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64305214.html
匿名

发表评论

匿名网友

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

确定