import java.util.Iterator;
import java.util.stream.StreamSupport;

public class TestList {

	public static void main(String[] args) {
		LinkedList<Integer> list = new LinkedList<>();

		for (int i = 1; i <= 5; ++i) {
			list.add(i);
			list.add(i);
			list.add(i);
		}
		
		System.out.println("After init: ");
		print (list);
		
		int removeCount = removeAll(1, list);
		System.out.println("Removed " + removeCount + " elements");
		print (list);
		
		
	}
	
	public static <E> void print(Iterable<E> collection) {
		StreamSupport
			.stream(collection.spliterator(), false)
			.map(s -> s + " ")
			.forEach(System.out::print);
		System.out.println();
		System.out.println();
	}
	
	public static <E> int removeAll(E elem, Iterable<E> collection) {
		int removed = 0;
		Iterator<E> iter = collection.iterator();
		while (iter.hasNext()) {
			E val = iter.next();
			if (val.equals(elem)) {
				++removed;
				iter.remove();
			}
		}
		return removed;
	}

	
	public static int sum(Iterable<Integer> collection) {
		return StreamSupport
			.stream(collection.spliterator(), false)
			.reduce((a,b) -> a + b)
			.get();
	}
	
	public static int product(Iterable<Integer> collection) {
		return StreamSupport
			.stream(collection.spliterator(), false)
			.reduce((a,b) -> a * b)
			.get();
	}

	public static int min(Iterable<Integer> collection) {
		return StreamSupport
			.stream(collection.spliterator(), false)
			.reduce((a,b) -> a < b ? a : b)
			.get();
	}

	public static int max(Iterable<Integer> collection) {
		return StreamSupport
			.stream(collection.spliterator(), false)
			.reduce((a,b) -> a > b ? a : b)
			.get();
	}
	
}
