Compile and run Eclipse Java projects from Linux terminal

If you are developing a Java project in Eclipse under a linux system, you may want to remote access the project from another location. You can remote desktop the linux box by using teamviewer, but sometimes that can be very slow. You can also edit, compile and execute your Java project from a regular ssh terminal. Using terminal to edit, compile and run your remote eclipse project is often faster. This post shows you how to compile and run eclipse project in terminal.

Read more

Java Remove Element from ArrayList

To remove some elements from an ArrayList while iterating over the ArrayList, we need to use Iterator. Integer[] arr = {1,2,3,4,5,6}; ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr)); System.out.println(list);   Iterator<Integer> iter = list.iterator(); while(iter.hasNext()){ int i = iter.next(); if(i==5) iter.remove(); }   System.out.println(list);Integer[] arr = {1,2,3,4,5,6}; ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr)); System.out.println(list); Iterator<Integer> iter = list.iterator(); … Read more

Compile and Run Java in Command Line with External Jars

The following example shows how to compile and run Java program in command line mode with external jars. It is developed under Linux. 1. Compile & Run Java Program Without External Jar Let’s create a simple hello world program “helloworld.java”. public class helloworld{ public static void main(String[] args){ System.out.println("Hello!"); } }public class helloworld{ public static … Read more

Java Serialization

What is Serialization? In Java, object serialization means representing an object as a sequence of bytes. The bytes includes the object’s data and information. A serialized object can be written into a file/database, and read from the file/database and deserialized. The bytes that represent the object and its data can be used to recreate the … Read more

Java Varargs Examples

1. What is Varargs in Java? Varargs (variable arguments) is a feature introduced in Java 1.5. It allows a method take an arbitrary number of values as arguments. public static void main(String[] args) { print("a"); print("a", "b"); print("a", "b", "c"); }   public static void print(String … s){ for(String a: s) System.out.println(a); }public static void … Read more

How to Calculate Time Difference in Java?

Given two strings of time, you may want to calculate the difference between them. The following provide a simple solution. import java.text.SimpleDateFormat; import java.util.Date;   public class Main { public static void main(String[] args) throws Exception{ String time1 = "12:00:00"; String time2 = "12:01:00";   SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); Date date1 = format.parse(time1); Date … Read more

Java Enum Examples

An enum in Java is just like any other class, with a predefined set of instances. Here are several examples to highlight how to use Java Enum. 1. Simple Example public enum Color { RED, YELLOW, BLUE; //each is an instance of Color }public enum Color { RED, YELLOW, BLUE; //each is an instance of … Read more

Top 9 questions about Java Maps

In general, Map is a data structure consisting of a set of key-value pairs, and each key can only appears once in the map. This post summarizes Top 9 FAQ of how to use Java Map and its implemented classes. For sake of simplicity, I will use generics in examples. Therefore, I will just write Map instead of specific Map. But you can always assume that both the K and V are comparable, which means K extends Comparable and V extends Comparable.

Read more