Previous Lecture Lecture 14 Next Lecture

Lecture 14, Mon 05/20

More on Interface Types, Java Collections

Extending Interfaces

Example

public interface InterfaceA {
    public abstract void method1();
    public abstract void method2();
}
public interface InterfaceB extends InterfaceA {
    public abstract void method3();
}
public interface InterfaceC {
    public abstract void method4();
}
public class ClassA implements InterfaceA {
    public void method1() { System.out.println("ClassA.method1"); }
    public void method2() { System.out.println("ClassA.method2"); }
}
public class ClassB implements InterfaceB {
    public void method1() { System.out.println("ClassB.method1"); }
    public void method2() { System.out.println("ClassB.method2"); }
    public void method3() { System.out.println("ClassB.method3"); }
}
public class ClassC implements InterfaceB, InterfaceC  {
    public void method1() { System.out.println("ClassC.method1"); }
    public void method2() { System.out.println("ClassC.method2"); }
    public void method3() { System.out.println("ClassC.method3"); }
    public void method4() { System.out.println("ClassC.method4"); }
}
// Lecture.java

public class Lecture {

    public static void main(String[] args) {
    ClassA a = new ClassA();
    ClassB b = new ClassB();
    ClassC c = new ClassC();

    ArrayList<InterfaceA> listA = new ArrayList<InterfaceA>();
    ArrayList<InterfaceB> listB = new ArrayList<InterfaceB>();
    ArrayList<InterfaceC> listC = new ArrayList<InterfaceC>();

    listA.add(a);
    listA.add(b);
    listA.add(c);

    //listB.add(a); // compilation error
    listB.add(b);
    listB.add(c);

    //listC.add(a); // Compilation error!
    //listC.add(b); // Compilation error!
    listC.add(c);

    for (InterfaceA item : listA) {
        item.method1();
        item.method2();
    }
/*
Output:
ClassA.method1
ClassA.method2
ClassB.method1
ClassB.method2
ClassC.method1
ClassC.method2
*/
    for (InterfaceB item : listB) {
        item.method1();
        item.method2();
        item.method3();
    }
/*
Output:
ClassB.method1
ClassB.method2
ClassB.method3
ClassC.method1
ClassC.method2
ClassC.method3
*/
    for (InterfaceC item : listC) {
        item.method4();
    }
/*
Output:
ClassC.method4
*/
    }
}

Collections

Collection Interface

List Interface

Set

Map

Examples

// HashSet implements Set Interface (no guaranteed order)
HashSet<String> s = new HashSet<String>();
System.out.println(s.add("S1")); //true - successful 
System.out.println(s.add("S2")); // true - successful
System.out.println(s.add("S2")); // false - unsuccessful
System.out.println(s.size()); // 2
System.out.println(s.contains("S1")); // true
System.out.println(s.remove("S1")); // returns true AND removes
System.out.println(s.contains("S1")); // false

// HashMap
HashMap<Integer, String> s = new HashMap<Integer,String>();
System.out.println(s.put(0, "Richert")); // null
System.out.println(s.put(1, "Wang")); // null
System.out.println(s.put(0, "RichARD")); // Richert - returns old value	

System.out.println(s.containsKey(1)); // true
System.out.println(s.containsKey(10)); // false
System.out.println(s.containsValue("Richert")); // false
System.out.println(s.containsValue("RichARD")); // true

// Get Value for specific key
System.out.println(s.get(1)); // Wang

// Traverse Keys
for (Integer i : s.keySet()) {
    System.out.println(i);
}

// Traverse values
for (String i : s.values()) {
    System.out.println(i);
}

System.out.println(s.remove(0)); // RichARD
System.out.println(s.containsValue("RichARD")); // false
System.out.println(s.remove(1, "fjskj")); // false
System.out.println(s.remove(1, "Wang")); // true
System.out.println(s.size()); // 0