Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

ada shatan posted:

The relevant file is here: https://controlc.com/1d390e17
password is "goonhomework"

It looks like you'd be well within the guidelines of the assignment to just change the map to use a Long, but if that isn't your intent Kilson's solution looks great. I'm sure myself and others would feel a lot more comfortable if you explained how Kilson's solution works before you hand it in as your own, but you do what works for you.

To be fair, I wouldn't really call it *my* solution. I only added the part that converts the list of longs to ints.

collectingAndThen() is also something I've never used before, or seen anyone else use ever. It seems unlikely it's intended to be used in this homework, but who knows.

Adbot
ADBOT LOVES YOU

ada shatan
Oct 20, 2004

that'll do pig, that'll do

Kilson posted:

To be fair, I wouldn't really call it *my* solution. I only added the part that converts the list of longs to ints.

collectingAndThen() is also something I've never used before, or seen anyone else use ever. It seems unlikely it's intended to be used in this homework, but who knows.

Sorry, you touched the code last and everyone likes it, the whole solution is yours now. If we encounter problems with it we'll call you. Thanks.

samcarsten
Sep 13, 2022

by vyelkin
okay, next loop.

code:
for (String word : document) {
					Integer cnt = wordFrequency.get(word);
					if (cnt != null) {
						if (cnt > maxCnt) {
							maxCnt = cnt;
						}
					}
				}
So far I have

maxCnt = document.stream().filter().collect();

and I need to figure out what the exact filter lambda should be, but I'm already getting the same error we've been working with.

The method stream() is undefined for the type Map<String,Integer>

samcarsten fucked around with this message at 20:59 on Nov 27, 2022

samcarsten
Sep 13, 2022

by vyelkin
Ok, taking another look, I need to figure out how to get cnt in there, I think. Should both objects be in there so i can compare them or is there another way?

samcarsten
Sep 13, 2022

by vyelkin
Ok, I don't think I need collect. Can you end a stream with a non terminal operation?

Edit: No, you can't, but count should work. Now do I get it to count word though?

CPColin
Sep 9, 2003

Big ol' smile.
You can't stream a Map on its own, but you can stream its keys, values, or entries, depending on what information you need.

samcarsten
Sep 13, 2022

by vyelkin
okay, current stream is

maxCnt = document.stream().filter().count();

but I still don't know how to filter it. I need to create cnt because its the only way to get wordFrequency(word), but it doesn't work.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
You should look at some of the other stream methods and think about whether they might be helpful to solve your problem.

Eezee
Apr 3, 2011

My double chin turned out to be a huge cyst
The loop you are trying to replace here is either complete garbage or intentionally confusing.

Try to think about what that loop is actually doing and look on the official documentation for java streams which methods you need. Also think about why they are iterating over document instead of the entries in wordFrequency.

samcarsten
Sep 13, 2022

by vyelkin
Ugh, I'm just not understanding how to do this one.

CPColin
Sep 9, 2003

Big ol' smile.
What's in the wordFrequency map and what are you trying to find?

samcarsten
Sep 13, 2022

by vyelkin

CPColin posted:

What's in the wordFrequency map and what are you trying to find?

I need to find the frequency of each word, then compare it to maxcnt to find the word that occurs most. Wait, can I just get max of wordfrequency with streams?

CPColin
Sep 9, 2003

Big ol' smile.
Only one way to find out! Just remember what I said earlier about Maps and streams:

CPColin posted:

You can't stream a Map on its own, but you can stream its keys, values, or entries, depending on what information you need.

samcarsten
Sep 13, 2022

by vyelkin
ok, did some googling, think I have something that should work, but I'm getting an error.

maxCnt = wordFrequency.stream().filter(entry -> value.equals(entry.getValue())).map(Map.Entry::getKey).max();

So, it streams wordfrequency, gets the values for each entry, then maps the keys for each entry, then gets the max integer because word frequency is string, integer. The error i'm getting is

The method stream() is undefined for the type Map<String,Integer>

Eezee
Apr 3, 2011

My double chin turned out to be a huge cyst
You need to look at the API of maps before you just guess code. Your IDE already tells you that you cannot stream maps. It should also tell you all methods that maps provide. If you are just using a text editor for some reason you need to look at the official documentation here https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/Map.html

You really need to learn how to use documentation to see what is actually possible before writing code.
Also what are you filtering? And what are you using that map for?

samcarsten
Sep 13, 2022

by vyelkin
When I looked up how to do this, the examples used this format. For example, https://stackoverflow.com/questions/30608360/java-8-extract-first-key-from-matching-value-in-a-map

Was I wrong in understanding what they were doing?

CPColin
Sep 9, 2003

Big ol' smile.
The only thing in that answer that will help you is the part right before stream()

samcarsten
Sep 13, 2022

by vyelkin

CPColin posted:

The only thing in that answer that will help you is the part right before stream()

ok, looked that up, and it seems i can call values directly using values(), but now I'm getting an error.

maxCnt = wordFrequency.values().stream().max();

The method max(Comparator<? super Integer>) in the type Stream<Integer> is not applicable for the arguments ()

I'm really not liking this topic.

Eezee
Apr 3, 2011

My double chin turned out to be a huge cyst
That's more like it. You could look it up on stack overflow, but you need to do .max(Comparator.naturalOrder()) for it to work.

If you only need to know how often the most common word appears you are now done. If you want to also know what the most common word is, you need to use a different Comparator and you cannot use wordFrequency.values(), since that only contains the frequency of words and not the word itself.
Even if you don't have to do it for your homework, you should try to do it by yourself by looking at the Comparator and Map documentation.

samcarsten
Sep 13, 2022

by vyelkin
Ok, got an error on that.

maxCnt = wordFrequency.values().stream().max(Comparator.naturalOrder());

Type mismatch: cannot convert from Optional<Integer> to int

Naar
Aug 19, 2003

The Time of the Eye is now
Fun Shoe
Why not read the Javadoc and what the compiler is telling you and have a think about it? Outsourcing your homework to other people doesn't seem to be really teaching you how to do stuff.

samcarsten
Sep 13, 2022

by vyelkin
ok, so next loop is

code:
for (String k : wordFrequency.keySet()) {
					if (maxCnt == wordFrequency.get(k)) {
						output += " " + k;
					}
				}
So, I need to print all words that appear most frequently. My previous stream defined maxCnt, so I can use that variable inside my stream, right? So, my current stream looks like this:

output = wordFrequency.keySet().stream().forEach();

I use for each to check if the variable is equal to maxcnt, then if it is, i print it. That's correct, right?

samcarsten
Sep 13, 2022

by vyelkin
I think I've nearly got it now.

output = wordFrequency.entrySet().stream().filter(k -> k.getKey(maxCnt)).collect(joining(" "));

samcarsten
Sep 13, 2022

by vyelkin
ok, so getKey doesn't take a value and get that key. gently caress.

samcarsten
Sep 13, 2022

by vyelkin
i am stumped on how to filter this. everything i;ve googled doesn't work.

Eezee
Apr 3, 2011

My double chin turned out to be a huge cyst
You did the exact thing you want to do a couple of posts earlier.

samcarsten
Sep 13, 2022

by vyelkin

Eezee posted:

You did the exact thing you want to do a couple of posts earlier.

you mean

output = wordFrequency.keySet().stream().forEach();

?

samcarsten
Sep 13, 2022

by vyelkin
no, you mean when if found maxCnt. well, I defined that, so how do i use it to draw the right values?

samcarsten
Sep 13, 2022

by vyelkin
slowly getting there.

output = wordFrequency.entrySet().stream().filter(k -> maxCnt.equals(k.getValue())
.map(Map.Entry::getKey)).collect(joining(" "));

Getting two errors:

Cannot invoke equals(Integer) on the primitive type int

The method joining(String) is undefined for the type new EventHandler<ActionEvent>(){}

samcarsten
Sep 13, 2022

by vyelkin
ok, fixed one error

output = wordFrequency.entrySet().stream().filter(k -> maxCnt == (k.getValue())
.map(Map.Entry::getKey)).collect(joining(" "));

getting a new one

The method map(Map.Entry::getKey) is undefined for the type Integer

Eezee
Apr 3, 2011

My double chin turned out to be a huge cyst
Remove the parenthesis before k.getValue()

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
I really dislike teaching newbies the streaming APIs, what was wrong with doing it the old fashioned way with for loops. I feel like stuff was so much simpler to reason about.

hooah
Feb 6, 2006
WTF?

Janitor Prime posted:

I really dislike teaching newbies the streaming APIs, what was wrong with doing it the old fashioned way with for loops. I feel like stuff was so much simpler to reason about.

That's one of the many things I like about IntelliJ: given a stream pipeline, it can convert it into a loop (I don't think it can do it the other way, at least not for anything moderately complex). That lets you easily reason about what's going on, then maybe you can rebuild the pipeline once you wrap your head around the internal logic.

Pedestrian Xing
Jul 19, 2007

Janitor Prime posted:

I really dislike teaching newbies the streaming APIs, what was wrong with doing it the old fashioned way with for loops. I feel like stuff was so much simpler to reason about.

They definitely lead to some antipatterns at times. I see stream-filter-findFirst to get an entity by ID (inside a loop) frequently instead of building a Map<ID, entity> once.

ada shatan
Oct 20, 2004

that'll do pig, that'll do
So samcarsten - how did the homework go? Did you figure it out?

samcarsten
Sep 13, 2022

by vyelkin
I ended up getting more help from other people in the class. I turned everything in, did my final, and I made an 87 in the course.

ada shatan
Oct 20, 2004

that'll do pig, that'll do
That's great, congratulations!

samcarsten
Sep 13, 2022

by vyelkin

ada shatan posted:

That's great, congratulations!

i got a 100 on most stuff, i just test really badly. I got a 58 on the midterm and a 50 on the final.

Twerk from Home
Jan 17, 2009

This avatar brought to you by the 'save our dead gay forums' foundation.

Janitor Prime posted:

I really dislike teaching newbies the streaming APIs, what was wrong with doing it the old fashioned way with for loops. I feel like stuff was so much simpler to reason about.

I realize you were just having a minor gripe, but the streams interface is so much nicer for simple parallelization than alternatives like OpenMP #pragma omp parallel for or something in C++-land. Also, the streaming interfaces are much easier to turn into reactive non-blocking concurrent code as well if your workload works better that way.

Adbot
ADBOT LOVES YOU

Pulchritudinous
May 19, 2005
It means "to reduce by one-tenth."



Got directed here from the macOS/Mac software thread:

I got a new MBP and wanted to get back into tinkering with code (took some classes in school, but don't use any programming for work). I was wondering what the suggested IDEs are now. In class, we used Eclipse and I liked that that well enough (coming from no experience), but I wanted to see if there was something that was better suited to new hardware (rMBP vs. M1).

Edit: good lord, I butchered that

Pulchritudinous fucked around with this message at 03:05 on Dec 29, 2022

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply