Sunday, November 9, 2014
Arrays.sort(fruits, Fruit.FruitNameComparator) - Fruit static object FruitNameComparator
import java.util.Arrays;
public class ComparatorTest {
public static void main(String[] args){
Fruit fruit1 = new Fruit("banana",1);
Fruit fruit2 = new Fruit("apple",2);
Fruit fruit3 = new Fruit("orange",3);
Fruit[] fruits = {fruit1, fruit2, fruit3};
// we can write comparator based on properties
Arrays.sort(fruits,Fruit.FruitNameComparator);
for (Fruit fruit: fruits){
System.out.println(fruit.getName());
}
}
}
import java.util.Comparator;
public class Fruit implements Comparable{
public static final Comparator<Fruit> FruitNameComparator = new Comparator<Fruit>(){
@Override
public int compare(Fruit fruit1, Fruit fruit2) {
// fruit1.getName() is String, String.compareTo
// String, Integer, Double all have compareTo()
return fruit1.getName().compareTo(fruit2.getName());
}
};
public Fruit(String name, int quantity) {
super();
this.name = name;
this.quantity = quantity;
}
String name;
int quantity;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
// comparable means this object is comparable so it can compareTo other object
@Override
public int compareTo(Object compareFruit) {
int compareFruitQuantity = ((Fruit)compareFruit).getQuantity();
//ascending order
return this.getQuantity() - compareFruitQuantity;
}
}
OUTPUT
apple
banana
orange
Arrays.sort(Fruit) - Fruit needs to implement Comparable
Comparable is java.lang.Comparable
Comparator is java.util.Comparator
Comparable means this object is Comparable, it can compareTo other object
Comparator means tool can compare two objects, so it has method compare(object1, object2)
Comparable compare based on single property
Comparator could compare based on multiple properties
import java.util.Arrays;
public class ComparableTest {
public static void main(String[] args){
Fruit fruit1 = new Fruit("banana",1);
Fruit fruit2 = new Fruit("apple",2);
Fruit fruit3 = new Fruit("orange",3);
Fruit[] fruits = {fruit1,fruit2,fruit3};
Arrays.sort(fruits);
for (Fruit fruit:fruits){
System.out.println(fruit.getName());
}
}
}
public class Fruit implements Comparable{
public Fruit(String name, int quantity) {
super();
this.name = name;
this.quantity = quantity;
}
String name;
int quantity;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
// comparable means this object is comparable so it can compareTo other object
@Override
public int compareTo(Object compareFruit) {
int compareFruitQuantity = ((Fruit)compareFruit).getQuantity();
//ascending order
return this.getQuantity() - compareFruitQuantity;
}
}
OUTPUT
banana
apple
orange
Comparator is java.util.Comparator
Comparable means this object is Comparable, it can compareTo other object
Comparator means tool can compare two objects, so it has method compare(object1, object2)
Comparable compare based on single property
Comparator could compare based on multiple properties
import java.util.Arrays;
public class ComparableTest {
public static void main(String[] args){
Fruit fruit1 = new Fruit("banana",1);
Fruit fruit2 = new Fruit("apple",2);
Fruit fruit3 = new Fruit("orange",3);
Fruit[] fruits = {fruit1,fruit2,fruit3};
Arrays.sort(fruits);
for (Fruit fruit:fruits){
System.out.println(fruit.getName());
}
}
}
public class Fruit implements Comparable{
public Fruit(String name, int quantity) {
super();
this.name = name;
this.quantity = quantity;
}
String name;
int quantity;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
// comparable means this object is comparable so it can compareTo other object
@Override
public int compareTo(Object compareFruit) {
int compareFruitQuantity = ((Fruit)compareFruit).getQuantity();
//ascending order
return this.getQuantity() - compareFruitQuantity;
}
}
OUTPUT
banana
apple
orange
Sort ArrayList using Collections.sort
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class SortArrayListTest {
public static void main(String[] args){
List list = new ArrayList();
list.add("banana");
list.add("apple");
list.add("orange");
Collections.sort(list);
Iterator it = list.listIterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
OUTPUT
apple
banana
orange
Sort Array using Arrays.sort
import java.util.Arrays;
public class SortArrayTest {
public static void main(String[] args){
String[] sa = {"banana","apple","orange"};
Arrays.sort(sa);
for (String s:sa){
System.out.println(s);
}
}
}
OUTPUT
apple
banana
orange
Deep Copy
// deep copy, in clone() it creates a new object
public class DeepCopyTest {
public static void main(String[] args) throws CloneNotSupportedException{
Student student = new Student("Jet","Chinese"); // create student object
Student student2 = student.clone(); // deep copy a new object student2
System.out.println("student name="+student2.getName()+" subject="+student2.getSubject().getName());// print out this new object student2
student.setName("Tom"); // change original object value
student.getSubject().setName("English");
System.out.println("student name="+student2.getName()+" subject="+student2.getSubject().getName()); // print out this new object value. it's changed.
}
}
public class Student implements Cloneable{
private String name;
private Subject subject;
public Student(String studentName, String subjectName) {
this.name = studentName;
this.subject = new Subject(subjectName);
}
@Override
public Student clone() throws CloneNotSupportedException {
Student student = new Student(name,subject.getName());
return student ;
}
public String getName() {
return this.name;
}
public Subject getSubject() {
return this.subject;
}
public void setName(String studentName) {
this.name = studentName;
}
}
package deepcopy;
public class Subject {
private String name;
public Subject(String subjectName) {
this.name = subjectName;
}
public String getName() {
return name;
}
public void setName(String subjectName) {
this.name = subjectName;
}
}
OUTPUT
student name=Jet subject=Chinese
student name=Jet subject=Chinese
public class DeepCopyTest {
public static void main(String[] args) throws CloneNotSupportedException{
Student student = new Student("Jet","Chinese"); // create student object
Student student2 = student.clone(); // deep copy a new object student2
System.out.println("student name="+student2.getName()+" subject="+student2.getSubject().getName());// print out this new object student2
student.setName("Tom"); // change original object value
student.getSubject().setName("English");
System.out.println("student name="+student2.getName()+" subject="+student2.getSubject().getName()); // print out this new object value. it's changed.
}
}
public class Student implements Cloneable{
private String name;
private Subject subject;
public Student(String studentName, String subjectName) {
this.name = studentName;
this.subject = new Subject(subjectName);
}
@Override
public Student clone() throws CloneNotSupportedException {
Student student = new Student(name,subject.getName());
return student ;
}
public String getName() {
return this.name;
}
public Subject getSubject() {
return this.subject;
}
public void setName(String studentName) {
this.name = studentName;
}
}
package deepcopy;
public class Subject {
private String name;
public Subject(String subjectName) {
this.name = subjectName;
}
public String getName() {
return name;
}
public void setName(String subjectName) {
this.name = subjectName;
}
}
OUTPUT
student name=Jet subject=Chinese
student name=Jet subject=Chinese
Friday, November 7, 2014
StringBuilder.insert - insert a string at position
public class InsertTest {
public static void main(String[] args){
StringBuilder sb = new StringBuilder("abcd");
sb.insert(sb.indexOf("b"),"word");// insert word at position of b
System.out.println(sb.toString());
}
}
OUTPUT:
awordbcd
Tuesday, November 4, 2014
Shallow Copy
cloned object has own copy for primitive member but for object member, it will be shared with original object
http://www.jusfortechies.com/java/core-java/deepcopy_and_shallowcopy.php
public class ShallowCopyTest {
public static void main(String[] args) throws CloneNotSupportedException{
Student stud = new Student("Jet","English");
Student stud2 = (Student) stud.clone();
System.out.println("student name="+stud2.getName()+" subject="+stud2.getSubject().getName());
stud.setName("Tom");
stud.getSubject().setName("Chinese");
System.out.println("after original object is updated, cloned student name="+stud2.getName()+" subject="+stud2.getSubject().getName());
}
}
public class Student implements Cloneable{
private String name;
private Subject subject;
public Student(String studentName, String subjectName) {
this.name = studentName;
this.subject = new Subject(subjectName);
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public String getName() {
return this.name;
}
public Subject getSubject() {
return this.subject;
}
public void setName(String studentName) {
this.name = studentName;
}
}
Subscribe to:
Posts (Atom)