Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Exploring the Java Collection Framework Maps

Kenzo Qui
Kenzo Qui
2,350 Points

What is stopping me to make treetsByAuthor a method in Treet.java

I'm curious though, why not? From the objective I did I made it into a method but why make it a variable, why not make it a method? I want your opinion

No it isn't a bot. I'm asking you to post your code and explain what you expect your code to do and what you code is doing.

I don't know what you mean by 'why store it in a variable rather than make it a variable'.

I'd like you to post your code and explain what isn't working how you think it should. Or explain what you would do differently.

Do that and I can help.

Steve - not a bot, but a moderator.

Kenzo Qui
Kenzo Qui
2,350 Points

sorry steve, it's method and never mind thank you,

2 Answers

Sebastian Röder
Sebastian Röder
13,878 Points

Nothing is stopping you from making it a method; it even is a good idea in my opinion.

The only gotcha is that you must make it a static method and pass in the treets as an argument. This is because treetsByAuthor() must know about all the treets, not just one instance of a treet.

Here is my example for getHashTagCounts() that I made into a static method in Treet.java. You can try to follow the same pattern for treetsByAuthor():

    public static Map<String, Integer> getHashTagCounts(List<Treet> treets) {
        Map<String, Integer> hashTagCounts = new TreeMap<>();

        for (Treet treet : treets) {
            for (String hashTag : treet.getHashTags()) {
                hashTagCounts.putIfAbsent(hashTag, 0);
                hashTagCounts.put(hashTag, hashTagCounts.get(hashTag) + 1);
            }
        }

        return hashTagCounts;
    }

The only downside is that the Treet class is starting to gather many different responsibilities, potentially violating the Single Responsibility Pattern. So it might be even better from an Object-oriented Design standpoint to move the method into a separate class that deals with a collection of Treets. It could be called Treets.java (plural), TreetList.java, TreetCollection.java, TwitterTimeline.java, … (you get the idea).

Hi,

Can you share your code that's not working?

Steve.

Kenzo Qui
Kenzo Qui
2,350 Points

It's working, I just want to know why can't it be a method/can it be a method?

Can you post your code? Explain what isn't working as you expect. We can then help.

Steve.

Kenzo Qui
Kenzo Qui
2,350 Points

It is working. What I am curious about is why store it in a variable instead of making it a variable. I can honestly think of a reason but I want your opinion. S.S. Is this a bot?