Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * SubListTest.java
- */
- package com.ptvgroup.mdf.mdf_bl_data.complex_elements;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- import org.junit.Test;
- import static org.junit.Assert.*;
- import static org.hamcrest.Matchers.*;
- /**
- * @author Simone De Cristofaro
- * Dec 14, 2018
- */
- public class SubListTest {
- @Test
- public void testSubList() {
- int nEl = 50000;
- List<Integer> list = IntStream.range(0, nEl).boxed().collect(Collectors.toList());
- for(int i = nEl - 1; i > 0; i--) {
- /*
- Unfortunately subList returns a view on the original list (this is documented) which is implemented
- in AbstractList by use of a pointer to the parent list (this is not documented).
- When calling certain operations on such a view, the operations are called recursively on the parent list (this is absolutely not documented).
- If you call subList on a view you get a view on a view, meaning you now have a parent hierarchy two levels deep.
- If you call the method a few thousand times you get a parent hierarchy a few thousand levels
- deep which is then used within a recursive method call and - hey presto - there is your stack overflow.
- */
- list = list.subList(0, i);
- }
- list = new ArrayList<>(list);
- // we can du suck operations only if the list is not a view of an original one otherwise we can occur in StackOverflowError
- list.add(1);list.remove(1);
- list.stream().skip(0).collect(Collectors.toList());
- int size = list.size();
- int firstEl = list.get(0);
- assertThat(list.size(), is(1));
- }
- }
Add Comment
Please, Sign In to add comment