Archive for the ‘java.util.Collections’ Category

PostHeaderIcon Java Comparators

The Collections class has a Sort method that takes a Comparator. The Comparator gives you the option of sorting the Collection in a variety of ways. A Comparator unlike the Comparable interface allows you to Sort instances of classes even if you cannot access the class source code. Running the source code below (TestPerson.java) will first sort an ArrayList using comparable before running a method and then sorting using the Comparator.

//Person class*****

public class Person implements PersonInterface, Comparable
{
private String id;
private String name;
private String dateOfBirth;
private int age;

public Person (String id2, String name2, String dateOfBirth2, int age2)
{
id = id2;
name = name2;
dateOfBirth = dateOfBirth2;
age = age2;

}

public String toString()
{
//return id +” “+name+” “+dateOfBirth+” “+age;
return name;
}

public int compareTo(Object o)
{
Person p = (Person)o;
return name.compareTo(p.getName());
}

public void setId(String id2)
{
id = id2;
}

public void setName(String name2)
{
name = name2;
}

public void setDateOfBirth(String dateOfBirth2)
{
dateOfBirth = dateOfBirth2;
}

public String getId()
{
return id;
}

public String getName()
{
return name;
}

public String getDateOfBirth()
{
return dateOfBirth;
}

public void setAge(int age2)
{
age = age2;
}

public int getAge()
{
return age;
}

public boolean equals(Object o)
{
if ((o instanceof Person) && (((Person)o).getAge() == this.age))
{
return true;
}
else
{
return false;
}
}

public int hashCode()
{
return age;
}
}

//*****

//Person Comparator*****

import java.util.*;

public class PersonComparator implements Comparator<Person>
{
public int compare (Person one, Person two)
{
return one.getDateOfBirth().compareTo(two.getDateOfBirth());
}
}

//*****

//Person Interface*****

public interface PersonInterface
{
public void setId(String id2);
public void setName(String name2);
public void setDateOfBirth(String dateOfBirth2);
public String getId();
public String getName();
public String getDateOfBirth();
public int compareTo (Object o);
}

//*****

//Test Person*****

import java.util.*;
import java.util.ArrayList;
import java.util.List;
public class TestPerson
{
public static void main (String [] args)
{
Person p1 = new Person (“1″, “Joe Blogs”, “04/11/1981″, 28);
//System.out.println(“Name before change = ” +p.getName());
//p.setName(“Ian F Stanford”);
//System.out.println(“Name after change = ” +p.getName());
Person p2 = new Person (“1″, “Janet Blogs”, “04/11/1981″, 28);
Person p3 = new Person (“3″, “Jim Blogs”, “05/01/1984″, 25);
Person p4 = new Person (“4″, “Andrew Blogs”, “09/10/1985″, 24);
Person p5 = new Person (“5″, “Simon Blogs”, “09/10/1989″, 24);

List <Person>People = new ArrayList<Person>(); //these have to be Persons and not PersonInterfaces like with the legacy class I have written – warnings appear when compiling the legacy class.
People.add(p1);
People.add(p2);
People.add(p3);
People.add(p4);
People.add(p5);
System.out.println (“Before sort: ” +People);
Collections.sort(People);
System.out.println (“After sort (By name using Comparable): ” +People);
System.out.println (“Before call to PersonInterface.setName: ” +People);
PersonInterface pi = p1;
p1.setName(“Ian Stanford”);
System.out.println (“After call to PersonInterface.setName: ” +People);
PersonComparator externalComparator = new PersonComparator();
Collections.sort(People, externalComparator);
System.out.println (“After sort (By name using Comparator): ” +People);

//If .equals() was not overridden in Person, then references would be compared instead of comparing by Name attribute
if (p1.equals(p2))
{
System.out.println(“Persons 1 and 2 have the same name”);
}
else
{
System.out.println(“Persons 1 and 2 don’t have the same name”);
}

if (p1.equals(p5))
{
System.out.println(“Persons 1 and 5 have the same name”);
}
else
{
System.out.println(“Persons 1 and 5 don’t have the same name”);
}
}
}

//*****

PostHeaderIcon Java Generics

Generics are a new feature of Java 1.5 and are generally used for validation purposes at compile time. For example, if you have an Array List of Person objects, then you can use Generics to ensure that every object that is added to the Array List is a Person and not another Object e.g. a Cat or a Dog Object. The example in this post is very simple as Generics can become more complicated after considering Inheritance.

//Person Class*****

public class Person implements PersonInterface, Comparable
{
private String id;
private String name;
private String dateOfBirth;
private int age;

public Person (String id2, String name2, String dateOfBirth2, int age2)
{
id = id2;
name = name2;
dateOfBirth = dateOfBirth2;
age = age2;

}

public String toString()
{
//return id +” “+name+” “+dateOfBirth+” “+age;
return name;
}

public int compareTo(Object o)
{
Person p = (Person)o;
return name.compareTo(p.getName());
}

public void setId(String id2)
{
id = id2;
}

public void setName(String name2)
{
name = name2;
}

public void setDateOfBirth(String dateOfBirth2)
{
dateOfBirth = dateOfBirth2;
}

public String getId()
{
return id;
}

public String getName()
{
return name;
}

public String getDateOfBirth()
{
return dateOfBirth;
}

public void setAge(int age2)
{
age = age2;
}

public int getAge()
{
return age;
}

public boolean equals(Object o)
{
if ((o instanceof Person) && (((Person)o).getAge() == this.age))
{
return true;
}
else
{
return false;
}
}

//*****

//Person Interface

public interface PersonInterface
{
public void setId(String id2);
public void setName(String name2);
public void setDateOfBirth(String dateOfBirth2);
public String getId();
public String getName();
public String getDateOfBirth();
public int compareTo (Object o);
}

//*****

//TestPerson*****

import java.util.*;
import java.util.ArrayList;
import java.util.List;
public class TestPerson
{
public static void main (String [] args)
{
Person p1 = new Person (“1″, “Joe Bloggs”, “04/11/1981″, 28);
//System.out.println(“Name before change = ” +p.getName());
//p.setName(“Ian F Stanford”);
//System.out.println(“Name after change = ” +p.getName());
Person p2 = new Person (“1″, “Jim Bloggs”, “04/11/1981″, 28);
Person p3 = new Person (“3″, “Janet Bloggs”, “05/01/1984″, 25);
Person p4 = new Person (“4″, “Andy Simmons”, “09/10/1985″, 24);
Person p5 = new Person (“5″, “Craig Harwood”, “09/10/1985″, 24);

List <Person>People = new ArrayList<Person>(); //these have to be Persons and not PersonInterfaces like with the legacy class I have written – warnings appear when compiling the legacy class.
People.add(p1);
People.add(p2);
People.add(p3);
People.add(p4);
People.add(p5);
System.out.println (“Before sort: ” +People);
Collections.sort(People);
System.out.println (“After sort: ” +People);
System.out.println (“Before call to PersonInterface.setName: ” +People);
PersonInterface pi = p1;
p1.setName(“Ian Stanford”);
System.out.println (“After call to PersonInterface.setName: ” +People);

//If .equals() was not overridden in Person, then references would be compared instead of comparing by Name attribute
if (p1.equals(p2))
{
System.out.println(“Persons 1 and 2 have the same name”);
}
else
{
System.out.println(“Persons 1 and 2 don’t have the same name”);
}

if (p1.equals(p5))
{
System.out.println(“Persons 1 and 5 have the same name”);
}
else
{
System.out.println(“Persons 1 and 5 don’t have the same name”);
}
}
}

PostHeaderIcon Java Collections

//Student.java

import java.io.*;
import java.util.*;

public class Student extends Person implements Serializable, Comparable
{
private int studentID; //Assuming StudentID is an int
private String course;

public Student (int studentID2, String course2, String name2, int age2, String address2, String telephoneNumber2)
{
super(name2, age2, address2, telephoneNumber2);
System.out.println(“Student constructor called with the following values: “+studentID2+” “+course2+” “+name2+” “+age2+” “+address2+” “+telephoneNumber2);
studentID = studentID2;
course = course2;
}

public Student()
{
super();
System.out.println(“Constructor called with no arguents”);
studentID = 0;
course = “”;
}

public int compareTo(Object o)
{
Student s = (Student)o;
return super.getName().compareTo(s.getName());
//return name.compareTo(s.getName());
}

public void setCourse(String course2)
{
course = course2;
}

public String getCourse()
{
return course;
}

public int getStudentID()
{
return studentID;
}

public static Student getList(List<Student> l)
{
return (Student)l.get(0);
}

public boolean equals(Object o)
{
if ((o instanceof Person) && (((Student)o).getStudentID() == this.getStudentID()))
{
return true;
}
else
{
return false;
}
}

public int hashCode()
{
return this.getStudentID();
}
}

import java.util.*;
import java.util.List;
import java.util.Map;
//import java.util.HashTable;

public class StudentGenerics
{
public static void main (String[] args)
{
try
{
Student s1 = new Student(1, “Business Information Systems”, “Nan Stanford”, 28, “30 Glenville Close”, “07955432329″);
Student s2 = new Student(2, “Computer Science”, “James Swat”, 28, “32 Mark Close”, “07125404329″);
Student s3 = new Student(1, “Computer Science”, “Mark Chip”, 28, “32 James Close”, “07125544329″);
List <Student> StudentList = new ArrayList<Student>();
StudentList.add(s1);
StudentList.add(s2);
StudentList.add(s3);
System.out.println();
System.out.println();
Student s4 = Student.getList(StudentList);
System.out.println(“Before Sorting using Comparable: ” +s4.getCourse());
Collections.sort(StudentList);
Student s5 = Student.getList(StudentList);
System.out.println(“After Sorting using Comparable: ” +s5.getCourse());
System.out.println();
System.out.println();

Map <String, Student> StudentMap = new HashMap<String, Student>();
StudentMap.put(s1.getName(), s1);
//Map.sort(StudentMap);
System.out.println(s1.getName());
Student s6 = (Student)StudentMap.get(“Nan Stanford”);
System.out.println(s6.getName());

Map<String, Student> StudentHashTable = new Hashtable <String, Student>();
StudentHashTable.put(s2.getName(), s2);
//Collections.sort(StudentHashtable);
Student s7 = (Student)StudentHashTable.get(“James Swat”);
System.out.println(s7.getName());

SortedMap<String, Student> StudentTree = new TreeMap<String, Student>();
StudentTree.put(s3.getName(), s3);
Student s8 = (Student)StudentTree.get(“Mark Chip”);
System.out.println(s8.getName());

Set <Student> StudentSet = new HashSet <Student>();
StudentSet.add(s1);
StudentSet.add(s2);
StudentSet.add(s3);
StudentSet.get(s1);

}
catch (NullPointerException n)
{
System.out.println(“There was a null pointer exception: “+n);
}
}
}

Adsense