I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string can look like: In this string every JSON object contains an array of other JSON objects. The intention is to …
Dumping a Java StringBuilder to File
What is the most efficient/elegant way to dump a StringBuilder to a text file? You can do: But is this efficient for a very long file? Is there a better way? Answer As pointed out by others, use a Writer, and use a BufferedWriter, but then don’t call writer.write(stringBuilder.toString()); instead just …
Java: Get first item from a collection
If I have a collection, such as Collection<String> strs, how can I get the first item out? I could just call an Iterator, take its first next(), then throw the Iterator away. Is there a less wasteful way to do it? Answer Iterables.get(yourC, indexYouWant) Because really, if you’re using Collection…
Compare Date objects with different levels of precision
I have a JUnit test that fails because the milliseconds are different. In this case I don’t care about the milliseconds. How can I change the precision of the assert to ignore milliseconds (or any precision I would like it set to)? Example of a failing assert that I would like to pass: Answer Use a Date…
How to add reference to a method parameter in javadoc?
Is there a way to add references to one or more of a method’s parameters from the method documentation body? Something like: Answer As far as I can tell after reading the docs for javadoc there is no such feature. Don’t use <code>foo</code> as recommended in other answers; you can use …
Too many open file handles
I’m working on a huge legacy Java application, with a lot of handwritten stuff, which nowadays you’d let a framework handle. The problem I’m facing right now is that we are running out of file handles on our Solaris Server. I’d like to know what’s the best way to track open file …
How to map a PostgreSQL array with Hibernate
Has anyone successfully mapped a numeric array in PostgreSQL to a numeric array in Java via Hibernate? SQL: Mapping: Class: I get an exception when querying the table. Answer Hibernate does not support database arrays (e.g. ones mapped to java.sql.Array) out of the box. array and primitive-array types provide…
How should equals and hashcode be implemented when using JPA and Hibernate
How should model class’s equals and hashcode be implemented in Hibernate? What are the common pitfalls? Is the default implementation good enough for most cases? Is there any sense to use business keys? It seems to me that it’s pretty hard to get it right to work in every situation, when lazy fetc…
Netty vs Apache MINA
They both provide roughly the same functionality. Which one should I choose to develop my high-performance TCP server? What are the pros & cons? Reference links: Apache MINA (source) Netty (source) Answer While MINA and Netty have similar ambitions, they are quite different in practice and you should cons…
Authenticated HTTP proxy with Java
How can I configure the username and password to authenticate a http proxy server using Java? I just found the following configuration parameters: But, my proxy server requires authentication. How can I configure my app to use the proxy server? Answer (EDIT: As pointed out by the OP, the using a java.net.Auth…