X-Git-Url: https://git.piment-noir.org/?p=Project_POO.git;a=blobdiff_plain;f=exo5%2FMain.java;h=1ec751215c7c5bd4710ca47cefe6a69f39c6a297;hp=4075cbc4ed5997f2f9c18bf6b6ec1b59a5fba719;hb=fb7a0378b9ea3a11f28beec72fd2fbce063fda3c;hpb=2260b7d67758bac45d08186f4eded0c2e679bd61 diff --git a/exo5/Main.java b/exo5/Main.java index 4075cbc..1ec7512 100644 --- a/exo5/Main.java +++ b/exo5/Main.java @@ -1,3 +1,8 @@ +import java.io.File; +import java.util.Scanner; +import java.io.IOException; +import java.util.TreeMap; +import java.util.SortedMap; class Main { @@ -6,6 +11,36 @@ 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(); + 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; + } + tm.put(wordCursor, count + 1); + } + sc.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + System.out.println("TreeMap full content:"); + for (String wordCursor : tm.keySet()) { + System.out.println("Word \"" + wordCursor + "\" occured " + tm.get(wordCursor) + " times"); + } + + System.out.println("SortedMap content after the word \"milieu\":"); + SortedMap stm = tm.tailMap("milieu"); + for (String wordCursor : stm.keySet()) { + System.out.println("Word \"" + wordCursor + "\" occured " + stm.get(wordCursor) + " times"); + } } }