Here you can find the source of max(List extends T> a, List extends T> b)
Parameter | Description |
---|---|
T | a parameter |
a | a parameter |
b | a parameter |
@SuppressWarnings("unchecked") public static <T> List<T> max(List<? extends T> a, List<? extends T> b)
//package com.java2s; /* License as published by the Free Software Foundation; either */ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { /**/*from w w w .ja v a 2 s.com*/ * @param <T> * @param a * @param b * @return */ @SuppressWarnings("unchecked") public static <T> List<T> max(List<? extends T> a, List<? extends T> b) { if (a == null) return (List<T>) b; if (b == null) return (List<T>) a; List<T> result = new ArrayList<T>(a); Map<T, Integer> coeffs = new HashMap<T, Integer>(); buildMap(a, coeffs); for (T t : b) { Integer coeff = coeffs.remove(t); if (coeff != null) { int newCoeff = coeff - 1; if (newCoeff > 0) coeffs.put(t, newCoeff); } else { result.add(t); } } return result; } private static <T> void buildMap(List<? extends T> a, Map<T, Integer> coeffs) { for (T t : a) { Integer coeff = coeffs.get(t); if (coeff == null) { coeffs.put(t, 1); } else { coeffs.put(t, coeff + 1); } } } }