Converting ‘ArrayList to ‘String[]’ 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[] stringArray = list.toArray(new String[0]); The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, … Read more

How do servlets work? Instantiation, sessions, shared variables and multithreading

ServletContext When the servlet container (like Apache Tomcat) starts up, it will deploy and load all its web applications. When a web application is loaded, the servlet container creates the ServletContext once and keeps it in the server’s memory. The web app’s web.xml and all of included web-fragment.xml files is parsed, and each <servlet>, <filter> … Read more

How to install Java 8 on Mac

Oracle has a poor record for making it easy to install and configure Java, but using Homebrew, the latest OpenJDK (Java 14) can be installed with: brew install –cask adoptopenjdk8 For the many use cases depending on an older version (commonly Java 8), the AdoptOpenJDK project makes it possible with an extra step. brew tap … Read more

Sort ArrayList of custom Objects by property

Since Date implements Comparable, it has a compareTo method just like String does. So your custom Comparator could look like this: public class CustomComparator implements Comparator<MyObject> { @Override public int compare(MyObject o1, MyObject o2) { return o1.getStartDate().compareTo(o2.getStartDate()); } } The compare() method must return an int, so you couldn’t directly return a boolean like you … Read more

How to parse JSON in Java

The org.json library is easy to use. Just remember (while casting or using methods like getJSONObject and getJSONArray) that in JSON notation [ … ] represents an array, so library will parse it to JSONArray { … } represents an object, so library will parse it to JSONObject Example code below: import org.json.*; String jsonString … Read more

Can’t execute jar- file: “no main manifest attribute”

First, it’s kind of weird, to see you run java -jar “app” and not java -jar app.jar Second, to make a jar executable… you need to jar a file called META-INF/MANIFEST.MF the file itself should have (at least) this one liner: Main-Class: com.mypackage.MyClass Where com.mypackage.MyClass is the class holding the public static void main(String[] args) … Read more

Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop

Iterator.remove() is safe, you can use it like this: List<String> list = new ArrayList<>(); // This is a clever way to create the iterator and call iterator.hasNext() like // you would do in a while-loop. It would be the same as doing: // Iterator<String> iterator = list.iterator(); // while (iterator.hasNext()) { for (Iterator<String> iterator = … Read more

How to get the current working directory in Java?

Code : public class JavaApplication { public static void main(String[] args) { System.out.println(“Working Directory = ” + System.getProperty(“user.dir”)); } } This will print the absolute path of the current directory from where your application was initialized. Explanation: From the documentation: java.io package resolve relative pathnames using current user directory. The current directory is represented as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)