Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4231848
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:01:16+00:00 2026-05-21T02:01:16+00:00

I have bunch of log files and I want to process them in java,

  • 0

I have bunch of log files and I want to process them in java, but I want to sort them first so I can have more human readable results.

My Log Class :

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

AccessType is Enum, which has values WEB, API, OTHER.

I’d like to group Log objects by both countryCode and accessType, so that end product would be log list.

I got this working for grouping Logs into log list by countryCode like this :

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

from this @Kaleb Brasee example :

Group by field name in Java

Here is what I’ve been trying for some time now, and really stuck now ..

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

I’m not sure I know what I’m doing anymore

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:01:17+00:00Added an answer on May 21, 2026 at 2:01 am

    Your question is confusing. You want to sort the list, but you are creating many new lists, then discarding all but one of them?

    Here is a method to sort the list. Note that Collections.sort() uses a stable sort. (This means that the original order of items within a group of country code and access type is preserved.)

    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());
    

    If you really want to do what your code is currently doing, at least skip the creation of unnecessary lists:

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of log files. I need to find out how many
I have a bunch of log files which are pure text. Here is an
I have a bunch of console.log() calls in my JavaScript. Should I comment them
I have a bunch of classes I want to rename. Some of them have
I have a folder with a bunch of log files. Each set of log
i have bunch of log files and I have to delete the files of
I have a bunch of files containing the exact same log message. One of
I have to parse a big bunch of log files, which are in the
I have a bunch of numbers represented as hexadecimal strings in log files that
Does Java and/or Spring have the concept of properties? I have bunch of domain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.