0

I am trying to build a list from two another list, which is fetched from the database. The list I wanted to build has a few values from List A, And I have to match on value from List B & get the values for List C.

My implementation seems as below, which is not effective when using when each array have 3000+ values, which eventually runs 3000*3000 in each run.

List<userApplicationModel> userApplicationModelList = new ArrayList<>();
AccessTokens.forEach(AccessToken -> {
ConsumerAppList.forEach(ConsumerApp -> {
   if (AccessToken.getConsumerKeyID().equals(ConsumerApp.getID())){
         userApplicationModelList.add(generateApplicationModel(ConsumerApp,AccessToken));                    }
            });
        });
return userApplicationModelList;

GenerateApplicationModel

    private userApplicationModel generateApplicationModel(ConsumerApps consumerApp, AccessToken accessToken) {
            userApplicationModel userApplication = new userApplicationModel();
            userApplication.setTokenStatus(accessToken.getTokenState());
            userApplication.setUserName(consumerApp.getUserName());
            return userApplication;
        }
Bubashan_kushan
  • 384
  • 8
  • 24
  • 1
    Why don't you try to map two tables using database query and get the list directly without mapping them programmatically? – Himashi Rodrigo Oct 27 '20 at 09:23
  • @HimashiRodrigo I tried, there is a jpa error arousing, thats the reason i went with this implementation. https://stackoverflow.com/questions/64551110/springboot-custom-select-query-returns-no-converter-found-capable-of-converting?noredirect=1#comment114140856_64551110 – Bubashan_kushan Oct 27 '20 at 09:25
  • In ```GenerateApplicationModel``` ```userApplication.setUserName(idnOauthConsumerApp.getUserName());``` uses parameter ```ConsumerApp```? No usage of ```accessToken``` parameter? – prostý člověk Oct 27 '20 at 10:22
  • I would eventually prepare some list into HashMap by some key and look for this key while iterating other lists. More memory consumption but more efficency. – Adam Ostrožlík Oct 27 '20 at 12:06

0 Answers0