|
Jarl posted:Anyone knows why it isn't possible to iterate over entries in a map in java without making and populating a set first (which is what getEntries does)? - Btw this is supported in the C++ standard library. Short answer: Because Map doesn't implement Iterable. Long answer: A map is, by definition, a mapping of keys to values. Iterable is designed for iterating over a orderable list of values of uniform type. In the context of a map the collections API doesn't make assumptions about if you want to iterate over the keys (.keySet()), values (.values()), or relationships (.entrySet()). I haven't looked, but I'm assuming that under the covers those methods return a shallow copy of the Map internals at worst, so it's not allocating a whole new copy of your data structure. This is also why ConcurrentModificationException is a thing.
|
# ? Oct 23, 2013 15:22 |
|
|
# ? May 8, 2024 19:34 |
|
mrmcd posted:Short answer: Because Map doesn't implement Iterable. Yes, it is shallow copy under the hood, but that doesn't change that it shouldn't be necessary (if it was only an option it was another matter). In C++ STL you can iterate over keys, values and entries too, so that's not the explanation. It's not important but I'm genuinely curious why this was decided.
|
# ? Oct 23, 2013 15:56 |
|
Jarl posted:Yes, it is shallow copy under the hood, but that doesn't change that it shouldn't be necessary (if it was only an option it was another matter). In C++ STL you can iterate over keys, values and entries too, so that's not the explanation. It's not important but I'm genuinely curious why this was decided. I don't know, honestly I think iterating through a key set is more intuitive than iterating through the map and calling first() or second() or whatever on each element to get the value you want. Java came later and worries a little more about protecting you from yourself than C++ does. C++ lets you overload operators, java says "hey pal just make em methods". Not a huge difference. You lose a little syntactic sugar but you gain readability.
|
# ? Oct 23, 2013 16:28 |
|
Jarl posted:Yes, it is shallow copy under the hood, but that doesn't change that it shouldn't be necessary (if it was only an option it was another matter). In C++ STL you can iterate over keys, values and entries too, so that's not the explanation. It's not important but I'm genuinely curious why this was decided. It's not even a shallow copy, it's a lazily-initialized facade (or view if you will) into the map. Expecting Java, of all things, to try to inline things at compile time or paint over complexity with overloaded syntax (ref: mrmcd's explanation about iterators and sequential access) is kind of missing the point of, you know, Java. I mean, jeez, the STL is the Standard Template Library. It is literally the standard library of undecidableness that waits until compile time to usually expand to logic that does what you expect. Just because the STL does something doesn't mean Java should do it too, or that the STL isn't masking essentially the same kind of thing. And the objects Java creates for the iteration are so lightweight I'm sure Java does a pretty decent job of JITting it down to something small and reasonable at runtime. Map/HashMap are only some of the most used interfaces/implementations in the millions of Java programs out there. Doctor w-rw-rw- fucked around with this message at 16:52 on Oct 23, 2013 |
# ? Oct 23, 2013 16:44 |
|
Jarl posted:Yes, it is shallow copy under the hood, but that doesn't change that it shouldn't be necessary (if it was only an option it was another matter). In C++ STL you can iterate over keys, values and entries too, so that's not the explanation. It's not important but I'm genuinely curious why this was decided. They could have made Map<K,V> implement Iterable<Entry<K,V>>. I can't think of any downside.
|
# ? Oct 23, 2013 17:07 |
|
If we want to talk about Java API design decisions we don't like, let's discuss why NumberFormatException is an unchecked exception.
|
# ? Oct 23, 2013 17:21 |
|
Sedro posted:The sets are views over the map. This is specified by the map interface so an implementation using shallow copies would be broken. The fact that it would likely be implemented the same way anyways, only now iterable over a type it didn't directly contain doesn't strike you as bad API design? What if they wanted to change the implementation? What if you wanted to iterate twice over the same entry set and error if there was a concurrent modification in between iterations? Inlining the iteration syntax would recreate rather than reuse the entry set.
|
# ? Oct 23, 2013 17:22 |
|
It seems like an improvement to me. Look at any other language API; the map/dictionary/associative array will be iterable as key-value tuples. Are you mixing up Iterable and Iterator? You can have two iterators going at the same time. There is only one entry set, so it would be functionally equivalent if it wasn't there. Just move its implementation into the map itself since they have the same lifetime.
|
# ? Oct 23, 2013 19:01 |
|
Doctor w-rw-rw- posted:It's not even a shallow copy, it's a lazily-initialized facade (or view if you will) into the map. From reading the documentation here http://developer.android.com/reference/java/util/HashMap.html#keySet%28%29 I did not get the impression it was created lazily, as in it starts out empty and fills up as you iterate over it. Cool. But yeah, what I really meant was that all references were copied, which they are (right?), albeit lazily.
|
# ? Oct 23, 2013 19:05 |
|
Jarl posted:From reading the documentation here http://developer.android.com/reference/java/util/HashMap.html#keySet%28%29 I did not get the impression it was created lazily, as in it starts out empty and fills up as you iterate over it. Cool. No. Remember that a Set isn't an object, it's an interface, and the internal implementation for iterating over HashMap just crawls over the HashMap's own internal table. The facade itself isn't even created until it's used. To spell it out: - Zero memory impact when not used. - Small and constant-size overhead when used. - Reuses backing storage for iteration.
|
# ? Oct 23, 2013 19:27 |
|
Doctor w-rw-rw- posted:No. Remember that a Set isn't an object, it's an interface, and the internal implementation for iterating over HashMap just crawls over the HashMap's own internal table. The facade itself isn't even created until it's used. Thanks.
|
# ? Oct 23, 2013 20:41 |
|
Anyone got any clue why trying to bind to a port on a remote server (using a socket, obviously) would succeed when run as a standard java application, but fails when run under tomcat? I've taken a look at network traffic both through my firewall and via wireshark, and I can see TCP requests heading out on the correct port and to the correct destination address, but all I end up with is a ConnectException under tomcat. This would be fine if it didn't work when not run under tomcat, and that I know the server is reachable under that port. If it helps, I'm using OpenSMPP (here), and you can see how it's doing its binding here.
|
# ? Oct 25, 2013 14:56 |
|
Crumpet posted:Anyone got any clue why trying to bind to a port on a remote server (using a socket, obviously) would succeed when run as a standard java application, but fails when run under tomcat? What do tomcat logs say? Are they receiving any requests? Is it actually running the application on tomcat? Maybe a permissions issue, Tomcat might not even be running the program. Or it could be refusing the requests. Check your tomcat configuration as well. Everything in /conf/, like server.xml and web.xml. I think you need to define a connector to the port you're using.
|
# ? Oct 25, 2013 15:25 |
|
Zaphod42 posted:What do tomcat logs say? Are they receiving any requests? Is it actually running the application on tomcat? Nothing in the tomcat logs other than the exceptions relating to this. Permissions are fine - I have full admin privileges on this machine, and I'm running tomcat from an elevated command prompt. I've got a connector to the correct port in server.xml.
|
# ? Oct 25, 2013 16:20 |
|
So I need to read in a text file, which has lines in format " name; category; cost; date;" take like categories and add the costs up and out put them as totaled. I assume the best way to do this would be to take in each line as an array, compare the arrays at index 1 to a list of known categories and if they're alike add them up and store it as a float. Any help on how to do that or a better solution than what I got. Java code:
Defenestrategy fucked around with this message at 20:25 on Oct 25, 2013 |
# ? Oct 25, 2013 20:23 |
|
KildarX posted:So I need to read in a text file, which has lines in format " name; category; cost; date;" take like categories and add the costs up and out put them as totaled. One way to handle this is to split each input line on ";", use the second and third columns to populate a map of categories to sums, then print the result. Here's an example: code:
code:
|
# ? Oct 25, 2013 21:16 |
|
KildarX posted:So I need to read in a text file, which has lines in format " name; category; cost; date;" take like categories and add the costs up and out put them as totaled. Personally, I'd read in each line as a String, and use String.split() to split the input on the semicolon. I'd then use a Map<String, Float> to associate each category with it's sum of costs. @MrPablo: Since this is titled "lab 7", you should give hints, but probably avoid flat-out doing this guy's homework for him.
|
# ? Oct 25, 2013 21:24 |
|
Gravity Pike posted:@MrPablo: Since this is titled "lab 7", you should give hints, but probably avoid flat-out doing this guy's homework for him. Yeah, that occurred to me a few minutes ago as well.
|
# ? Oct 25, 2013 21:47 |
|
Gravity Pike posted:Personally, I'd read in each line as a String, and use String.split() to split the input on the semicolon. I'd then use a Map<String, Float> to associate each category with it's sum of costs. I guess I'm over thinking it again, thought I was gonna be clever using arrays and crap. Thanks for the help guys, back to drawing board.
|
# ? Oct 25, 2013 22:19 |
|
Crumpet posted:Nothing in the tomcat logs other than the exceptions relating to this. Permissions are fine - I have full admin privileges on this machine, and I'm running tomcat from an elevated command prompt. Oh, I see, Tomcat is trying to connect to the remote server, I figured that you were running the server on tomcat, rather than as an application. Hmm, so Tomcat is the client. What about the server then, do you have access to its logs?
|
# ? Oct 25, 2013 22:52 |
|
KildarX posted:I guess I'm over thinking it again, thought I was gonna be clever using arrays and crap. So other people might fervently disagree with me on this statement, but, as a professional software developer: Never be clever. There are many things that you can optimize for in your code: speed, memory usage, source lines, compile size. The most important is maintainability. If you think you're being clever, the person sitting next to you is going to read the code and think that it's confusing. Clever code causes bugs. Clever code eats up dev-hours as people try to parse through your logic. Clever code usually fails to foresee the product requirement that is going to be thrown at the codebase a few months down the line. Clever code takes somewhere between two and ten times as long to update as boring, idiomatic code. By all means, be intelligent. Write smart code. But remember that your code is going to be read by other devs far more frequently that the one time that you write it. It not only has to produce the correct output, it has to, at a glance, look like it's going to produce the correct output.
|
# ? Oct 25, 2013 23:08 |
|
Zaphod42 posted:Oh, I see, Tomcat is trying to connect to the remote server, I figured that you were running the server on tomcat, rather than as an application. Sadly, I do not - the server in question is a staging server which is part of a department that's only related to mine by name. I suppose I could submit a request for logs, but they'd take about 2 weeks to materialise. Thankfully this is only a POC, so I'm in no particular rush to get this sorted - and I always have the option of just not using tomcat.
|
# ? Oct 25, 2013 23:30 |
|
Gravity Pike posted:So other people might fervently disagree with me on this statement, but, as a professional software developer: Never be clever. To add to this, a very wise man once said: quote:Write code as if the person who's going to maintain it is a violent psychopath who knows where you live. Code quality measurement comic: http://www.techtoolblog.com/archives/wtfminute-code-reviews
|
# ? Oct 26, 2013 00:16 |
|
Crumpet posted:Sadly, I do not - the server in question is a staging server which is part of a department that's only related to mine by name. I suppose I could submit a request for logs, but they'd take about 2 weeks to materialise. Thankfully this is only a POC, so I'm in no particular rush to get this sorted - and I always have the option of just not using tomcat. There should (in theory) be no reason why you are able to connect to a remote server when running as a standalone application but not when running as a web application in an application server. I see that you have ensured that the user tomcat runs under is a privileged one, but just double/triple check. Who knows? Is it under Windows? Maybe the java process that runs tomcat is different from the java process that runs your standalone application (I have a ton of jdk's installed and it's easy to choose a "wrong" one) and Windows firewall may be blocking that one but not yours? That's all i can think of right now, but this should not happen. At all. It doesn't really make any sense. As a last resort, make the simplest, dumbest web app, that only has one index.jsp that makes a connection to that server and see if it works or not.
|
# ? Oct 26, 2013 00:24 |
|
A lot of times, things like Tomcat run with elevated privileges, then drop them when they're done with certain things (e.g., binding ports below 1024 on Linux).
|
# ? Oct 26, 2013 06:54 |
|
Okay, so I've been trying to wrap my head around inheritance (and honestly, OOP in general since it sounds great in principle but the implementation of it can be confusing to me) and I'm having a little bit of a stumble here with my test class. To give you guys some context, here's the assignment: code:
Java code:
Java code:
quote:cannot find symbol A few questions about this giant wall o' code: 1) Do I make class Quadrilateral extends Object or am I misunderstanding something? 2) if I make class Square extend Rectangle then use super( x1, y1, ... , x4, y4 ) will that be inheriting those variables from class Quadrilateral or from class Rectangle? I'm assuming it'd be from Rectangle and ultimately Quadrilateral but I just wanted to double check. 3) How do I go about fixing the error in the test class? I explicitly defined the constructors in their classes (or at least as far as I understand, I did ), so if they're not inherited from their original classes, do I have to redefine them in class QuadrilateralTest Thanks for reading through this massive post, this is one of the first things I've come across in Java that's legitimately stumped me. Otherwise, I've really enjoyed it, especially compared to C/C++. FAT32 SHAMER fucked around with this message at 22:48 on Oct 27, 2013 |
# ? Oct 27, 2013 21:44 |
|
Tusen Takk posted:1) Do I make class Quadrilateral extends Object or am I misunderstanding something? All classes implicitly extend Object if no extends is specified, meaning all classes are a subclass of object, even if they may be a distant descendant. Tusen Takk posted:2) if I make class Square extend Rectangle then use super( x1, y1, ... , x4, y4 ) will that be inheriting those variables from class Quadrilateral or from class Rectangle? I'm assuming it'd be from Rectangle and ultimately Quadrilateral but I just wanted to double check. Instance variables with the same name are overridden by subclasses, which means that for any variable that exists in several classes that are in the same inheritance hierarchy, the lowest one is used. The same goes for methods. In this case, because Rectangle has a constructor accepting the same number of arguments of the same type in the same order, you will use Rectangle's constructor. However, if Rectangle's constructor did not take exactly 8 doubles, but you called the 8 double super(), it would use Quadrilateral's constructor directly. Tusen Takk posted:3) How do I go about fixing the error in the test class? I explicitly defined the constructors in their classes (or at least as far as I understand, I did ), so if they're not inherited from their original classes, do I have to redefine them in class QuadrilateralTest Quadrilateral and all subclasses have a constructor taking 8 double variables. You are trying to call it and supply 4 locationPoint variables. Java has no way of knowing how to convert the two, so you should specify the points as doubles and let the constructor make the locationPoints objects. Your other option is to create another constructor which takes four locationPoints variables and just store them in the instance variables; this is called overloading. carry on then fucked around with this message at 22:03 on Oct 27, 2013 |
# ? Oct 27, 2013 22:00 |
|
carry on then posted:All classes implicitly extend Object if no extends is specified, meaning all classes are a subclass of object, even if they may be a distant descendant. quote:Instance variables with the same name are overridden by subclasses, which means that for any variable that exists in several classes that are in the same inheritance hierarchy, the lowest one is used. The same goes for methods. In this case, because Rectangle has a constructor accepting the same number of arguments of the same type in the same order, you will use Rectangle's constructor. However, if Rectangle's constructor did not take exactly 8 doubles, but you called the 8 double super(), it would use Quadrilateral's constructor directly. quote:Quadrilateral and all subclasses have a constructor taking 8 double variables. You are trying to call it and supply 4 locationPoint variables. Java has no way of knowing how to convert the two, so you should specify the points as doubles and let the constructor make the locationPoints objects. Your other option is to create another constructor which takes four locationPoints variables and just store them in the instance variables; this is called overloading. Java code:
Java code:
I'm not really sure how I would go about creating a constructor to take the four locationPoints vars and store them in the instance vars though . edit: actually, wouldn't it be Java code:
FAT32 SHAMER fucked around with this message at 22:47 on Oct 27, 2013 |
# ? Oct 27, 2013 22:28 |
|
This is the signature of your Quadrilateral constructor:Java code:
As far as taking the four locationPoints vars instead of 8 doubles, look at the types of your instance variables and the types of the proposed arguments, and think about what (if anything) you need to do with the parameters to make the constructor have the same functionality as the one you already have.
|
# ? Oct 27, 2013 22:35 |
|
carry on then posted:This is the signature of your Quadrilateral constructor: OHHH okay so I have to either change the signature of Quadrilateral to something like Java code:
Java code:
edit: I got it working, now to just fix my logic errors and formatting! output posted:The Coordinates of this quadrilateral are: FAT32 SHAMER fucked around with this message at 23:01 on Oct 27, 2013 |
# ? Oct 27, 2013 22:45 |
|
Tusen Takk posted:OHHH okay so I have to either change the signature of Quadrilateral to something like More like Java code:
|
# ? Oct 27, 2013 23:06 |
|
You are kind of missing out on the major positive aspects of inheritance in your sample classes: abstract methods. First off, inheritance means that you can stick a more-specific object in a less-specific variable: Java code:
Java code:
Put these two concepts together, and you can do things like: Java code:
|
# ? Oct 28, 2013 10:14 |
|
Gravity Pike posted:You are kind of missing out on the major positive aspects of inheritance in your sample classes: abstract methods. That's pretty awesome, actually. I'm gonna have to go over my design and see if I can implement this and simplify what I have . Thanks!!
|
# ? Oct 28, 2013 16:49 |
|
Kilson posted:A lot of times, things like Tomcat run with elevated privileges, then drop them when they're done with certain things (e.g., binding ports below 1024 on Linux). This was exactly it, thanks.
|
# ? Oct 28, 2013 17:43 |
|
Crumpet posted:This was exactly it, thanks. I knew it was permissions! I've had so many cases where Tomcat doesn't start up properly when it should "just work", ugh.
|
# ? Oct 28, 2013 18:15 |
|
Zaphod42 posted:I knew it was permissions! Yeah, it didn't surprise me that it was permissions - there's really no other reason it shouldn't have worked fine. And yes, most of my early dev time when creating some form of POC is struggling with tomcat, but this was a new one to my colleagues and I (admittedly, maven can sometimes be a hindrance rather than a help when battling tomcat, too).
|
# ? Oct 28, 2013 18:33 |
|
I have a multiproject set up in Gradle. I'd like to be able to set a 'buildType' external property per subproject, so I can do some generic setup in the main build.gradle based on this 'buildType'. So for example, if a subproject has 'buildType' set to 'java', I would apply the java plugin, set up some default dependencies. If a subproject has 'buildType' set to 'javascript', I'd set up npm tasks, whatever. Anyway, I'm having trouble setting this up. For some reason Gradle doesn't allow me to filter subprojects based on buildType, but that's probably because I'm doing something wrong build.gradle: code:
code:
code:
code:
code:
|
# ? Oct 30, 2013 10:14 |
|
We've recently started having trouble with some Java based projects at work (Jive, if anyone is familiar) that have started hitting their heap limit of 4GB. We've increased to 6GB and it seems to have stabilized but we have a lot of concerns that something is deeply hosed up with the code. Are heap dumps with tools like jmap very useful from our operations non-developer perspective? Are heap/stack dumps possible without completely pegging the system in a production environment?
|
# ? Nov 1, 2013 01:08 |
|
ahmeni posted:We've recently started having trouble with some Java based projects at work (Jive, if anyone is familiar) that have started hitting their heap limit of 4GB. We've increased to 6GB and it seems to have stabilized but we have a lot of concerns that something is deeply hosed up with the code. Heap dumps won't be too useful to your ops people, there's rarely anything they can adjust if it's a problem with the application. However it will be useful for devs to figure out where the problem is and have a good starting place to fix it. There is a JVM option to save a heap dump in case an OutOfMemoryError occurs pre:-XX:+HeapDumpOnOutOfMemoryError and -XX:HeapDumpPath=/disk2/dumps
|
# ? Nov 1, 2013 05:40 |
|
|
# ? May 8, 2024 19:34 |
|
Gravity Pike posted:So other people might fervently disagree with me on this statement, but, as a professional software developer: Never be clever. "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan
|
# ? Nov 1, 2013 05:58 |