How to loop / iterate a List in Java

Here i show you four ways to loop a List in Java. Iterator loop For loop For loop (Adcance) While loop package com.mkyong.core; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class ArrayToList { public static void main(String[] argv) { String sArray[] = new String[] { "Array 1", "Array 2", "Array 3" }; // convert array …

Read more

How to convert Array to List in Java

Java example to show you how to convert a Array to a List ArrayToList.java package com.mkyong; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ArrayToList { public static void main(String[] argv) { String sArray[] = new String[] { "A", "B", "C" }; // convert array to list #1 List<String> list = Arrays.asList(sArray); System.out.println(list); …

Read more

Bind variables Performance Test in Java

I heard a lot of people talking about “Bind variables” will increase java application performance. Is this really true? i skeptical and make a simple performance test between Bind variables in PreparedStatement class and Non Bind variables in Statement class How do i test it? I will create a simple java class and keep sending …

Read more

How to modify date time (Date Manipulation) – Java

Java Calendar class (java.util.Calendar) is a very useful and handy class in java date time manipulation. here i will demonstrate how to modify date time with calender class. Get current date time with Calendar() DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); System.out.println("Current Date Time : " + dateFormat.format(cal.getTime())); Calender date time manipulation …

Read more

Java – How to read input from the console

In Java, there are three ways to read input from a console : System.console (JDK 1.6) Scanner (JDK 1.5) BufferedReader + InputStreamReader (Classic) 1. System.console Since JDK 1.6, the developer starts to switch to the more simple and powerful java.io.Console class. JavaConsole.java package com.mkyong.io; import java.io.Console; public class JavaConsole { public static void main(String[] args) …

Read more

Is there any constant in Java? how to declare it?

Recently, I’m been asked by my colleague (C++ programmer) regarding why java does not contains a constant keyword? Actually java do contains constant function , but it just appear as different keyword – final. A final variable in java is equal to C++ constant. The final keyword is declare before a data type, it make …

Read more

How to read file in Java – BufferedInputStream

Here is another example to show how to read a file in Java with BufferedInputStream and DataInputStream classes. The readLine() from the type DataInputStream is deprecated. Sun officially announced this method can not convert property from bytes to characters. It’s advised to use BufferedReader. You may interest to read this How to read file from …

Read more

How to read file in Java – BufferedReader

In this article, we will show you how to use java.io.BufferedReader to read content from a file Note Read this different ways read a file 1. Files.newBufferedReader (Java 8) In Java 8, there is a new method Files.newBufferedReader(Paths.get(“file”)) to return a BufferedReader filename.txt A B C D E FileExample1.java package com.mkyong; import java.io.BufferedReader; import java.io.IOException; …

Read more

Java HashMap example

HashMap is an object that stores both key=value as a pair. This HashMap permits null values and the null key, unsynchronized and no guarantees to the order of the map. ["key","value"] = ["java","mkyong.com"] 1. HashMap Basic 1.1 Add an Item Map map = new HashMap<String, String>(); map.put("PostgreSQL", "Free Open Source Enterprise Database"); 1.2 Get an …

Read more

Java Web Start (Jnlp) Tutorial

Here is a brief explanation about Java Web Start from SUN “Java Web Start is a mechanism for program delivery through a standard Web server. Typically initiated through the browser, these programs are deployed to the client and executed outside the scope of the browser. Once deployed, the programs do not need to be downloaded …

Read more

Java decompiler plugin for Eclipse IDE

In Eclipse IDE, we can use Enhanced Class Decompiler plugin to decompile Java class files without source code directly. After installing and configuring the Enhanced Class Decompiler plugin, click on the class or methods, press F3, and the plugin will automatically decompile the Java class. Table of contents 1. What is Enhanced Class Decompiler plugin? …

Read more

Open Browser in Java windows or Linux

A very useful Java code, to open a web browser from Java application in windows or Linux. package com.mkyong; public class StartBrowser { public static void main(String args[]) { String url = "http://www.google.com"; String os = System.getProperty("os.name").toLowerCase(); Runtime rt = Runtime.getRuntime(); try{ if (os.indexOf( "win" ) >= 0) { // this doesn’t support showing urls …

Read more

ClassNotFoundException: org.apache.commons.logging.LogFactory

Starting a web application, but hits the following error messages : … Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory … 1. Normal Case 1.1 Obviously, the Apache Commons logging is missing – commons-logging-xxx.jar. To fix it, get it from Maven central repository. pom.xml <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> 2. Spring Case 2.1 For Spring application, developers always excluded …

Read more

How to install java jdk on fedora core (linux)

How to install java jdk on fedora core? here i provide few steps to demonstrate how it’s work. Installation Setup 1 ) Please visit sun java website to download any java jdk version you like. http://java.sun.com/javase/downloads/index.jsp 2 ) Click download, select Linux platform, language and accept license and continue. 3 ) Select “Linux RPM in …

Read more

Java – Check if array contains duplicated value

This article shows a few ways to detect if an array contains a duplicated value. Java 8 Stream, compare the array size. TreeSet, compare the array size. Two for loops, classic algorithm, 0(n^2). Set, 0(n). Bitmap, boolean[] 0(n). All the above solutions are tested with the below program and generate the same output. (except 5. …

Read more