Created
April 23, 2018 17:49
-
-
Save d630/a25ed5e09efcac17089ee2897db928f3 to your computer and use it in GitHub Desktop.
Playing with HashMaps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 19 wikipedia | |
| 10 featured | |
| 9 free | |
| 9 more | |
| 8 page | |
| 7 articles | |
| 7 other | |
| 6 english | |
| 6 her | |
| 6 from |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // good read: http://www.novixys.com/blog/java-hashmap-examples/ | |
| //package webseite; | |
| import java.io.BufferedReader; | |
| import java.io.ByteArrayInputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.nio.charset.StandardCharsets; | |
| import java.util.HashMap; | |
| import org.jsoup.Jsoup; | |
| import org.jsoup.nodes.Document; | |
| public class Webseite { | |
| public static void main(String[] args) throws IOException { | |
| Document doc = Jsoup.connect("http://en.wikipedia.org/").get(); | |
| HashMap<String, Integer> mapFreq = new HashMap<>(); | |
| HashMap<String,Integer> mapNegative = new HashMap<>(); | |
| mapNegative.put("a", 0); | |
| mapNegative.put("an", 0); | |
| mapNegative.put("and", 0); | |
| mapNegative.put("as", 0); | |
| mapNegative.put("at", 0); | |
| mapNegative.put("by", 0); | |
| mapNegative.put("d630", 0); | |
| mapNegative.put("for", 0); | |
| mapNegative.put("in", 0); | |
| mapNegative.put("is", 0); | |
| mapNegative.put("it", 0); | |
| mapNegative.put("its", 0); | |
| mapNegative.put("of", 0); | |
| mapNegative.put("on", 0); | |
| mapNegative.put("s", 0); | |
| mapNegative.put("than", 0); | |
| mapNegative.put("that", 0); | |
| mapNegative.put("the", 0); | |
| mapNegative.put("this", 0); | |
| mapNegative.put("to", 0); | |
| mapNegative.put("was", 0); | |
| mapNegative.put("with", 0); | |
| InputStream inputStream = new ByteArrayInputStream( | |
| doc | |
| .body() | |
| .text() | |
| .toLowerCase() | |
| .replaceAll("([\\p{Punct}]|[/–·])+", " ") | |
| .replaceAll("\\s+", "\n") | |
| //.replaceAll("[0-9]+", "NUMBER") | |
| .getBytes(StandardCharsets.UTF_8)); | |
| BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | |
| String line; | |
| while ((line = reader.readLine()) != null) { | |
| if (mapFreq.containsKey(line)) { | |
| mapFreq.put(line, mapFreq.get(line) + 1); | |
| } else { | |
| if (! mapNegative.containsKey(line)) { | |
| mapFreq.put(line, 1); | |
| } | |
| } | |
| } | |
| //mapFreq.remove("NUMBER"); | |
| mapFreq | |
| .entrySet() | |
| .stream() | |
| .sorted((x, y) -> y.getValue().compareTo(x.getValue())) | |
| .limit(10) | |
| .forEach(x -> System.out.println(x.getValue() + " " + x.getKey())); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment