Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Which is best? Is there a more elegant way to do it in Java 6?
- // Solution 1 with nested for loops
- for(A a : listOfAs){
- for(B b : listOfBs){
- if (b.getId().equals(a.getId())){
- // do stuff
- break;
- }
- }
- }
- // Solution 2 with a map as a kind of cache
- Map<Id, B> cache = new HashMap<>();
- for(B b : listOfBs){
- cache.put(b.getId(), b);
- }
- for(A a : listOfAs){
- B b = cache.get(a.getId());
- // do stuff
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement