View
536
Download
0
Category
Preview:
DESCRIPTION
Citation preview
Lecture 13
Iteration in Java
Object Oriented ProgrammingEastern University, Dhaka
Md. Raihan Kibria
Simple c-style iteration
public class IterationDemo {
public static void main(String[] args) {List<String>lst = new ArrayList<String>();lst.add("One");lst.add("Two");lst.add("Three");
for (int i=0; i<lst.size(); i++)System.out.println(lst.get(i));
}}
Output: OneTwoThree
More iterators//more eye-friendly iterationfor (String s : lst) System.out.println(s);
Gives the same output
Iterator<String>it = lst.iterator();while (it.hasNext()) System.out.println(it.next());
Gives the same output
Most generic iteration—using Iterator
Iterating over a setSet<String>s = new HashSet<String>();s.add("One");s.add("Two");s.add("Three");
Iterator<String>it = lst.iterator();while (it.hasNext()) System.out.println(it.next());
Gives the same output:OneTwoThree
Iterating over a mapMap<String, String>map = new HashMap<String, String>();map.put("One", "111111");map.put("Two", "222222");map.put("Three", "333333");
Iterator<Map.Entry<String, String>>it = map.entrySet().iterator();while (it.hasNext()){ Map.Entry<String, String>entry = it.next(); System.out.println(entry.getKey() + "--" + entry.getValue());}
Output:
Three--333333One--111111Two--222222
Remember old style iteration still works for arrays
String[] str = new String[]{"One", "Two", "Three"};for (int i=0; i<str.length; i++) System.out.println(str[i]);
String[] str = new String[]{"One", "Two", "Three"};
for (String s : str) System.out.println(s);
Output:OneTwoThree
Output:OneTwoThree
Some common methods present in all objects
toString()equals()hashCode()finalize()
toString()public class Student { int id; String name;
public Student(int id, String name) {super();this.id = id;this.name = name;
}
public String toString(){return this.id + "--" + this.name;
}}
public static void main(String[] args){Student student = new Student(3, "Joe");System.out.println(student);
}
Output:3--Joe
Another toString() demo
List<Student>stus = new ArrayList<Student>();Student st = new Student(1, "John");stus.add(st);stus.add(st);System.out.println("Printing list: ");for (Student s: stus)
System.out.println(s);
Output:
Printing list: 1--John1--John
hashCode() demo with equals()public class HashCodeDemo {
public static class Student {int id;String name;
public Student(int id, String name) {super();this.id = id;this.name = name;
}
public String toString() {return this.id + "--" + this.name;
}
/* public boolean equals(Object obj) {Student arg = (Student) obj;return this.id == arg.id;}*/
public int hashCode() {return this.id;
}}
public static void main(String[] args) {Map<Student, String> map = new HashMap<Student, String>();map.put(new Student(1, "John"), null);map.put(new Student(1, "John"), null);map.put(new Student(2, "John"), null);System.out.println(map.size());
Iterator<Map.Entry<Student, String>> it1 = map.entrySet().iterator();System.out.println("Printing map: ");while (it1.hasNext()) System.out.println(it1.next());}
}
Output:3Printing map: 1--John=null1--John=null2--John=null
Now we uncomment the equals() method in the previous slide:
Output:2Printing map: 1--John=null2--John=null
Explanation: hashCode merely specifies a slot; equals() helps put the object in the slot
equals() method Two objects are equal if their equals() method returns true.
Demo:public class Student { int id; String name;
public Student(int id, String name) {super();this.id = id;this.name = name;
}
public String toString(){return this.id + "--" + this.name;
}
public int hashCode() { return this.id; }
public boolean equals(Object obj) {Student s = (Student)obj;if (s.id == this.id)
return true;return false;
}}
equals() continued
System.out.println(st.equals(st2));System.out.println(st==st2);
Output:
truefalse
In java “==” operator is not same as “equals()”
finalize()
What is garbage collection? In C/C++ the programmer can get a chunk
of program using malloc() and can dispose memory using memfree()
Having programmer free will at memory management resulted in memory leaks in many C programs
Java will not let programmer directly acquiring memory.
Rather JVM manages memory
Finalize() (c..)
When an object is de-referenced, the object is automatically removed from the memory by JVM.
Whenever, an object is removed its finalize() method is called
Garbage collection is automatically scheduled by the JVM
However, a programmer can trigger a garbage collection by calling System.gc()
Example in the next page:
Finalize() (c..)public class AnObject {
protected void finalize() throws Throwable {super.finalize();
System.out.println("Finalize method is called"); }
public static void main(String[] args){new AnObject();
}}
Output:
Finalize() (c..)public class AnObject {
protected void finalize() throws Throwable {super.finalize();System.out.println("Finalize method is called");
}
public static void main(String[] args){new AnObject();System.gc();
}}
Output:Finalize method is called
Garbage collection formally defined
Garbage collection is a mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection
Recommended