java - java中按两个字段对对象进行分组

标签 java

我有一堆日志文件,我想用 java 处理它们,但我想先对它们进行排序,这样我可以获得更多人类可读的结果。

我的日志类:

public class Log{
//only relevant fields here
private String countryCode;
private AccessType accessType;
...etc..
}

AccessType 是 Enum,其值为 WEB、API、OTHER。

我想按国家代码和访问类型对日志对象进行分组,以便最终产品将是日志列表。

我得到了这个工作,可以按国家代码将日志分组到日志列表中,如下所示:

public List<Log> groupByCountryCode(String countryCode) {
        Map<String, List<Log>> map = new HashMap<String, List<Log>>();
        for (Log log : logList) {
            String key = log.getCountryCode();
            if (map.get(key) == null) {
                map.put(key, new ArrayList<Log>());
            }
            map.get(key).add(log);
        }
        List<Log> sortedByCountryCodeLogList = map.get(countryCode);

        return sortedByCountryCodeLogList;
    }

来自这个@Kaleb Brasee 示例:

Group by field name in Java

这是我已经尝试了一段时间的方法,但现在真的陷入困境..

public List<Log> groupByCountryCode(String countryCode) {
        Map<String, Map<AccessType, List<Log>>> map = new HashMap<String, Map<AccessType, List<Log>>>();
        AccessType mapKey = null;
        List<Log> innerList = null;
        Map<AccessType, List<Log>> innerMap = null;
        // inner sort
        for (Log log : logList) {
            String key = log.getCountryCode();
            if (map.get(key) == null) {
                map.put(key, new HashMap<AccessType, List<Log>>());
                innerMap = new HashMap<AccessType, List<Log>>();
            }

            AccessType innerMapKey = log.getAccessType();
            mapKey = innerMapKey;
            if (innerMap.get(innerMapKey) == null) {
                innerMap.put(innerMapKey, new ArrayList<Log>());
                innerList = new ArrayList<Log>();
            }

            innerList.add(log);
            innerMap.put(innerMapKey, innerList);
            map.put(key, innerMap);
            map.get(key).get(log.getAccessType()).add(log);
        }

        List<Log> sortedByCountryCodeLogList = map.get(countryCode).get(mapKey);

        return sortedByCountryCodeLogList;
    }

我不确定我是否知道自己在做什么

最佳答案

你的问题很困惑。您想要对列表进行排序,但您正在创建许多新列表,然后丢弃除其中一个之外的所有列表?

这是一个对列表进行排序的方法。请注意,Collections.sort() 使用稳定排序。 (这意味着一组国家/地区代码和访问类型中项目的原始顺序将被保留。)

class MyComparator implements Comparator<Log> {
  public int compare(Log a, Log b) {
    if (a.getCountryCode().equals(b.getCountryCode()) {
      /* Country code is the same; compare by access type. */
      return a.getAccessType().ordinal() - b.getAccessType().ordinal();
    } else
      return a.getCountryCode().compareTo(b.getCountryCode());
  }
}
Collections.sort(logList, new MyComparator());

如果您确实想做代码当前正在做的事情,至少跳过创建不必要的列表:

public List<Log> getCountryAndAccess(String cc, AccessType access) {
  List<Log> sublist = new ArrayList<Log>();
  for (Log log : logList) 
    if (cc.equals(log.getCountryCode()) && (log.getAccessType() == access))
      sublist.add(log);
  return sublist;
}

关于java - java中按两个字段对对象进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5492761/

相关文章:

Java ProcessBuilder 问题

java - 右对齐图片上的文字

java - 在struts的索引属性中添加新项目

java - 使用 URLConnection 使用 REST Web 服务

java - 找不到/lib/libJOCL-linux-x86.so

java - 在我的 "Custom Set"实现中需要帮助

java - java中可以同时使用fork/join和executor服务吗?

java - 为什么我会使用传统数组而不是 ArrayList 对象?

java - SOAPFaultException 详细信息为 null

java - 调用其他方法和不可变实例