-3

A Java library which I am using throws 'tab' seperated values as (per-line)single String output shown below

ID1 John
ID2 Jerry
ID3 John
ID4 Mary
ID5 John

I am trying to get the names and its frequency

John  3
Jerry 1
Mary  1

Is there a way to achieve this using regex (substring match then take the frequency count)

Betafish
  • 1,212
  • 3
  • 20
  • 45

1 Answers1

0

Is there a way to achieve this using regex (substring match then take the frequency count)?

This is not 100% possible, if it is not impossible, so you can create your own simple program to solve this problem.

Here is a simple piece of code can solve your problem :

public static void main(String[] args) {
    String str = "ID1 John\n"
            + "ID2 Jerry\n"
            + "ID3 John\n"
            + "ID4 Mary\n"
            + "ID5 John";

    //replace all the first part which contain (ID_Number_Space)
    //And split with \n
    String spl[] = str.replaceAll("(ID\\d+\\s)", "").split("\n");

    //result of this array is [John, Jerry, John, Mary, John]

    //create a map, which contain your key (name) value (nbr occurrence)
    Map<String, Integer> map = new HashMap<>();
    for (String s : spl) {
        if (map.containsKey(s)) {
            map.put(s, map.get(s) + 1);
        } else {
            map.put(s, 1);
        }
    }

    //Print your array
    for (Map.Entry entry : map.entrySet()) {
        System.out.println(entry.getKey() + " - " + entry.getValue());
    }
}

OutPut

John - 3
Jerry - 1
Mary - 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140