Guest User

Untitled

a guest
Dec 14th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. /**
  2. * SubListTest.java
  3. */
  4. package com.ptvgroup.mdf.mdf_bl_data.complex_elements;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.IntStream;
  10.  
  11. import org.junit.Test;
  12.  
  13. import static org.junit.Assert.*;
  14. import static org.hamcrest.Matchers.*;
  15.  
  16. /**
  17. * @author Simone De Cristofaro
  18. * Dec 14, 2018
  19. */
  20. public class SubListTest {
  21.  
  22.  
  23. @Test
  24. public void testSubList() {
  25.  
  26. int nEl = 50000;
  27.  
  28. List<Integer> list = IntStream.range(0, nEl).boxed().collect(Collectors.toList());
  29.  
  30. for(int i = nEl - 1; i > 0; i--) {
  31.  
  32. /*
  33. Unfortunately subList returns a view on the original list (this is documented) which is implemented
  34. in AbstractList by use of a pointer to the parent list (this is not documented).
  35. When calling certain operations on such a view, the operations are called recursively on the parent list (this is absolutely not documented).
  36. If you call subList on a view you get a view on a view, meaning you now have a parent hierarchy two levels deep.
  37. If you call the method a few thousand times you get a parent hierarchy a few thousand levels
  38. deep which is then used within a recursive method call and - hey presto - there is your stack overflow.
  39. */
  40. list = list.subList(0, i);
  41.  
  42. }
  43.  
  44. list = new ArrayList<>(list);
  45. // we can du suck operations only if the list is not a view of an original one otherwise we can occur in StackOverflowError
  46. list.add(1);list.remove(1);
  47. list.stream().skip(0).collect(Collectors.toList());
  48. int size = list.size();
  49. int firstEl = list.get(0);
  50.  
  51. assertThat(list.size(), is(1));
  52.  
  53. }
  54.  
  55. }
Add Comment
Please, Sign In to add comment