Java List Max max(List a, List b)

Here you can find the source of max(List a, List b)

Description

max

License

Open Source License

Parameter

Parameter Description
T a parameter
a a parameter
b a parameter

Declaration

@SuppressWarnings("unchecked")
public static <T> List<T> max(List<? extends T> a, List<? extends T> b) 

Method Source Code


//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);
            }
        }
    }
}

Related

  1. getMaxValIndex(List vals)
  2. getMaxValue(List list)
  3. getMaxValue(List values)
  4. max(final List aList, final Double aDefalut)
  5. max(List values)
  6. max(List booleans)
  7. max(List a)
  8. max(List data)
  9. max(List l)