-2

I'm a beginner in Java and is starting to learn how to use hashsets. I have a problem with my practice exercise. The goal is to remove the duplicate description and then adding the count of similar descriptions.

For example I have,

Computer Science - 15
Engineering - 20
Dentistry - 10
Architecture - 11
Computer Science - 25
Dentistry - 7

then the output should only be:

Computer Science - 40
Engineering - 20
Dentistry - 17
Architecture - 11

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    //printing summary report
    ArrayList<String>   summaryReport=  new ArrayList<String>();

    final String[] CourseDesc           = {"Computer Science", "Architecture", "Dentistry", "Computer Science"};
    final int[] CourseCount         = {15, 10, 10, 25};

    for (String element: CourseDesc) {
        for(int el: CourseCount){
             summaryReport.add(element + " "+ el);
        }
     }
   System.out.println(summaryReport);
  }
}

I'm just stuck on how to add the count of similar descriptions. Thanks!

Xiaoren
  • 29
  • 2
  • 7

2 Answers2

2

A smart way to solve you problem would be to use Java Streams:

final String[] courseDesc = {"Computer Science", "Engineering", "Dentistry", "Architecture", "Computer Science", "Dentistry"};
final int[] courseCount = {15, 20, 10, 11, 25, 7};

Map<String, Integer> summaryReport = IntStream.range(0, courseDesc.length).boxed()
    .collect(Collectors.toMap(
        index -> courseDesc[index], index -> courseCount[index],
        (count0, count1) -> count0 + count1, LinkedHashMap::new
    ));

This creates a Map with the course as key and the sum as value. If the map contains an course and the same course is collected again the lambda expression (count0, count1) -> count0 + count1 handles it by summing the values. The LinkedHashMap::new is to maintain the order.

Now you can print the summary like this:

summaryReport.forEach((course, count) -> System.out.println(course + " - " + count));

Result:

Computer Science - 40
Engineering - 20
Dentistry - 17
Architecture - 11
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
0

From OP I can see that you need an association key-value container, so the most obvious way will be to use Map, check if the value present as a key, if not - add it with 1 counter, if present - add + 1:

void test() {
    Map<String, Integer> summaryReport = new HashMap<>();
    String curDesc = "whatever";

    Integer count = summaryReport.get(curDesc);
    if (count == null) {
        summaryReport.put(curDesc, 1);
    } else {
        summaryReport.put(curDesc, count + 1);
    }
}

Doing in more tricky way, you can use mutable AtomicInteger as a counter:

void test() {
    Map<String, AtomicInteger> summaryReport = new HashMap<>();
    AtomicInteger counter = summaryReport.putIfAbsent("whatever", new AtomicInteger(0));
    if (counter != null) {
        counter.incrementAndGet();
    }
}
J-Alex
  • 6,881
  • 10
  • 46
  • 64