1

I have an ArrayList from recursively crawling through a directory

[project1_john_document1, project1_john_document2, project2_jose_document1, project2_jose_document2, project3_juan_document1, ...... ]

I am trying to count the instances for each project to get the following output

project1 = 3, 
project2 = 2,
project3 = 1, ....

What I have done is to Iterate through the list but somehow I am stuck as to how to get the "project1" as a common project, as there are numerous project names on the directory. I tried splitting the string using the split("_"), but since I am entirely noob, I couldn't get the logic of classifying the different project name.

Newbie in java here and sorry for the vague phrasing of my question.

2
  • 2
    if you would show us your code, we can tell what's wrong with it. Commented May 17, 2017 at 8:58
  • Take a look on regex, if you want to find that pattern inside your array, and when a match occurs, count it. tutorialspoint.com/java/java_regular_expressions.htm Commented May 17, 2017 at 9:01

5 Answers 5

1

You can use regex to get all the projects names, then you can use a Map for example :

String str = "[project1_john_document1, project1_john_document2, project2_jose_document1, project2_jose_document2, project3_juan_document1]";
Pattern p = Pattern.compile("project\\d+");
Matcher m = p.matcher(str);
Map<String, Integer> map = new HashMap<>();
String project;
while (m.find()) {
    project = m.group();
    if (map.containsKey(project )) {
        map.put(project , map.get(project ) + 1);
    } else {
        map.put(project , 1);
    }
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + "\t" + entry.getValue());
}

Outputs

project2    2
project1    2
project3    1

regex demo

Sign up to request clarification or add additional context in comments.

Comments

1

If the pattern is simple - projectNumber before "_" this one do this job:

 Map<String, Long> projectNumbers = Arrays.asList("project1_john_document1", "project1_john_document2", "project2_jose_document1", "project2_jose_document2", "project3_juan_document1")
                .stream().map(s -> s.split("_")[0])
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Comments

1

If you want to get the count of how many project1, project2 and so on folders you have, you could achieve it with the following code:

    String[] names = {"project1_john_document1", "project1_john_document2", "project2_jose_document1", "project2_jose_document2", "project3_juan_document1"};
    Map<String, Integer> counts = new HashMap<>();
    for (String entry : names) {
        String project = entry.split("_")[0];
        int count = counts.containsKey(project) ? counts.get(project) : 0;
        counts.put(project, count + 1);
    }

    System.out.println(counts);
    // prints: {project2=2, project1=2, project3=1}

As the other answers mentioned, you could use regex, streams, etc. to do similar things. But the basic logic is the same: for each folder, get the root name, increment the counter in a map. If you're a beginner, I'd probably get my head around the most basic flow first before diving into slightly more complex things (e.g., streams).

Comments

1

define your custom Function that converts your string names into something more simpler to be compared, then collect that

List<String> myL = Arrays.asList("project1_john_document1", "project1_john_document2",
            "project2_jose_document1", "project2_jose_document2", "project3_juan_document1");
Function<String, String> myFRegex = t -> {
        return t.substring(0, t.indexOf("_"));
    };
Map<String, Long> primeFactorCount = myL.stream()
            .collect(Collectors.groupingBy(myFRegex, Collectors.counting()));
System.out.println(primeFactorCount);

the output can look like:

{project2=2, project1=2, project3=1}

Comments

0

This is the "Longest common substring problem". You can find an algorithm (in pseudo code) on the following article:

https://en.wikipedia.org/wiki/Longest_common_substring_problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.