简体   繁体   中英

Counting appearance while using LinkedHashSet

I have an XML file in which I need to find and count the appearance of year tag. For example:

Found year 2020 10 times.
Found year 2017 1 times.
Found year 2019 2 times. 
(...)

To avoid the duplications of the years I used HashSet. Code:

public class Publications {
    public static void main(String[] args) throws IOException {
        Set<String> publicationYears = new LinkedHashSet<>();
        try (BufferedReader reader = Files.newBufferedReader(Paths.get("dblp-2020-04-01.xml"))) {
            Pattern pattern = Pattern.compile("<year>(.+?)</year>", Pattern.DOTALL);
            for (String line; (line = reader.readLine()) != null; ) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    String year = matcher.group(1);
                    publicationYears.add(year);
                }
            }
        }

Results:

2010
2002
1992
1994
1993
2006(...)

But now I can't find an efficient code to count the appearance of each year. Creating an multidimensional array and then searching would be very slow. Any suggestions?

Try this:

  • I replaced the set with a map.
  • The statement that does the work is
        count.compute(year, (k,v)->v == null ? 1 : v + 1); 
  • It simply puts 1 for the year if the year when it first encounters it, otherwise it adds 1 to that year.
   Map<String, Integer> count = new LinkedHashMap<>();
        try (BufferedReader reader = Files.newBufferedReader(Paths.get("dblp-2020-04-01.xml"))) {
            Pattern pattern = Pattern.compile("<year>(.+?)</year>", Pattern.DOTALL);
            for (String line; (line = reader.readLine()) != null; ) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    String year = matcher.group(1);
                    count.compute(year, (k,v)->v == null ? 1 : v + 1);
                }
            }
        }
    }

To print them out, do the following

count.entrySet().forEach(System.out::println);

There are so many ways to do it. Some of them are given below:

  1. Add all the years to a List and use Collections.frequency as follows:
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        List<Integer> years = List.of(2010, 2011, 2013, 2010, 2012, 2010, 2011, 2010, 2014);
        Set<Integer> yearSet = new LinkedHashSet<Integer>(years);
        for (Integer year : yearSet) {
            System.out.println("Found year " + year + " " + Collections.frequency(years, year) + " times");
        }
    }
}

Output:

Found year 2010 4 times
Found year 2011 2 times
Found year 2013 1 times
Found year 2012 1 times
Found year 2014 1 times
  1. Add all the years to a List and then create a Map of frequency as follows:
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static void main(String[] args) {
        List<Integer> years = List.of(2010, 2011, 2013, 2010, 2012, 2010, 2011, 2010, 2014);
        Map<Integer, Integer> frequencyMap = new LinkedHashMap<Integer, Integer>();
        for (Integer year : years) {
            if (frequencyMap.get(year) == null) {
                frequencyMap.put(year, 1);
            } else {
                frequencyMap.put(year, frequencyMap.get(year) + 1);
            }
        }
        for (Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
            System.out.println("Found year " + entry.getKey() + " " + entry.getValue() + " times");
        }
    }
}

Output:

Found year 2010 4 times
Found year 2011 2 times
Found year 2013 1 times
Found year 2012 1 times
Found year 2014 1 times
  1. Add all the years to a List and then create a Map of frequency by using Map::merge as follows:
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static void main(String[] args) {
        List<Integer> years = List.of(2010, 2011, 2013, 2010, 2012, 2010, 2011, 2010, 2014);
        Map<Integer, Integer> frequencyMap = new LinkedHashMap<Integer, Integer>();
        years.forEach(year -> frequencyMap.merge(year, 1, (oldValue, newValue) -> oldValue + newValue));
        for (Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
            System.out.println("Found year " + entry.getKey() + " " + entry.getValue() + " times");
        }
    }
}

Output:

Found year 2010 4 times
Found year 2011 2 times
Found year 2013 1 times
Found year 2012 1 times
Found year 2014 1 times

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM