Monday, August 22, 2011

Test Collections


package com.evs.objava33.class13;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Vector;

public class TestCollections {

public static void main(String[] args) {
// Sorted Duplicate Ordered
// Set No Yes No
// Sequence / List No/Yes Yes Yes
// Map Yes No Yes

// Set -> HashSet, LinkedHashSet, TreeSet, EnumSet
// List -> Vector, Stack, LinkedList, ArrayList, Queue
// Map -> Hashtable, HashMap, LinkedHashMap, WeakHashMap
// IdentityHashMap, TreeMap

Vector<Person> vec = new Vector<Person>();
vec.add(new Person(30, "Shahzad", "shahzad@gmail.com"));
vec.add(new Person(21, "Ahmad", "zeeshan@gmail.com"));
vec.add(new Person(11, "Mahmood", "mahmood@gmail.com"));

System.out.println(vec);
// System.out.println(vec.indexOf(new Person(21, "", "")));
// System.out.println(vec.contains(new Person(21, "", "")));
Collections.sort(vec);
System.out.println(vec);
Collections.sort(vec, new Comparator<Person>() {
public int compare(Person o1, Person o2) {
return o1.getEmail().compareTo(o2.getEmail());
}
});
System.out.println(vec);

// Date
java.sql.Date date = new java.sql.Date(System.currentTimeMillis());

// Time
Time time = new Time(System.currentTimeMillis());

// Timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());

// Calendar
Calendar cal = Calendar.getInstance();
// cal.add(Calendar.DAY_OF_MONTH, 15);
// java.sql.Date dt = new java.sql.Date(cal.getTimeInMillis());
// cal.add(Calendar.DAY_OF_WEEK, -2);
// cal.get(Calendar.DAY_OF_WEEK);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, -3);
java.sql.Date begin = new java.sql.Date(cal.getTimeInMillis());
cal.add(Calendar.MONTH, 4);
cal.add(Calendar.DAY_OF_MONTH, -1);

}

private static class Person implements Comparable<Person> {
private Integer id;
private String name;
private String email;

public int compareTo(Person o) {
// Small = -1
// Equal = 0
// Greater = 1
int idx = getId().compareTo(o.getId());
return idx == 0 ? getName().compareTo(o.getName()) : idx;
}

public boolean equals(Object obj) {
// return super.equals(obj);
Person p = (Person) obj;
return p.getId().intValue() == getId().intValue();
}

public Person(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}

/**
* @return the id
*/
public Integer getId() {
return id;
}

/**
* @param id
*            the id to set
*/
public void setId(Integer id) {
this.id = id;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
*            the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the email
*/
public String getEmail() {
return email;
}

/**
* @param email
*            the email to set
*/
public void setEmail(String email) {
this.email = email;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", email=" + email
+ "]";
}
}
}

No comments:

Post a Comment