Per the Java documentation, the hash code for a String object is computed as: using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. Why is 31 used as a multiplier? I understand that the multiplier should be a relatively large prime …
Inserting a node into a linked list in constant-time?
I’m working on an assignment that is telling me to assume that I have a singly linked list with a header and tail nodes. It wants me to insert an item y before position p. Can anybody please look over my code and tell me if I’m on the right track? If not, can you provide me with any tips
Output RFC 3339 Timestamp in Java
I want to output a timestamp with a PST offset (e.g., 2008-11-13T13:23:30-08:00). java.util.SimpleDateFormat does not seem to output timezone offsets in the hour:minute format, it excludes the colon. Is there a simple way to get that timestamp in Java? Also, SimpleDateFormat cannot properly parse the example …
What is meant by immutable?
What exactly does immutable mean – that is, what are the consequences of an object being mutable or immutable? In particular, why are Java’s Strings immutable? My understanding is that the StringBuilder type is something like a mutable equivalent to String. When would I use StringBuilder rather th…
How do I count the number of occurrences of a char in a String?
I have the string I want to count the occurrences of ‘.’ in an idiomatic way, preferably a one-liner. (Previously I had expressed this constraint as “without a loop”, in case you’re wondering why everyone’s trying to answer without using a loop). Answer My ‘idiomatic …
Can java’s assert statement allow you to specify a message?
Seems likes it might be useful to have the assert display a message when an assertion fails. Currently an AssertionError gets thrown, can you specify a custom message for it? Can you show an example mechanism for doing this (other than creating your own exception type and throwing it)? Answer You certainly ca…
Maven compile with multiple src directories
Is there a way to compile multiple java source directories in a single maven project? Answer You can add a new source directory with build-helper:
Remove HTML tags from a String
Is there a good way to remove HTML from a Java string? A simple regex like will work, but some things like & won’t be converted correctly and non-HTML between the two angle brackets will be removed (i.e. the .*? in the regex will disappear). Answer Use a HTML parser instead of regex. This is dea…
During execution, how can a java program tell how much memory it is using?
During execution, how can a java program tell how much memory it is using? I don’t care how efficient it is! Answer VonC’s answer is an interactive solution – if you want to know programatically, you can use Runtime.totalMemory() to find out the total amount used by the JVM, and Runtime.free…
What is the best way to do GUIs in Clojure?
What is the best way to do GUIs in Clojure? Is there an example of some functional Swing or SWT wrapper? Or some integration with JavaFX declarative GUI description which could be easily wrapped to s-expressions using some macrology? Any tutorials? Answer I will humbly suggest Seesaw. Here’s a REPL-base…