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

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

How to replace loop in loop in functional way?

问题

我有类似这样的代码:

  1. List<Account> myAccounts = new ArrayList<>();
  2. List<String> numbers = new ArrayList<>();
  3. List<Account> resultList = new ArrayList<>();
  4. for (String number : numbers) {
  5. for (Account account : myAccounts) {
  6. if(number.equals(account.getNumber())){
  7. resultList.add(account);
  8. }
  9. }
  10. }

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

英文:

I have code like this:

  1. List&lt;Account&gt; myAccounts = new ArrayList&lt;&gt;();
  2. List&lt;String&gt; numbers = new ArrayList&lt;&gt;();
  3. List&lt;Account&gt; resultList = new ArrayList&lt;&gt;();
  4. for (String number : numbers) {
  5. for (Account account : myAccounts) {
  6. if(number.equals(account.getNumber())){
  7. resultList.add(account);
  8. }
  9. }
  10. }

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

  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.stream.Collectors;
  4. List<Account> myAccounts = new ArrayList<>();
  5. List<String> numbers = new ArrayList<>();
  6. List<Account> resultList = myAccounts.stream()
  7. .filter(account -> numbers.contains(account.getNumber()))
  8. .collect(Collectors.toList());
英文:
  1. List&lt;Account&gt; myAccounts = new ArrayList&lt;&gt;();
  2. List&lt;String&gt; numbers = new ArrayList&lt;&gt;();
  3. List&lt;Account&gt; resultList = myAccounts.stream()
  4. .filter(account -&gt; numbers.contains(account.getNumber()))
  5. .collect(Collectors.toList());

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

答案2

得分: 2

请尝试以下方式。

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

Try it like this.

  1. List&lt;Account&gt; results = numbers.stream().flatMap(numb -&gt; myAccounts
  2. .stream()
  3. .filter(acct-&gt;numb.equals(acct.getNumber()))
  4. .collect(Collectors.toList());
  5. </details>
  6. # 答案3
  7. **得分**: -1
  8. 以下是翻译好的内容:
  9. 最佳实现方法是将您的账户对象加载到SQLite数据库中。(如果您已经有一个数据库,也可以将其添加到当前数据库中)
  10. https://developer.android.com/training/data-storage/sqlite
  11. 一旦您将所有账户对象存储在数据库中,您只需编写一个查询来列出具有匹配号码的账户。
  12. 下面是一个更完整的示例,展示了从创建数据库到创建对象,最终到查询对象的整个过程。
  13. https://www.androidauthority.com/creating-sqlite-databases-in-your-app-719366/
  14. <details>
  15. <summary>英文:</summary>
  16. 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 )
  17. https://developer.android.com/training/data-storage/sqlite
  18. 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.
  19. 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.
  20. https://www.androidauthority.com/creating-sqlite-databases-in-your-app-719366/
  21. </details>
  22. # 答案4
  23. **得分**: -2
  24. 用函数式方法,您可以将 for 循环构建为一个方法,如下所示:
  25. ```java
  26. public void compareNumbers(List<String> numbers, List<Account> resultList) {
  27. numbers.forEach(number -> myAccounts.stream()
  28. .filter(account -> number.equals(account.getNumber()))
  29. .forEach(resultList::add));
  30. }
英文:

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

  1. public void compareNumbers(List&lt;String&gt; numbers, List&lt;Account&gt; resultList){
  2. for (String number : numbers) {
  3. for (Account account : myAccounts) {
  4. if(number.equals(account.getNumber())){
  5. resultList.add(account);
  6. }
  7. }
  8. }

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:

确定