Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. // Which is best? Is there a more elegant way to do it in Java 6?
  2. // Solution 1 with nested for loops
  3. for(A a : listOfAs){
  4. for(B b : listOfBs){
  5. if (b.getId().equals(a.getId())){
  6. // do stuff
  7. break;
  8. }
  9. }
  10. }
  11.  
  12. // Solution 2 with a map as a kind of cache
  13. Map<Id, B> cache = new HashMap<>();
  14. for(B b : listOfBs){
  15. cache.put(b.getId(), b);
  16. }
  17. for(A a : listOfAs){
  18. B b = cache.get(a.getId());
  19. // do stuff
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement