|
ada shatan posted:The relevant file is here: https://controlc.com/1d390e17 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.
|
# ? Nov 22, 2022 21:57 |
|
|
# ? Jun 8, 2024 08:45 |
|
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. 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.
|
# ? Nov 22, 2022 22:04 |
|
okay, next loop. code:
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 |
# ? Nov 27, 2022 20:40 |
|
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?
|
# ? Nov 27, 2022 20:42 |
|
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?
|
# ? Nov 27, 2022 20:50 |
|
You can't stream a Map on its own, but you can stream its keys, values, or entries, depending on what information you need.
|
# ? Nov 27, 2022 21:07 |
|
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.
|
# ? Nov 27, 2022 21:12 |
|
You should look at some of the other stream methods and think about whether they might be helpful to solve your problem.
|
# ? Nov 28, 2022 00:14 |
|
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.
|
# ? Nov 28, 2022 01:01 |
|
Ugh, I'm just not understanding how to do this one.
|
# ? Nov 28, 2022 18:15 |
|
What's in the wordFrequency map and what are you trying to find?
|
# ? Nov 28, 2022 18:34 |
|
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?
|
# ? Nov 28, 2022 18:48 |
|
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.
|
# ? Nov 28, 2022 19:02 |
|
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>
|
# ? Nov 28, 2022 19:10 |
|
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?
|
# ? Nov 28, 2022 20:16 |
|
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?
|
# ? Nov 28, 2022 20:25 |
|
The only thing in that answer that will help you is the part right before stream()
|
# ? Nov 28, 2022 20:28 |
|
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.
|
# ? Nov 28, 2022 20:35 |
|
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.
|
# ? Nov 28, 2022 21:11 |
|
Ok, got an error on that. maxCnt = wordFrequency.values().stream().max(Comparator.naturalOrder()); Type mismatch: cannot convert from Optional<Integer> to int
|
# ? Nov 28, 2022 21:43 |
|
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.
|
# ? Nov 28, 2022 22:43 |
|
ok, so next loop iscode:
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?
|
# ? Nov 28, 2022 22:54 |
|
I think I've nearly got it now. output = wordFrequency.entrySet().stream().filter(k -> k.getKey(maxCnt)).collect(joining(" "));
|
# ? Nov 28, 2022 23:03 |
|
ok, so getKey doesn't take a value and get that key. gently caress.
|
# ? Nov 28, 2022 23:18 |
|
i am stumped on how to filter this. everything i;ve googled doesn't work.
|
# ? Nov 28, 2022 23:24 |
|
You did the exact thing you want to do a couple of posts earlier.
|
# ? Nov 28, 2022 23:34 |
|
Eezee posted:You did the exact thing you want to do a couple of posts earlier. you mean output = wordFrequency.keySet().stream().forEach(); ?
|
# ? Nov 28, 2022 23:37 |
|
no, you mean when if found maxCnt. well, I defined that, so how do i use it to draw the right values?
|
# ? Nov 28, 2022 23:40 |
|
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>(){}
|
# ? Nov 28, 2022 23:43 |
|
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
|
# ? Nov 29, 2022 18:24 |
|
Remove the parenthesis before k.getValue()
|
# ? Nov 29, 2022 22:50 |
|
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.
|
# ? Nov 30, 2022 02:36 |
|
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.
|
# ? Nov 30, 2022 02:39 |
|
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.
|
# ? Nov 30, 2022 02:56 |
|
So samcarsten - how did the homework go? Did you figure it out?
|
# ? Dec 5, 2022 19:20 |
|
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.
|
# ? Dec 6, 2022 07:44 |
|
That's great, congratulations!
|
# ? Dec 6, 2022 15:27 |
|
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.
|
# ? Dec 6, 2022 15:39 |
|
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.
|
# ? Dec 29, 2022 02:35 |
|
|
# ? Jun 8, 2024 08:45 |
|
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 |
# ? Dec 29, 2022 02:48 |