From: Jérôme Benoit Date: Fri, 6 Apr 2018 09:03:00 +0000 (+0200) Subject: exo5: add filtred maps from the original tree map. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a1aa04de75e28577a5c1c28ff6cf25ca6b2f913c;p=Project_POO.git exo5: add filtred maps from the original tree map. Signed-off-by: Jérôme Benoit --- diff --git a/exo4/Main.java b/exo4/Main.java index 48b9391..a83fab4 100644 --- a/exo4/Main.java +++ b/exo4/Main.java @@ -39,7 +39,7 @@ class Main { } } catch (ParseException e) { - + e.printStackTrace(); } } } diff --git a/exo5/Main.java b/exo5/Main.java index c5b80c4..3e0f331 100644 --- a/exo5/Main.java +++ b/exo5/Main.java @@ -1,8 +1,10 @@ import java.io.File; import java.util.Scanner; import java.io.IOException; +import java.util.Map; import java.util.TreeMap; import java.util.SortedMap; +import java.util.stream.Collectors; class Main { @@ -11,15 +13,17 @@ class Main { * @param String[] args main() function arguments array */ public static void main(String[] args) { - //TreeMap tm = new TreeMap(new StrComparator()); // default TreeMap comparator keep alphabetical order - TreeMap tm = new TreeMap(); + // NOTE: words comparaison is case insensitive and punctuation aware + TreeMap tm = new TreeMap(new StrComparator()); + // NOTE: words comparaison is case sensitive and punctuation aware + //TreeMap tm = new TreeMap(); File f = new File("test_file.txt"); try { Scanner sc = new Scanner(f); while (sc.hasNext()) { String wordCursor = sc.next(); - //NOTE: words comparaison is case sensitive and punctuation aware + Integer count = tm.get(wordCursor); if (count == null) { count = 0; @@ -37,10 +41,26 @@ class Main { System.out.println("Word \"" + wordCursor + "\" occured " + tm.get(wordCursor) + " times"); } - System.out.println("SortedMap content after the word \"milieu\":"); - SortedMap sm = tm.tailMap("milieu"); + System.out.println("SortedMap content of words inferior to the word \"milieu\":"); + SortedMap sm = tm.headMap("milieu"); for (String wordCursor : sm.keySet()) { System.out.println("Word \"" + wordCursor + "\" occured " + sm.get(wordCursor) + " times"); } + + System.out.println("Map content of duplicates inferior to the word \"milieu\":"); + Map m1 = tm.entrySet().stream() + .filter(map -> map.getKey().compareTo("milieu") < 0 && map.getValue() == 2) + .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())); + for (String wordCursor : m1.keySet()) { + System.out.println("Word \"" + wordCursor + "\" occured " + sm.get(wordCursor) + " times"); + } + + System.out.println("Map content of duplicates with occurence between 2 and 5:"); + Map m2 = tm.entrySet().stream() + .filter(map -> map.getValue() >= 2 && map.getValue() <= 5) + .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())); + for (String wordCursor : m2.keySet()) { + System.out.println("Word \"" + wordCursor + "\" occured " + sm.get(wordCursor) + " times"); + } } } diff --git a/exo5/StrComparator.java b/exo5/StrComparator.java index b797083..0ce8f87 100644 --- a/exo5/StrComparator.java +++ b/exo5/StrComparator.java @@ -3,7 +3,7 @@ import java.util.Comparator; class StrComparator implements Comparator { public int compare(String str1, String str2) { - return str1.compareTo(str2); + return str1.compareToIgnoreCase(str2); } }