Here you can find the source of findMaximalMatch(List
public static Map<String, Object> findMaximalMatch(List<Map<String, Object>> mapList, LinkedHashMap<String, Object> fieldsByPriority)
//package com.java2s; /*// ww w. ja v a2 s .c om * This software is in the public domain under CC0 1.0 Universal plus a * Grant of Patent License. * * To the extent possible under law, the author(s) have dedicated all * copyright and related and neighboring rights to this software to the * public domain worldwide. This software is distributed without any * warranty. * * You should have received a copy of the CC0 Public Domain Dedication * along with this software (see the LICENSE.md file). If not, see * <http://creativecommons.org/publicdomain/zero/1.0/>. */ import java.util.*; public class Main { /** * For a list of Map find the entry that best matches the fieldsByPriority Ordered Map; null field values in a Map * in mapList match against any value but do not contribute to maximal match score, otherwise value for each field * in fieldsByPriority must match for it to be a candidate. */ public static Map<String, Object> findMaximalMatch(List<Map<String, Object>> mapList, LinkedHashMap<String, Object> fieldsByPriority) { int numFields = fieldsByPriority.size(); String[] fieldNames = new String[numFields]; Object[] fieldValues = new Object[numFields]; int index = 0; for (Map.Entry<String, Object> entry : fieldsByPriority.entrySet()) { fieldNames[index] = entry.getKey(); fieldValues[index] = entry.getValue(); index++; } int highScore = -1; Map<String, Object> highMap = null; for (Map<String, Object> curMap : mapList) { int curScore = 0; boolean skipMap = false; for (int i = 0; i < numFields; i++) { String curField = fieldNames[i]; Object compareValue = fieldValues[i]; // if curMap value is null skip field (null value in Map means allow any match value Object curValue = curMap.get(curField); if (curValue == null) continue; // if not equal skip Map if (!curValue.equals(compareValue)) { skipMap = true; break; } // add to score based on index (lower index higher score), also add numFields so more fields matched weights higher curScore += (numFields - i) + numFields; } if (skipMap) continue; // have a higher score? if (curScore > highScore) { highScore = curScore; highMap = curMap; } } return highMap; } }