0

I have code like this:

 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 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.

4
  • Are you asking how to make it into a function or how to get the same results without having to run two loops every time you want to check? Commented Oct 11, 2020 at 14:41
  • @NathanWalsh without having to run two loops every time Commented Oct 11, 2020 at 14:42
  • you can use the streams api. Example: List<Integer> list = new ArrayList<>(); list.stream().filter( i -> true ).forEach( i -> list.add(i)); Or you can even replace forEach with simply collect Commented Oct 11, 2020 at 14:46
  • 2
    @merc-angel What do you mean by "without having to run two loops every time"? Do you mean that you don't need to iterate over all elements for every element? Because Naga's answer is almost no different than your nested loop in terms of performance. The only difference is that it doesn't continue after a match is found, but you can easily put a break in your if to accomplish that. Commented Oct 11, 2020 at 14:51

4 Answers 4

3
List<Account> myAccounts = new ArrayList<>();    
List<String> numbers = new ArrayList<>();    
List<Account> resultList = myAccounts.stream()
                .filter(account -> numbers.contains(account.getNumber()))
                .collect(Collectors.toList());

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

Sign up to request clarification or add additional context in comments.

2 Comments

@merc-angel numbers should probably be a Set<String> since sets usually optimize contains method the most. For instance HashSet#contiains complexity is close ot O(1).
100% agreed with @Pshemo
2

Try it like this.

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

Comments

-1

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/

Comments

-2

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

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

6 Comments

Ok, but I mean how to replace loop in loop
You have to use loops, otherwise how are you going to compare the values?
I think it is possible to do :D
You could read into lambda, if you're interested. But I'm not sure if you can iterate through arrays with lambda.
Streams api that's how, look at the other answers. He asked for a functional approach and this is not functional.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.