Making A Collection Read-Only
Making A Collection Read-Only
Making A Collection Read-Only
try {
// Try modifying the
list
list.set(0, "new
value");
} catch
(UnsupportedOperationException
e) {
// Can't modify
}
/*
Java Comparable example.
This Java Comparable example
describes how
java.util.Comparable interface
is implemented to compare user
defined classe's objects.
*/
import java.util.*;
/*
Comparable interface is used to
make user defined class's
objects comparable.
This interface declares one
method compareTo(Object object)
and determines how ojbects can
be compared to each other.
*/
/*
Implement java.util.Comparable
interface to make class objects
comparable.
*/
class Employee implements
Comparable{
private int age;
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
/*
Signature of compareTo method
is.
public int compareTo(Object
object).
compareTo method should return 0
if both objects are equal, 1 if
first grater than other and -1
if first less than the other
object of the same class.
*/
public int compareTo(Object
otherEmployee){
/*
If passed object is of type
other than Employee, throw
ClassCastException.
*/
if( ! ( otherEmployee instanceof
Employee ) ){
throw new
ClassCastException("Invalid
object");
}
int age = ( (Employee)
otherEmployee ).getAge();
if( this.getAge() > age )
return 1;
else if ( this.getAge() < age )
return -1;
else
return 0;
}
}
public class
JavaComparableExample{
public static void main(String
args[]){
/*
Create two different Employee
objects, so that we can compare
both.
*/
Employee one = new Employee();
one.setAge(40);
Employee two = new Employee();
one.setAge(30);
/*
Use compareTo method to
determine which employee is
younger
*/
if( one.compareTo(two) > 0 ) {
System.out.println("Employee one
is elder than employee two !");
} else if( one.compareTo(two) <
0 ) {
System.out.println("Employee one
is younger than employee
two !");
} else( one.compareTo(two) == 0
) {
System.out.println("Both
employees are same !");
}
}
}
/*
OUTPUT of the above given Java
Comparable Example would be :
Employee one is elder than
employee two !
*/
BgThread() {
// As a daemon
thread, this thread won't
prevent the application from
exiting
setDaemon(true);
}
Copying a Directory
// Copies all files under
srcDir to dstDir.
// If dstDir does not exist,
it will be created.
public void
copyDirectory(File srcDir, File
dstDir) throws IOException {
if
(srcDir.isDirectory()) {
if (!
dstDir.exists()) {
dstDir.mkdir();
}
String[] children =
srcDir.list();
for (int i=0;
i<children.length; i++) {
copyDirectory(ne
w File(srcDir, children[i]),
myObj = null;
myObj = sref.get(); // we
recover the soft reference.
The get method should be
implemented to return the object,
or a null if it was placed to disk,
and we can do it as follows:
import java.land.ref.*;
Weaving Threads
Java provides two ways to create
and run a thread. You can
implement the Runnable interface
or extend the Thread class. The
Runnable interface defines a single
method run(). Consider the
following example of a class that
extends the Runnable interface:
1. Class RunnableClass
implements Runnable {
2. // Class attributes and
methods
3.
4. public void run() {
5. // Code for the run()
method
6. }
7. }
This thread can be started from
another section of the program as
follows:
8. RunnableClass rc = new
RunnableClass();
9. Thread t = new Thread(rc);
10. rc.start();
11. // Other code
The start() method calls the run()
method on Line 4. Program control
returns to Line 11 and
simultaneously to Line 5. At this
point two threads are executing.
The Thread class can also be
extended directly, making the
derived class a Runnable (Thread
extends Runnable):