Java tutorial
//package com.java2s; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.function.BiPredicate; public class Main { public static <E> void transferElementsToHead(List<E> source, List<E> target, Collection<E> elementsToTransfer) { // iterate the source in reverse order so that the order in the target is correct for (int i = source.size() - 1; i >= 0; i--) { E sourceTag = source.get(i); if (elementsToTransfer.contains(sourceTag)) { target.add(0, sourceTag); } } } public static <E> boolean contains(Collection<E> c, E item, BiPredicate<? super E, ? super E> equalTester) { Iterator<E> iter = c.iterator(); while (iter.hasNext()) { if (equalTester.test(iter.next(), item)) { return true; } } return false; } }