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 796543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:39:23+00:00 2026-05-14T22:39:23+00:00

I have a class, the outline of which is basically listed below. import org.apache.commons.math.stat.Frequency;

  • 0

I have a class, the outline of which is basically listed below.

import org.apache.commons.math.stat.Frequency;
public class WebUsageLog {
    private Collection<LogLine> logLines;
    private Collection<Date> dates;

    WebUsageLog() {
        this.logLines = new ArrayList<LogLine>();
        this.dates = new ArrayList<Date>();
    }

    SortedMap<Double, String> getFrequencyOfVisitedSites() {
        SortedMap<Double, String> frequencyMap = new TreeMap<Double, String>(Collections.reverseOrder()); //we reverse order to sort from the highest percentage to the lowest.
        Collection<String> domains = new HashSet<String>();
        Frequency freq = new Frequency();
        for (LogLine line : this.logLines) {
            freq.addValue(line.getVisitedDomain());
            domains.add(line.getVisitedDomain());
        }

        for (String domain : domains) {
            frequencyMap.put(freq.getPct(domain), domain);
        }

        return frequencyMap;
    }
}

The intention of this application is to allow our Human Resources folks to be able to view Web Usage Logs we send to them. However, I’m sure that over time, I’d like to be able to offer the option to view not only the frequency of visited sites, but also other members of LogLine (things like the frequency of assigned categories, accessed types [text/html, img/jpeg, etc…] filter verdicts, and so on). Ideally, I’d like to avoid writing individual methods for compilation of data for each of those types, and they could each end up looking nearly identical to the getFrequencyOfVisitedSites() method.

So, my question is twofold: first, can you see anywhere where this method should be improved, from a mechanical standpoint? And secondly, how would you make this method more generic, so that it might be able to handle an arbitrary set of data?

  • 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-14T22:39:24+00:00Added an answer on May 14, 2026 at 10:39 pm

    This is basically the same thing as Eugene’s solution, I just left all the frequency calculation stuff in the original method and use the strategy only for getting the field to work on.

    If you don’t like enums you could certainly do this with an interface instead.

    public class WebUsageLog {
        private Collection<LogLine> logLines;
        private Collection<Date> dates;
    
        WebUsageLog() {
            this.logLines = new ArrayList<LogLine>();
            this.dates = new ArrayList<Date>();
        }
    
        SortedMap<Double, String> getFrequency(LineProperty property) {
            SortedMap<Double, String> frequencyMap = new TreeMap<Double, String>(Collections.reverseOrder()); //we reverse order to sort from the highest percentage to the lowest.
            Collection<String> values = new HashSet<String>();
            Frequency freq = new Frequency();
            for (LogLine line : this.logLines) {
                freq.addValue(property.getValue(line));
                values.add(property.getValue(line));
            }
    
            for (String value : values) {
                frequencyMap.put(freq.getPct(value), value);
            }
    
            return frequencyMap;
        }
    
        public enum LineProperty {
            VISITED_DOMAIN {
                @Override
                public String getValue(LogLine line) {
                    return line.getVisitedDomain();
                }
            },
            CATEGORY {
                @Override
                public String getValue(LogLine line) {
                    return line.getCategory();
                }
            },
            VERDICT {
                @Override
                public String getValue(LogLine line) {
                    return line.getVerdict();
                }
            };
    
            public abstract String getValue(LogLine line);
        }
    }
    

    Then given an instance of WebUsageLog you could call it like this:

    WebUsageLog usageLog = ...
    SortedMap<Double, String> visitedSiteFrequency = usageLog.getFrequency(VISITED_DOMAIN);
    SortedMap<Double, String> categoryFrequency = usageLog.getFrequency(CATEGORY);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have class like this below shown. which contains the shopping items where the
I have Class and Student objects. Both have collection of another as property. Which
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have class with back reference: public class Employee : Entity { private string
We have class lua. In lua class there is a method registerFunc() which is
I have class that looks like this: class A { public: class variables_map vm
I am just playing around with threads in java. I have a class which
I have a class called SynonymMapping which has a collection of values mapped as
I have created a simple layout with Html and CSS which is like below

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.