0

I have a requirement to parse a file which contains 1000 of sentences in the file. where I have to find a unique word in a file means that has not come more than one time in the file. which data structure of should I use.(need to perform this operation using DS of java only)and why and how

2nd Question is

map.put("abc","hello");
map.put.("ABC","hi");

when we are inserting in map object as given above code what will happen.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56

2 Answers2

0

You can fill your words in a map witch has a key the word and the value the number of occurrence.
When you finish filling your data in your Map loop again again check the value if great then on or equal one, if it is great then one, remove it from your map. And you can return the size of this map in the end, to know the number of unique words


If you dont want to use just abc or ABC, you ca use LowerCase or UpperCase when you insert in your Map.

Here a simple example you can go throw :

public static void main(String[] args) {
    //i consider you know how to learn from a file, i use a simple array just to explain
    String[] listWords = {"hello", "word", "hello", "stack", "HELLO", "WORD", "123", "what?"};

    //fill your words in your map
    Map<String, Integer> map = new HashMap<>();
    for (String word : listWords) {
        if (!map.containsKey(word.toLowerCase())) {
            map.put(word.toLowerCase(), 1);
        } else {
            map.put(word.toLowerCase(), map.get(word.toLowerCase()) + 1);
        }
    }

    //print your map
    map.entrySet().forEach((entry) -> {
        System.out.println("word : " + entry.getKey() + " occurence : " + entry.getValue());
    });
    System.out.println("**************************************************");
    //loop throw your map and remove the words which occurrence > 1
    for (Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator(); it.hasNext();) {
        Map.Entry<String, Integer> entry = it.next();
        if (entry.getValue() > 1) {
            it.remove();
        }
    }

    //print your map again
    map.entrySet().forEach((entry) -> {
        System.out.println("word : " + entry.getKey() + " occurence : " + entry.getValue());
    });

    //size of your end map
    System.out.println("Size of map = " + map.size());
}

Some good references you can base one to play with maps:

How to update a value, given a key in a java hashmap?
iterating over and removing from a map

Hope this can gives you an idea.

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

Use Map, use the word as key and value as count, for every word you have put it in the map and increment the count by one.

if(map.containsKey("some")){
    // get the current value
    int currentValue = map.get("some");
    // put back the key with incremented value
    map.put("some",currentValue+1);
} else {
    // first time
    map.put("some",1);
}

For your second question, both put will be added in the Map since map key is case sensitive.

johnII
  • 1,423
  • 1
  • 14
  • 20