1

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();
}

1 Answer 1

1

I suggest you get the frequency of all elements and extract what you need rather than scanning the collection again and again.

Map<String, Long> count = Stream.of(myString.split(","))
                            .collect(Collectors.groupingBy(w -> w, Collectors.counting());
Sign up to request clarification or add additional context in comments.

5 Comments

Okay, this looks more elegant. I'm trying to get more info on the w -> w syntax...I'm new to java
@user25976 it's a function which returns whatever you are given. This is needed as the first function/argument to groupingBy has to provide the key, but you want the key to be the word unaltered so the function doesn't do anything. e.g. say you did w -> w.toLowerCase() it would count the words in lower case so that the case would be ignored. This means that "and" and "And" and "AND" would all be counted as "and"
@user25976 you could do w -> w.length() This would make the key, the length of the word instead of the words itself. Or you could so w -> w.charAt(0) and the key would be the first letter of each word instead.
Do you have any suggestions on how to use the map? I've been working on a solution, but haven't gotten any closer than just retrieving the keys and values individually. I think my difficulty is not necessarily knowing which strings will be present.
@user25976 You don't need to know which strings/keys are present. You can just iterate over all the entries in a for-loop or use another stream to process the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.