How to directly initialize a HashMap (in a literal way)?
2021-6-3 anglehua
Is there some way of initializing a Java HashMap like this?: Map<String,String> test = new HashMap<String, String>{"test":"test","test":"test"}; What would be the correct syntax? I have not found anything regarding this. Is this possible? I am looking for the shortest/fastest way t...'Must Override a Superclass Method' Errors after importing a project into Eclipse
2021-6-3 anglehua
Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or changed the location of the projects), almost all of my overridden methods are not formatted correctly, causing the error: The method must override a superclass method It may be noteworthy to mention this is with An...Difference between wait() and sleep()
2021-6-3 anglehua
What is the difference between a wait() and sleep() in Threads? Is my understanding that a wait()-ing Thread is still in running mode and uses CPU cycles but a sleep()-ing does not consume any CPU cycles correct? Why do we have both wait() and sleep(): how does their implementation vary at a lower l...Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
2021-6-3 anglehua
We all know you can't do the following because of ConcurrentModificationException: for (Object i : l) { if (condition(i)) { l.remove(i); } } But this apparently works sometimes, but not always. Here's some specific code: public static void main(String[] args) { Collection<Int...Android SDK installation doesn't find JDK
2021-6-3 anglehua
I'm trying to install the Android SDK on my Windows 7 x64 System. jdk-6u23-windows-x64.exe is installed, but the Android SDK setup refuses to proceed because it doesn't find the JDK installation. Is this a known issue? And is there a solution? Press Back when you get the notification and then Ne...Sort ArrayList of custom Objects by property
2021-6-3 anglehua
I read about sorting ArrayLists using a Comparator but in all of the examples people used compareTo which according to some research is a method for Strings. I wanted to sort an ArrayList of custom objects by one of their properties: a Date object (getStartDay()). Normally I compare them by item1.ge...How do servlets work? Instantiation, sessions, shared variables and multithreading
2021-6-3 anglehua
Suppose, I have a webserver which holds numerous servlets. For information passing among those servlets I am setting session and instance variables. Now, if 2 or more users send request to this server then what happens to the session variables? Will they all be common for all the users or they will ...What are the -Xms and -Xmx parameters when starting JVM?
2021-6-3 anglehua
Please explain the use of Xms and Xmx parameters in JVMs. What are the default values for them? The flag Xmx specifies the maximum memory allocation pool for a Java Virtual Machine (JVM), while Xms specifies the initial memory allocation pool. This means that your JVM will be started with Xms amoun...How can I initialise a static Map?
2021-6-3 anglehua
How would you initialise a static Map in Java? Method one: static initialiser Method two: instance initialiser (anonymous subclass) or some other method? What are the pros and cons of each? Here is an example illustrating the two methods: import java.util.HashMap; import java.util.Map; public clas...How to parse JSON in Java
2021-6-3 anglehua
I have the following JSON text. How can I parse it to get the values of pageName, pagePic, post_id, etc.? { "pageInfo": { "pageName": "abc", "pagePic": "http://example.com/content.jpg" }, "posts": [ { "post_id": "123...How to get the current working directory in Java?
2021-6-3 anglehua
I want to access my current working directory using Java. My code: String currentPath = new java.io.File(".").getCanonicalPath(); System.out.println("Current dir:" + currentPath); String currentDir = System.getProperty("user.dir"); System.out.println("Current dir using System:" + currentDir); ...How to create a generic array in Java?
2021-6-3 anglehua
Due to the implementation of Java generics, you can't have code like this: public class GenSet<E> { private E a[]; public GenSet() { a = new E[INITIAL_ARRAY_LENGTH]; // error: generic array creation } } How can I implement this while maintaining type safety? I saw a solu...What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do
2021-6-3 anglehua
I really want to know more about the update, export and the values that could be given to hibernate.hbm2ddl.auto I need to know when to use the update and when not? And what is the alternative? These are changes that could happen over DB: new tables new columns in old tables columns deleted data ty...How to install Java 8 on Mac
2021-6-3 anglehua
Editors note: This question was asked in 2014, and the answers may be outdated. I want to do some programming with the latest JavaFX, which requires Java 8. I'm using IntelliJ 13 CE and Mac OS X 9 Mavericks. I ran Oracle's Java 8 installer, and the files look like they ended up at /Library/Java/Ja...Converting 'ArrayList<String> to 'String[]' in Java
2021-6-3 anglehua
How might I convert an ArrayList<String> object to a String[] array in Java? List<String> list = ..; String[] array = list.toArray(new String[0]); For example: List<String> list = new ArrayList<String>(); //add some stuff list.add("android"); list.add("apple"); String[] str...Can't execute jar- file: “no main manifest attribute”
2021-6-3 anglehua
I have installed an application, when I try to run it (it's an executable jar) nothing happens. When I run it from the commandline with: java -jar "app.jar" I get the following message: no main manifest attribute, in "app.jar" Normally, if I had created the program myself, I would have added a ...Which @NotNull Java annotation should I use?
2021-6-3 anglehua
I'm looking to make my code more readable as well as use tooling like IDE code inspection and/or static code analysis (FindBugs and Sonar) to avoid NullPointerExceptions. Many of the tools seem incompatible with each others' @NotNull/@NonNull/@Nonnull annotation and listing all of them in my code w...How can I pad an integer with zeros on the left?
2021-6-3 anglehua
How do you left pad an int with zeros when converting to a String in java? I'm basically looking to pad out integers up to 9999 with leading zeros (e.g. 1 = 0001). Use java.lang.String.format(String,Object...) like this: String.format("%05d", yournumber); for zero-padding with a length of 5. For h...Converting array to list in Java
2021-6-3 anglehua
How do I convert an array to a list in Java? I used the Arrays.asList() but the behavior (and signature) somehow changed from Java SE 1.4.2 (docs now in archive) to 8 and most snippets I found on the web use the 1.4.2 behaviour. For example: int[] spam = new int[] { 1, 2, 3 }; Arrays.asList(spam) ...Download a file with Android, and showing the progress in a ProgressDialog
2021-6-3 anglehua
I am trying to write a simple application that gets updated. For this I need a simple function that can download a file and show the current progress in a ProgressDialog. I know how to do the ProgressDialog, but I'm not sure how to display the current progress and how to download the file in the fir...