Java / J2EE
Sort Collection in Java
Example program showing how to sort ArrayList in Java.
//SortArrayList.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SortArrayList {
public static void main(String[] args){
//declare generic ArrayList
ArrayList<Person> list = new ArrayList<Person>();
//add person to list
list.add( new Person("Juan", "Cruz", 16) );
list.add( new Person("Peter", "James", 14) );
list.add( new Person("James", "Bond", 16) );
list.add( new Person("JR", "Galia", 20) );
list.add( new Person("Jack", "Bauer", 11) );
//sort list of Person according to age
Collections.sort(list, new Comparator<Person>(){
public int compare(Person p1, Person p2) {
if( p1.getAge() < p2.getAge() )
return -1;
else if( p1.getAge() > p2.getAge() )
return 1;
else
return 0;
}
});
//display sorted list
System.out.println("\n\tFirst Name\tLast " +
"Name\tAge");
for( Person p: list ){
System.out.println("\t"+p.getFirstName() +
"\t\t" + p.getLastName() + "\t\t"
+p.getAge());
}
}
}
Using for each in Java in Iterating Arraylist
The basic for loop in Java was extended to make iteration over arrays and other collections more convenient. This newer for statement is called the enhanced for or foreach.
Here's an example of using foreach to iterate Arraylist of type Person.
//Person.java
public class Person {
private String firstName = null;
private String lastName = null;
private int age = 0;
public Person(String firstName, String lastName,
int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Euler Tour Traversal of Binary Search Tree
Euler Tour Traversal is a generic traversal of a binary tree. It walks around the tree and visit each node three times:
- on the left, before the Euler tour of the node's left subtree
- from below, as we finish the tour of the left subtree
- on the right, after we finish the Euler tour of the right subtree.
note: if the node is a leaf, we consider the visits to all occur at once
Below is a Euler Tour Traversal implementation in Java. The program will create a Binary Search Tree and then traverse in Euler Tour.
Opening / Displaying Content of Text File in java
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class OpenFile {
public static void main(String[] args)
throws IOException {
String path = "/home/jr/Desktop/file1.txt";
File file = new File(path);
DataInputStream dis = new
DataInputStream(new BufferedInputStream(
new FileInputStream(file) ) );
if( file.exists() )
while (dis.available() != 0)
System.out.println(dis.readLine());
else
System.out.println(" file does not exist");
dis.close();
}
}
Creating Text File in Java
import java.io.File;
import java.io.IOException;
public class CreateFile {
public static void main(String[] args) {
String path = "/home/jr/Desktop/file01.txt";
File file = new File(path);
if ( file.exists() )
System.out.println("file already exist");
else
try {
file.createNewFile();
} catch (IOException e) {
e. Append String to Text File in Java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class AppendToFile {
public static void main(String[] args)
throws IOException {
String path = "/home/jr/Desktop/file1.txt";
String text = "add this text";
File file = new File(path);
FileWriter writer = new
FileWriter(file, true);
writer.appe Validate Input in Java using Regular Expression
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Validate {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Boolean isValid = false;
while( ! Iterating Object Array in Hibernate
Hibernate queries sometimes return tuples of objects in which each tuple is returned as an array. This kind of results are considered scalar. This is how to iterate this kind of query result in hibernate.
Iterator itr = session.createSQLQuery
("select * from table").list().iterator();
while(itr.hasNext()){
Object[] row = (Object[]) itr.next();
System.out.println(row[0]);
System.out.println(row[1]);
System.out.println(row[2]);
}
Struts 2 Validation - Example Program
Example program showing how to validate input in struts 2 using xml configuration file.
AddEntry.java


Recent comments
5 days 3 hours ago
2 weeks 4 days ago
3 weeks 1 hour ago
3 weeks 1 hour ago
4 weeks 2 days ago
4 weeks 5 days ago
4 weeks 5 days ago
6 weeks 1 day ago
7 weeks 3 days ago
7 weeks 3 days ago