For my work I have to develop a small Java application that parses very large XML files (~300k lines) to select very specific data (using Pattern), so I’m trying to optimize it a little. I was wondering what was better between these 2 snippets: OR Other details: These if statements are executed on each …
Tag: java
Hibernate : How override an attribute from mapped super class
The generic entity, super class: The pojo: I try to use thoses annotations : @AttributeOverride, @Id, … but It doesn’t work. Can you help me? I want to override the attribute “id” to specify another column name and a sequence by pojo/table. What is the best way to do that? Answer Try t…
Adding whitespace in Java
There is a class trim() to remove white spaces, how about adding/padding? Note: ” ” is not the solution. Answer I think you are talking about padding strings with spaces. One way to do this is with string format codes. For example, if you want to pad a string to a certain length with spaces, use s…
Set running time limit on a method in java
I have a method that returns a String. Is it possible that after a certain time, if threshold is excedeed for that method, to return some specific string? Answer The Guava library has a very nice TimeLimiter that lets you do this on any method that’s defined by an interface. It can generate a proxy for …
Get page content from Apache Commons HTTP Request
So I’m using Apache Commons HTTP to make a request to a webpage. I cannot for the life of me figure out how to get the actual content from the page, I can just get its header information. How can I get the actual content from it? Here is my example code: Answer Use HttpResponse#getEntity() and then Http…
Force point (“.”) as decimal separator in java
I currently use the following code to print a double: return String.format(“%.2f”, someDouble); This works well, except that Java uses my Locale’s decimal separator (a comma) while I would like to …
How to do method overloading for null argument?
I have added three methods with parameters: When I am calling doSomething(null) , then compiler throws error as ambiguous methods. So is the issue because Integer and char[] methods or Integer and Object methods? Answer Java will always try to use the most specific applicable version of a method that’s …
How can I scan a maven repository?
I’m developing a code-sharing plugin for eclipse (for a bachelor thesis project). Currently I’m trying to scan a maven repository and generate a package list. I can download and parse a pom.xml using the maven.model classes, but I can’t figure out which maven classes are responsible for pars…
javax.xml.bind.UnmarshalException: unexpected element (uri:””, local:”Group”)
Meet an exception when unmarshalling from xml Group class has no any annotation and group.xml just contains data. Anything can be the cause? Answer It looks like your XML document has the root element “Group” instead of “group”. You can: Change the root element on your XML to be “…
StackOverflowError when running my “Knight’s Tour” (it’s pretty much finished otherwise)
I’m trying to make a program that goes through all squares of a chessboard (size doesn’t really matter, but for now it’s 6×6) with a knight, called a “Knight’s Tour” check it out on wiki. The tour is supposed to be closed, which means the knight on the last visited squ…