NLinker

Exceptions and Java 8 streams. Enjoy!

Nov 4th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.95 KB | None | 0 0
  1. import org.junit.Test;
  2.  
  3. import java.util.ConcurrentModificationException;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6.  
  7. import static java.util.stream.IntStream.range;
  8.  
  9. public class ConcurrentModificationExceptionTest {
  10.     @Test(expected = ConcurrentModificationException.class)
  11.     public void shouldThrowWhileIteratingAndRemove() throws Exception {
  12.         final List<Integer> ls = range(1, 100).boxed().collect(Collectors.toList());
  13.         for (Integer l : ls) {
  14.             System.out.println(l);
  15.             ls.remove(l);
  16.         }
  17.  
  18.     }
  19.  
  20.     @Test(expected = ConcurrentModificationException.class)
  21.     public void shouldThrowWhileIteratingAndRemove_StreamApi() throws Exception {
  22.         final List<Integer> ls = range(1, 100).boxed().collect(Collectors.toList());
  23.         ls.stream().forEach(
  24.                 l -> {
  25.                     System.out.println(l);
  26.                     ls.remove(l);
  27.                 });
  28.     }
  29. }
Add Comment
Please, Sign In to add comment