2. 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:
One
Two
Three
3. More iterators
//more eye-friendly iteration
for (String s : lst)
System.out.println(s);
Gives the same output
Most generic iteration—using Iterator
Iterator<String>it = lst.iterator();
while (it.hasNext())
System.out.println(it.next());
Gives the same output
4. Iterating over a set
Set<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:
One
Two
Three
5. Iterating over a map
Map<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--333333
One--111111
Two--222222
6. 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]);
Output:
One
Two
Three
String[] str = new String[]{"One", "Two", "Three"};
for (String s : str)
System.out.println(s);
Output:
One
Two
Three
7. Some common methods present in
all objects
toString()
equals()
hashCode()
finalize()
8. 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
9. 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--John
1--John
10. 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;
}
}
11. 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: Now we uncomment the equals() method in the
3 previous slide:
Printing map:
1--John=null Output:
1--John=null 2
2--John=null Printing map:
1--John=null
2--John=null
Explanation: hashCode merely specifies a slot;
equals() helps put the object in the slot
12. 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;
}
14. 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
15. 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:
16. 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:
17. 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
18. 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