class TableIndexes {

    public TableIndex getBestTableIndexMatch(List<String> fields)
    {
        if(fields == null || fields.size() == 0) return null;
        if(fields.size() == 1) return getTableIndex(fields.get(0));

        // NOTE: this set technically includes more data than required in the lookup below (no harm)
        Set<String> otherFields = new HashSet<>(fields);

        TableIndex bestIndexMatch = null;
        for(String field : fields)
        {
            List<TableIndex> tableIndexSet = fieldToIndexSetMap.get(field);
            if(tableIndexSet == null || tableIndexSet.size() == 0) continue;

            // look for matching sort keys
            Optional<TableIndex> tableIndexSearch = tableIndexSet.stream()
                .filter(tableIndex -> otherFields.contains(tableIndex.getSortKey()))
                .findAny();
            // if both the primary and sort key are found this is a good index match
            if(tableIndexSearch.isPresent()) return tableIndexSearch.get();
            if(bestIndexMatch == null) bestIndexMatch = tableIndexSet.get(0);
        }
        return bestIndexMatch;
    }

}