Java - 如何实现一个数据结构,其中包含记录在每个域及其下的每个子域上的点击次数

Java - How to implement a data structure that contains the number of clicks that were recorded on each domain AND each subdomain under it

我有一个问题陈述,如下所述: 编写一个函数,将此输入作为参数和 returns 包含

数量的数据结构

输入数据如下:

 String[] counts = {
                  "900,google.com",
                  "60,mail.yahoo.com",
                  "10,mobile.sports.yahoo.com",
                  "40,sports.yahoo.com",
                  "300,yahoo.com",
                  "10,whosebug.com",
                  "20,overflow.com",
                  "5,com.com",
                  "2,en.wikipedia.org",
                  "1,m.wikipedia.org",
                  "1,mobile.sports",
                  "1,google.co.uk"
                };

以下是预期输出:

calculateClicksByDomain(counts) =>
com:                     1345
google.com:              900
whosebug.com:       10
overflow.com:            20
yahoo.com:               410
mail.yahoo.com:          60
mobile.sports.yahoo.com: 10
sports.yahoo.com:        50
com.com:                 5
org:                     3
wikipedia.org:           3
en.wikipedia.org:        2
m.wikipedia.org:         1
mobile.sports:           1
sports:                  1
uk:                      1
co.uk:                   1
google.co.uk:            1

我试图为上面的问题陈述写一个解决方案:

Map<String, Integer> calculateClicksByDomainMap = new HashMap<>();
        for(int i = 0; i < counts.length; i++) {
            String[] seperateClickCountsAtComma = counts[i].split("\,");
            for(int j = 0; j < seperateClickCountsAtComma.length; j += 2) {
                String clickCounts = seperateClickCountsAtComma[j];
                String domain = seperateClickCountsAtComma[j+1];
                calculateClicksByDomainMap.put(domain, Integer.parseInt(clickCounts));
            }
        }
        
        for(Entry<String, Integer> domainCounts : calculateClicksByDomainMap.entrySet()) {
            String domainName = domainCounts.getKey();
            Integer domainCount = domainCounts.getValue();          
            splitStringOnOccurenceOfDot(domainName);
            //System.out.println(domainName + " " + domainCount);
                //String test[] = domainName.split("\.");
                //System.out.println(test[0] + "=======" + test[1] + "-----");
        }


public static String splitStringOnOccurenceOfDot(String domainName) {
        if(!domainName.contains(".")) {
            return domainName;
        }
        String[] subdomain  = domainName.split("\.");
        domainName = subdomain[1];
        System.out.println(domainName + "===============" );
        return splitStringOnOccurenceOfDot(domainName);
    }

但是,我不确定如何使用递归拆分字符串。 谁能帮我编写代码以获得预期输出的有效方法是什么? 我有办法使用递归来解决它吗? 谢谢你的时间。

Map 需要 ConcurrentHashMap,以允许并发更新。

第一个嵌套的for循环可以像下面这样调用递归方法,可能不需要另一个for循环:

    Map<String, Integer> calculateClicksByDomainMap = new ConcurrentHashMap<>();
    for (final String count : counts) {
      String[] separateClickCountsAtComma = count.split("\,");
      for (int j = 0; j < separateClickCountsAtComma.length; j += 2) {
        Integer clickCount = Integer.parseInt(separateClickCountsAtComma[j]);
        String domain = separateClickCountsAtComma[j + 1];
        calculateClicksByDomainMap.put(domain, clickCount);
        splitStringOnOccurenceOfDot(domain.substring(domain.indexOf(".")+1), clickCount, calculateClicksByDomainMap);
      }
    }

递归 add/update 映射自身而不是返回值。

  public static void splitStringOnOccurenceOfDot(String domainName, Integer domainCount, Map<String, Integer> calculateClicksByDomainMap) {
    if(calculateClicksByDomainMap.containsKey(domainName)) {
      
      Integer newCount = calculateClicksByDomainMap.get(domainName) + domainCount;
      calculateClicksByDomainMap.put(domainName, newCount);
    } else {
      calculateClicksByDomainMap.put(domainName, domainCount);
    }
    if(domainName.contains(".")) {
      splitStringOnOccurenceOfDot(domainName.substring(domainName.indexOf(".")+1), domainCount, calculateClicksByDomainMap);
    }
  }