Java – Iterator, Iterable

Iterator

An Iterator is an interface which allows iterating over collection
It provides hasNext() and next() methods to iterate as shown below –

Iterator lists= Arrays.asList("A","B","C").iterator();
		while(lists.hasNext()) {
			System.out.println(lists.next());
		}

Iterable

An Iterable represents a collection which can be traversed.
Implementing the Iterable() interface allows an object to make use of a for-each loop.

package com.pwn.collection;
import java.util.Iterator;

public class CustomIterable {

	public static void main(String[] args) {
		System.out.println("Alphabet List below ");
		for (char c : new Alphabet()) {
			System.out.println(c);
		}
	}
}

class Alphabet implements Iterable<Character> {
	@Override
	public Iterator<Character> iterator() {
		return new Iterator<Character>() {
			char letter = 'a';

			@Override
			public boolean hasNext() {
				return letter <= 'z';
			}

			@Override
			public Character next() {
				return letter++;
			}

			@Override
			public void remove() {
				throw new 
                                UnsupportedOperationException(
		            "Doesn't make sense to remove a letter");
			}
		};
	}
}
Alphabet List below 
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

Java – Execute Javascipt

Nashorn is a JavaScript engine developed in Java by Oracle, and has been released with Java 8. Nashorn allows embedding Javascript in Java applications via JSR-223 and allows to develop standalone Javascript applications, and it provides better runtime performance and better compliance with the ECMA normalized Javascript specification.

Java 8 introduces new command-line tool called jjs, for executing JavaScript. It can be found in the bin/ folder of a JDK installation along with the well-known java, javac, or jar tools.

The jjs tool accepts a list of JavaScript source code files as arguments.

Please note this program run in JDK version 8 or greater.

 
package com.pwn.js;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ExecuteJS {

	public static void main(String[] args) {
		// Obtain an instance of the JavaScript engine
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("nashorn");
		/*
		 * hello.js source code 
		 * var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
		 * var evenNumbers = array.filter(function(i) { return i % 2 == 0; });
		 * print('Output from JS file ',evenNumbers);
		 */

		// Load and execute a script from the file 'hello.js'
		try {
			engine.eval(
					new FileReader(new File("c://install/hello.js")));
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (ScriptException ex) {
			ex.printStackTrace();
		}

	}

}

Output from JS file  2,4,6,8,10

Java – Writing XML using JAXB

 
package com.pwn.xml;

import java.io.File;
import javax.xml.bind.JAXB;

public class XMLWriter {

	public static void main(String[] args) {
		User user = new User();
		user.setName("Johnny");
		user.setUserID(12313);
		try {
			JAXB.marshal(user,
					new File("C://install//userDetail.xml"));
		} catch (Exception e) {
			System.err.println(
					"Exception occurred while writing in XML!");
		} finally {
			System.out.println("XML created");
		}

	}

}


@XmlRootElement
class User {
	private long userID;
	private String name;

	public long getUserID() {
		return userID;
	}

	public void setUserID(long userID) {
		this.userID = userID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

Java – Convert java.util.Date() to java.sql.Date()

 

package com.pwn.dateUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DateToSQLDateConversion {

	public static void main(String[] args) {
		java.util.Date utilDate = new java.util.Date();
		System.out.println("java.util.Date is : " + utilDate);
		java.sql.Date sqlDate = convert(utilDate);
		System.out.println("java.sql.Date is : " + sqlDate);
		DateFormat df = new SimpleDateFormat("dd/MM/YYYY - hh:mm:ss");
		System.out.println(
				"dateFormated date is : " + df.format(utilDate));

	}

	private static java.sql.Date convert(java.util.Date utilDate) {
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
		return sqlDate;
	}

}

java.util.Date is : Mon Jan 29 16:53:36 PST 2018
java.sql.Date is : 2018-01-29
dateFormated date is : 29/01/2018 - 04:53:36