import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * {@link <a href=
 * "https://[Log in to view URL]"
 * target= "_blank">Primitive ArrayList<E></a>}
 * 
 * @author itammb ( Italia Massimiliano Buscati )
 * @version JDK 1.15
 *
 */
class Main {
	/**
	 * @see Comparator
	 *
	 */
	private static class ComparatorImp implements Comparator<Integer> {
		public int compare(Integer a, Integer b) {
			// return Integer.compare(a, b);
			return a == b ? 0 : (a < b) ? -1 : 1;
		}
	}

	public static void applayMethodAdd(List<Integer> buffer, Integer element) {
		buffer.add(element);
		applayLoop(buffer);
	}

	/**
	 * @see ArrayList#add(int, Object)
	 *
	 */
	public static void applayMethodAdd(List<Integer> buffer, int index, Integer element) {
		buffer.add(index, element);
		applayLoop(buffer);
	}

	/**
	 * @see List#size()
	 * @see List#get(int)
	 *
	 */
	public static void applayLoop(List<Integer> buffer) {
		if (buffer.isEmpty())
			System.out.print("[]");
		else
			for (int i = 0; i < buffer.size(); i++)
				System.out.print("[i=" + i + ", e=" + buffer.get(i) + "] ");

		println();
	}

	/**
	 * @see ArrayList#size()
	 * @see ArrayList#get(int)
	 *
	 */
	public static void applayMethodIterator(List<Integer> buffer) {
		for (Iterator<Integer> itr = buffer.iterator(); itr.hasNext();)
			System.out.print("e=" + itr.next() + " ");
		
		println();
	}

	/**
	 * @see ArrayList#clear()
	 *
	 */
	public static void applayMethodClear(List<Integer> buffer) {
		buffer.clear();
		System.out.println(buffer);
	}

	/**
	 * @see ArrayList#remove(int)
	 *
	 */
	public static Integer applayMethodRemove(List<Integer> buffer) {
		Integer removedElement = buffer.remove(0);
		applayLoop(buffer);

		return removedElement;
	}

	/**
	 * @see ArrayList#remove(Object)
	 *
	 */
	public static void applayMethodRemove(List<Integer> buffer, Object element) {
		buffer.remove((Integer) element);
		applayLoop(buffer);
	}

	/**
	 * @see ArrayList#set(int, Object)
	 *
	 */
	public static void applayMethodSet(List<Integer> buffer, int index, Integer element) {
		buffer.set(index, element);
		applayLoop(buffer);
	}

	public static void applayMethodEquals(List<Integer> buffer) {
		List<Integer> localeBuf = new ArrayList<Integer>() {
			private static final long serialVersionUID = -69L;

			{
				add(5);
				add(6);
				add(7);
			}
		};

		applayEquals(buffer, localeBuf);

		localeBuf = new ArrayList<Integer>() {
			private static final long serialVersionUID = -69L;

			{
				add(1);
				add(2);
				add(3);
			}
		};

		applayEquals(buffer, localeBuf);
	}

	/**
	 * @see ArrayList#equals(Object)
	 *
	 */
	private static void applayEquals(List<Integer> source, List<Integer> other) {
		applayLoop(source);
		applayLoop(other);
		System.out.println(source.equals(other));
	}

	public static void applayMethodRemoveAll(List<Integer> buffer) {
		List<Integer> localeBuf = new ArrayList<Integer>() {
			private static final long serialVersionUID = -69L;

			{
				add(1);
				add(2);
				add(5);
			}
		};

		applayRemoveAll(buffer, localeBuf);
	}

	/**
	 * @see ArrayList#removeAll(java.util.Collection)
	 *
	 */
	private static void applayRemoveAll(List<Integer> source, List<Integer> other) {
		System.out.println(source.removeAll(other));
		applayLoop(source);
		applayLoop(other);
	}

	public static void applayMethodRetainAll(List<Integer> buffer) {
		List<Integer> localeBuf = new ArrayList<Integer>() {
			private static final long serialVersionUID = -69L;

			{
				add(1);
				add(2);
				add(5);
			}
		};

		applayRetainAll(buffer, localeBuf);
	}

	/**
	 * @see ArrayList#retainAll(java.util.Collection)
	 *
	 */
	private static void applayRetainAll(List<Integer> source, List<Integer> other) {
		System.out.println(source.retainAll(other));
		applayLoop(source);
		applayLoop(other);
	}

	/**
	 * @see Collections#unmodifiableList(List)
	 *
	 */
	public static void createUnmodifiableList() {
		List<Integer> view = Collections.unmodifiableList(createBufferTest());
		view.add(1);
	}

	/**
	 * @see ArrayList#subList(int, int)
	 *
	 */
	public static void applayMethodSubList(List<Integer> buffer, int fromIndex, int toIndex) {
		List<Integer> view = buffer.subList(fromIndex, toIndex);

		// verifica l'indipendenza della struttura
		view.remove(0);
		applayLoop(view);
		applayLoop(buffer);
	}

	/**
	 * @see Collections#synchronizedList(List)
	 *
	 */
	public static void applaySynchronizedList(List<Integer> buffer) {
		List<Integer> synBuffer = Collections.synchronizedList(buffer);

		synBuffer.add(-1);

		// wrapper
		applayLoop(synBuffer);
		// sorgente
		applayLoop(buffer);
	}

	/**
	 * @see List#copyOf(java.util.Collection)
	 *
	 */
	public static void applayCopyOf(List<Integer> source) {
		// vista immutabile
		List<Integer> view = List.copyOf(source);

		List<Integer> copyBuf = new ArrayList<Integer>(view);
		copyBuf.remove(0);
		applayLoop(copyBuf);
		applayLoop(source);
		applayLoop(view);
	}

	/**
	 * @see Arrays#asList(Object...)
	 * @see Collections#copy(List, List)
	 *
	 */
	public static void applayCopy(List<Integer> source) {
		List<Integer> copyBuf = new ArrayList<Integer>(Arrays.asList(-1, -2, -3));

		// la struttura destinazione deve avere gli stessi elementi della sorgente
		Collections.copy(copyBuf, source);

		// verifica l'indipendenza della struttura
		copyBuf.remove(0);
		applayLoop(copyBuf);
		applayLoop(source);
	}

	public static void applayMethodStream(List<Integer> source) {
		List<Integer> copyBuf = applayStream(source);

		copyBuf.remove(0);
		applayLoop(copyBuf);
		applayLoop(source);
	}

	/**
	 * @see ArrayList#stream()
	 * @see Stream#collect(java.util.stream.Collector)
	 * @see Collectors#toList()
	 *
	 */
	private static List<Integer> applayStream(List<Integer> source) {
		Stream<Integer> stream = source.stream();
		return stream.collect(Collectors.toList());
	}

	/**
	 * @see Collections#sort(List)
	 *
	 */
	public static void applaySort(List<Integer> buffer) {
		Collections.sort(buffer);
		applayLoop(buffer);
	}

	/**
	 * @see Collections#sort(List, Comparator)
	 *
	 */
	public static void applayComparator(List<Integer> buffer) {
		Collections.sort(buffer, new ComparatorImp());
		applayLoop(buffer);
	}

	/**
	 * @see Collections#sort(List)
	 * @see Collections#binarySearch(List, Object)
	 *
	 */
	public static void applayBinarySearch(List<Integer> buffer, Integer element) {
		Collections.sort(buffer);
		applayLoop(buffer);
		System.out.println(Collections.binarySearch(buffer, element));
	}
	
	/**
	 * @see Collections#fill(List, Object)
	 *
	 */
	public static void applayFill(List<Integer> buffer, Integer element) {
		Collections.fill(buffer, element);
		applayLoop(buffer);
	}

	/**
	 * @see Collections#emptyList()
	 *
	 */
	public static void applayEmptyList() {
		List<Integer> buffer = Collections.emptyList();
		System.out.println(buffer);
	}
	
	/**
	 * @see ArrayList#add(Object)
	 *
	 */
	private static List<Integer> createBufferTest() {
		return new ArrayList<Integer>() {
			private static final long serialVersionUID = -69L;

			{
				add(1);
				add(2);
				add(3);
			}
		};
	}
	
	private static void println() {
		System.out.println();
	}

	public static void main(String args[]) throws Exception {
		// Unit test - aggiunge un elemento in sequenza a una struttura
		applayMethodAdd(createBufferTest(), 4);

		// Unit test - attraversa una struttura con un metodo ad accesso casuale
		applayLoop(createBufferTest());

		// Unit test - attraversa una struttura con l'ausilio di un'interfaccia
		// Iteretor<E>
		applayMethodIterator(createBufferTest());

		// Unit test - pulisce una struttura
		applayMethodClear(createBufferTest());

		// Unit test - aggiunge e modifica la seguenza degli elementi in una struttura
		// [ 0 -> posizione inserimento, -1 elemento inserito ]
		applayMethodAdd(createBufferTest(), 0, -1);

		// Unit test - rimuove un elemento in POSIZIONE 0 e lo restituisce
		applayMethodRemove(createBufferTest());

		// Unit test - cerca e rimuove un elemento da una struttura
		applayMethodRemove(createBufferTest(), 2);

		// Unit test - sovrascrive un elemento di una struttura
		// [ 0 -> posizione sovrascrittura, -1 elemento inserito ]
		applayMethodSet(createBufferTest(), 0, -1);

		// Unit test - verifica se due strutture sono uguali ( stessi elementi )
		applayMethodEquals(createBufferTest());

		// Unit test - rimuove elementi concordi in una struttura ( sorgente ) rispetto
		// a una seconda
		applayMethodRemoveAll(createBufferTest());

		// Unit test - rimuove elementi discordi in una struttura ( sorgente ) rispetto
		// a una seconda
		applayMethodRetainAll(createBufferTest());

		// Unit test - vista NON MODIFICABILE di una struttura
		// createUnmodifiableList();

		// Unit test - vista di una struttura ( sotto-insieme )
		// [ posizione-base=0, quantità elementi da prelevare ]
		applayMethodSubList(createBufferTest(), 0, 2);

		// Unit test - wrapper class sincronizzata
		applaySynchronizedList(createBufferTest());

		// Unit test - copia di una struttura ( da una sua view immutabile )
		applayCopyOf(createBufferTest());

		// Unit test - copia di una struttura ( da una struttura d'appoggio)
		applayCopy(createBufferTest());

		// Unit test - copia di una struttura ( da uno stream )
		applayMethodStream(createBufferTest());

		// Unit test - ordina una struttura
		applaySort(createBufferTest());

		// Unit test - ordina una struttura ( con una algoritmo di comparazione )
		applayComparator(createBufferTest());

		// Unit test - ricerca di un elemento in una struttura ( se elemento trovato
		// restituisce una posizione =>0 )
		applayBinarySearch(createBufferTest(), 1);

		// Unit test - riempie una struttura
		applayFill(createBufferTest(), -1);

		// Unit test - una struttura vuota
		applayEmptyList();
	}
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: