Advertisement
onlytt

Increment counter in lambda functions

Nov 16th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.45 KB | None | 0 0
  1. Increment counter in lambda functions
  2. ------------------ Pre Java 8 ---------------------
  3. Map<Employee, File> fileMap = new HashMap<Employee, File>();
  4. int i =0;
  5. for (Map.Entry<Employee, File> entry : map.entrySet()) {
  6.        i++;
  7.        // do something with i
  8.    }
  9.  
  10. ------------------ Java 8 Lambda ---------------------
  11. AtomicInteger counter = new AtomicInteger(0);
  12. fileMap.forEach((k, v) -> {
  13.     counter.addAndGet(1);
  14.     //do something with i
  15.   });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement