0

I have a scenario to count number of names which is stored in the LinkedHashMap , and names can be duplicated but i should not count the duplicate name.

Below is Sample code:

         LinkedHashMap<Long,MyApplicationDTO> myApps = (LinkedHashMap<Long,MyApplicationDTO>) request.getAttribute("data");
                  for (Map.Entry app : myApps.entrySet()) {                  
                  Long  ID = (Long)app.getKey() ;
                  MyApplicationDTO singleMyApp =  (MyApplicationDTO) app.getValue();

        LinkedHashMap<Long, MyDTO> myList  = singleMyApp.getMyList();
          String name = "";  
        for (Map.Entry details : myList.entrySet()) {      
            Long id1 = (Long)details.getKey() ;
            MyDTO myDetails =  (MyDTO) details.getValue();

            name = myDetails.getName(); // For first time it stores A
            //how to loop so that i can only get the count of names as 3 by avoiding duplicate names from the below shown list.
            //A B A B A B C

}

}

On the Screen i have something as below:

Name : A B A B A B C

I have to print the count of the name as 3(non repeating names)

user222
  • 587
  • 3
  • 10
  • 31
  • http://stackoverflow.com/questions/4415803/java-code-to-prevent-duplicate-key-value-pairs-in-hashmap-hashtable – Ya Wang Feb 10 '15 at 18:19
  • Do you don't want to save the duplicates in your Map? Or just want to skip the duplicates while counting? – Vivin Feb 10 '15 at 18:33
  • No , i want to count the number of names using myDetails.getName() from LinkedHashMap but should not count the repeated names. @Vwin. – user222 Feb 10 '15 at 18:34
  • 1
    You can use a set for unique Names and get its size – Vivin Feb 10 '15 at 18:35
  • @Vwin , i want to skip the duplicates names while counting , so my output should give me name count as 3. thanks. – user222 Feb 10 '15 at 18:35

4 Answers4

3

As you iterate over the entrySet, add all names to a Set<String>. Then output set.size().

The Set will not add duplicates when you add names by set.add(name), so the size of the set will be the count of uniqe names.

duckstep
  • 1,148
  • 8
  • 17
2
LinkedHashMap<Long,MyApplicationDTO> myApps = (LinkedHashMap<Long,MyApplicationDTO>) request.getAttribute("data");
for (Map.Entry app : myApps.entrySet()) {                  
Long  ID = (Long)app.getKey() ;
MyApplicationDTO singleMyApp =  (MyApplicationDTO) app.getValue();

LinkedHashMap<Long, MyDTO> myList  = singleMyApp.getMyList();
String name = "";
Set<String> uniqueNames = new HashSet<String>();
for (Map.Entry details : myList.entrySet()) {      
     Long id1 = (Long)details.getKey() ;
     MyDTO myDetails =  (MyDTO) details.getValue();

     name = myDetails.getName(); // For first time it stores A
       //how to loop so that i can only get the count of names as 3 by avoiding duplicate names from the below shown list.
            //A B A B A B C
     uniqueNames.add(name);

}

}

To get size do =

uniqueNames.size();
Vivin
  • 1,327
  • 2
  • 9
  • 28
1

I'm not sure I'm understanding your question entirely, but if you're just looking to count the occurrences of unique values in the LinkedHashmap you can do something like this `

public static void main(String[] args) {

    LinkedHashMap<Long, String> myApps = new LinkedHashMap<Long, String>();
    myApps.put(4L, "A");
    myApps.put(14L, "B");
    myApps.put(44L, "A");
    myApps.put(54L, "B");
    myApps.put(46L, "A");
    myApps.put(543L, "B");
    myApps.put(144L, "C");

    ArrayList<String> names = new ArrayList<String>();

    for (Map.Entry app : myApps.entrySet()) {

        if (!(names.contains(app.getValue()))) {
            names.add(app.getValue().toString());
        }
    }

    System.out.println(names.size());
    for (String s : names ) {
        System.out.print(s + " ");
    }
}
tlw11591
  • 330
  • 1
  • 3
  • 6
  • 1
    Looks like @duckstep gave the idea, so credit to him. Here's a representation of it. Output : 3 A B C – tlw11591 Feb 10 '15 at 18:41
1

One liner with JAVA 8 Stream API

LinkedHashMap<Long, MyDTO> myList  = singleMyApp.getMyList();

long disctinctNamesCount = myList.values().stream().map(MyDTO::getNAme).distinct().count();
iamiddy
  • 3,015
  • 3
  • 30
  • 33