I have a string that I am converting to an ArrayList with the intention of counting the number of particular values in the list.
My string may look like this :
String myString = "Living Room, Bedroom, Bedroom, Bedroom, Bathroom, Bathroom, Family Room."
I'm currently finding the number of occurences of unique values in this way:
Map<String, Long> count = Stream.of(myString.split(",")).collect(Collectors.groupingBy(w -> w, Collectors.counting()));
What's the best way to go about this so that I can print something along the lines of "My home includes 1 Living Room, 3 Bedrooms, 2 Bathrooms, and 1 Family Room."
For the moment, my solution is the following. I haven't yet accepted an answer, because I think the way I followed through is a little clunky:
String newDescription = "My Home includes ";
public String getDescription(){
String myDescription = home.getDescription();
Map<String, Long> count = Stream.of(myDescription.split(",")).collect(Collectors.groupingBy(w -> w, Collectors.counting()));
for (Map.Entry<String, Long> entry : count.entrySet()){
newDescription = newDescription + entry.getValue() + " " + entry.getKey() + "(s), ";
}
return newDescription;
}
I was previously going about it in this way, but another SO user suggested the change:
public ArrayList convertDescription(){
//getDescription is inherited from another class to provide a string like the myString example above
String myDescription = home.getDescription();
ArrayList<String>Map<String, listDescriptionLong> =count new= ArrayList<String>(ArraysStream.asListof(myDescription.split(",")));
int livingRoomOccurrences = Collections.frequencycollect(listDescription, "Living Room");
int bedroomOccurrences = CollectionsCollectors.frequencygroupingBy(listDescription, "Bedroom");
int bathroomOccurrencesw =-> Collections.frequency(listDescriptionw, "Bathroom");
int familyRoomOccurrences = CollectionsCollectors.frequencycounting(listDescription, "Family Room");
int kitchenOccurrences = Collections.frequency(listDescription, "Kitchen");
int walkInClosetOccurrences = Collections.frequency(listDescription, "Walk-in Closet");
//not sure what I should return to make it easy to override getDescription with the proper string formatting
return listDescription;
}
public String getDescription(){
return home.getDescription();
}