Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
donuts236788y@linux-colonel I wanted to flip the order. Need to define the comparator for the heap which is generic, can compare any type.
-
donuts236788y@Qchmqs yes but as you probably can tell from the project tree, I'm studying data structures... And Algorithms
-
@allanx2000
b.compareTo(a) would have the opposite order to a.compareTo(b) 😊 -
donuts236788y@linux-colonel but then I would have to rewrite the whole thing... -1* is much easier
-
@allanx2000
You wouldn't have to rewrite the whole thing, that lambda would just be (a, b) -> b.compareTo(a). I guarantee you it'd look better in an interview situation. Readability is everything! -
Out of curiosity what is the constructor doing with the value of the compareTo value? I would assume it would have something to do with minimum heap size, but since it could be -1 passed in, I'm just curious...
-
Btw one recommendation if this is code you are submitting for the interview- clean up the commented out import statements and add more javadoc and comments. Documentation and clean code is really, really important. Especially in an interview where they usually judge code style pretty closely.
-
donuts236788y@Yankeesrule
It's an implementation of Comparator, which the heap uses to sort the values -
@allanx2000 ah, got it. I read the code quickly and didn't see that it was a bi-function. Java 8 lambdas are awesome but definitely need to pay attention when reading it. My bad.
Congrats for figuring it out! It doesn't matter how long it takes. What matters is you got it. To be quite honest in a real world scenario I'm sure there's a heap library you could just import. But you taking the time to understand it is huge. So congrats! -
donuts236788y@Yankeesrule didn't know I could do this till I saw a lightbulb that said it could use a lambda
-
@allanx2000 yeah. Prior to java 8 you coulda used an anonymous inner class that implemented comparator but this is WAY cleaner. Understanding lambdas is a big achievement too since they are kind of mind exploding at first (though streams are stranger at first). So kudos. I think you are better off than you think.
-
Eariel19068y@Yankeesrule, lambdas make the code so nice to read. As an Android developer I can use the RetroLambda library which is not complete but it's nice too.
-
@Eariel I know! When I first read about them I was very dubious about how complex it would make the code but if anything it's done the opposite. They are great.
... But you definitely need to be careful not to abuse them. Like I've had to stop myself from writing a function or supplier within a function just because then it can be difficult to understand the business logic when reading it at first.
It's strange though because I really do love them but then I show the code to one co-worker who can't stand them- to her the business logic is not "linear" and so it's not readable for her. I just think to myself she is lucky she is working in Java because she would be so screwed dealing with pointers in other languages -
@allanx2000 when you are building everything up from scratch, I don't think it makes sense to use an ArrayList, use a resizing array instead.
-
donuts236788y@ravik yes but uhm thats exactly what an array list is though. Just track the size and when it hits double it
-
Out of curiosity, is this really useful? I had a class for a whole semester about "Information Structures" where we talked about LinkedList, DoublyLinkedLists, trees, heaps, graphs, etc. We did a cool little project during the semestre (the really simplified model of a social network) but it was mostly just to apply the algorithms and classes from the lectures so I didn't get the point of it.
-
@allanx2000 I have that book, I'm planning to start a "training regime" later this year since I'm going to need to apply for internships for my degree. Still, these are the kind of concepts that are interesting and seem really cool but I don't see them being actually useful. I mean, at least creating a heap logic by yourself when there are tons and tons of implementations already available. IDK.
-
donuts236788y@milkbytes yep that's what I thought too. I'm self taught, didn't study CS but all the good tech companies ask this stuff and I just have to bite the bullet...
-
@allanx2000 My field of studies is actually Software Engineering and I'm yet to understand the usefulness of doing my own implementation of heaps and etc. Maybe it's one of those things that I'm only going to understand when I need it 😂
-
@Yankeesrule if you are familiar with RX(Java, JavaScript, RX Android), doesn't matter, those emitted events are just streams, basically. You can change and manipulate data as it passed through the stream and them collect it or just pass it to a function.
Wooh! I implemented a heap.... that only took... 5 hours...
I am so screwed for my interview next week..... :(
undefined