package com.semanticsquare.generics; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; public class GenericsDemo{ public static void main(String[] args) { Container stringStore = new Store<>(); stringStore.set("java"); //stringStore.set(1); System.out.println(stringStore.get()); Container integerStore = new Store<>(); integerStore.set(1); System.out.println(integerStore.get()); Container> listStore = new Store<>(); listStore.set(Arrays.asList(1, 2, 3)); System.out.println(listStore.get()); //Container intStore = new Store<>(); List list = new ArrayList<>(); list.add(new Integer(1)); list.add(new Double(22.0)); //list.add(new String("22.0")); List[] array = new List[2]; array[0] = new ArrayList(); array[1] = new LinkedList(); // Raw type demo: //rawTypeTest(); List strList1 = Arrays.asList("a", "b", "c"); List strList2 = Arrays.asList("b", "c", "d"); getCommonElementsCount(strList1, strList2); } public static int getCommonElementsCount(List list1, List list2) { int count = 0; for (Object element : list1) { if (list2.contains(element)) { count++; } } System.out.println("Common elements count: " + count); return count; } public static void rawTypeTest() { System.out.println("\n\nInside rawTypeTest ..."); int ISBN = 1505297729; List prices = new ArrayList<>(); HalfIntegrator.getPrice(ISBN, prices); Double price = prices.get(0); } } class HalfIntegrator { public static void getPrice(int ISBN, List prices) { prices.add(45); } } interface Container { void set(T a); T get(); } class Store implements Container { private T a; public void set(T a) { this.a = a; } public T get() { return a; } }