I want to compute the cartesian product of an arbitrary number of nonempty sets in Java. I’ve wrote that iterative code… public static List<Set> cartesianProduct(List<…
Tag: java
Generate RSA key pair and encode public as string
I want to generate 512 bit RSA keypair and then encode my public key as a string. How can I achieve this? Answer For output as Hex-String For output as byte values
Java: Literal percent sign in printf statement
I’m trying to add an actual percent sign into a printf statement in Java and I’m getting the error: I can’t figure out how to put an actual percent sign into my printf? I thought using % to escape it would work, but it isn’t. Any ideas? Answer The percent sign is escaped using a percen…
Simple way to get wrapper class type in Java
I have a piece of code where I need to pass the class of a field in a method. Because of the mechanics of my code I can only handle reference objects and not primitives. I want an easy way of determining if a Field’s type is primitive and swap it with the appropriate wrapper class. So in code what
Java Array Sort descending?
Is there any EASY way to sort an array in descending order like how they have a sort in ascending order in the Arrays class? Or do I have to stop being lazy and do this myself :[ Answer You could use this to sort all kind of Objects Arrays.sort() cannot be used directly to sort primitive arrays in descending
Converting JSON data to Java object
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 …