@@ -0,0 +1,2126 @@
+package com.levelup.java.collections;%0A%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Spliterator;%0Aimport java.util.Spliterators;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.StreamSupport;%0A%0Aimport org.apache.commons.collections.IteratorUtils;%0Aimport org.junit.Test;%0A%0Aimport com.google.common.collect.Lists;%0A%0A/**%0A * This java example will demonstrate how to convert an Iterator to List.%0A * %0A * @author Justin Musgrove%0A * @see %3Ca href='http://www.leveluplunch.com/java/examples/convert-iterator-arraylist/'%3EConvert Iterator to ArrayList%3C/a%3E%0A */%0Apublic class ConvertIteratorToArrayList %7B%0A%0A%09static LinkedList%3CString%3E collection = new LinkedList%3CString%3E();%0A%0A%09static %7B%0A%09%09collection.push(%22One%22);%0A%09%09collection.push(%22Two%22);%0A%09%09collection.push(%22Three%22);%0A%09%09collection.push(%22Four%22);%0A%09%7D%0A%0A%09@Test%0A%09public void convert_iterator_to_list_java() %7B%0A%0A%09%09Iterator%3CString%3E iteratorToList = collection.iterator();%0A%0A%09%09List%3CString%3E listOfStrings = new ArrayList%3CString%3E(4);%0A%09%09while (iteratorToList.hasNext()) %7B%0A%09%09%09listOfStrings.add(iteratorToList.next());%0A%09%09%7D%0A%0A%09%09assertTrue(listOfStrings.size() == 4);%0A%09%7D%0A%0A%09@Test%0A%09public void convert_iterator_to_list_java8() %7B%0A%0A%09%09Iterator%3CString%3E iteratorToCollection = collection.iterator();%0A%0A%09%09List%3CString%3E convertedIterator = StreamSupport.stream(%0A%09%09%09%09Spliterators.spliteratorUnknownSize(iteratorToCollection,%0A%09%09%09%09%09%09Spliterator.ORDERED), false).collect(%0A%09%09%09%09Collectors.%3CString%3E toList());%0A%0A%09%09assertTrue(convertedIterator.size() == 4);%0A%0A%09%7D%0A%0A%09@Test%0A%09public void convert_iterator_to_list_guava() %7B%0A%0A%09%09Iterator%3CString%3E iteratorToArray = collection.iterator();%0A%0A%09%09List%3CString%3E convertedIterator = Lists.newArrayList(iteratorToArray);%0A%0A%09%09assertTrue(convertedIterator.size() == 4);%0A%09%7D%0A%0A%09@Test%0A%09public void convert_iterator_to_list_apache() %7B%0A%0A%09%09Iterator%3CString%3E iteratorToArrayList = collection.iterator();%0A%0A%09%09@SuppressWarnings(%22unchecked%22)%0A%09%09List%3CString%3E convertedIteratorToList = IteratorUtils%0A%09%09%09%09.toList(iteratorToArrayList);%0A%0A%09%09assertTrue(convertedIteratorToList.size() == 4);%0A%09%7D%0A%7D%0A

convert iterator to list