M Zia

Greenhorn
+ Follow
since Nov 24, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by M Zia

ALL, Thanks for your support on this forum. It means a lot.
So for future reference, if a method's return type is an interface, we know that it is returning an object that is an instance of a class that implements that interface and we can assume all the methods listed in the interface are implemented in the class which can be accessed via the object instance invocation, set.iterator() in this particular case. This is was my source of confusion which caused a while(1) loop to spew forth from my end. Sorry for that.
12 years ago
Where is the documentation that states that EntrySet is an inner class of HashMap class that implements the Set interface and the iterator method??? How can i make progress when it becomes impossible to get documentation?? In the documenation for the entrySet() method for HashMap it does not state that an object instance of EntrySet inner class is being returned. It just states Set is being returned. Where do we find this??? any book/docs???
12 years ago
Bear,
I need to know because otherwise i am living in a vacuum. I just need to know what class that object that is returned belongs to and read the class implementation code. I would like to know what the iterator() method in the set interface's implementation looks like. Without having access to the class , it appears that the object is coming out of nowhere.
12 years ago
I did a getClass().getName on the "set" interface implementing object and this is what came back:
name of class: java.util.HashMap$EntrySet

What does it mean???
12 years ago
Bear, Joanne,
If hashmap.entrySet() returns an object which is an instance of a class that implements the Set interface, how would one go about finding that class that is implementing the interface. Without knowing that class, the code i pasted does not make sense to me since we do not know that class listing.
12 years ago
In the following code set.iterator() gets called and there is no class shown implementing the Set Interface.
How and why is this happening. can anyone explain?

import java.util.*;
class HashMapDemo {
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();
// Put elements to the map
hm.put("John Doe", new Double(3434.34));
hm.put("Tom Smith", new Double(123.22));
hm.put("Jane Baker", new Double(1378.00));
hm.put("Todd Hall", new Double(99.22));
hm.put("Ralph Smith", new Double(-19.08));
// Get a set of the entries
Set set = hm.entrySet();
// Get an iterator
Iterator i = set.iterator();

// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account
double balance = ((Double)hm.get("John Doe")).doubleValue();
hm.put("John Doe", new Double(balance + 1000));
System.out.println("John Doe's new balance: " +
hm.get("John Doe"));
}
}
12 years ago
James, Joanne,
Thanks for replying so fast. I really appreciate it.
So in the above code, "students" refers to an array of 10 String objects.
When and if we set the "students" reference to null by setting students = null,
then the array of 10 String objects will be garbage collected?? or do we
have to set every member of the array to null in a for loop??
12 years ago
James,
The "String studentname" is not an object in this case and will not be garbage collected
after setting it to null??
12 years ago
Joanne,
but there is no call to entries.nextElement being made inside the for loop.
are you saying this happens automatically or hasMoreElements calls nextElement.
Please clarify.
Thanks for your assistance.
12 years ago

RC, Cambell,
in the code listed above, how does the iteration take place:
for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();)

generally a for loop is of the form for (i=0; i<10; i++)
can you explain the for loop above for me please??
Thanks again for your assistance.
12 years ago
I have a question which relates to the following exercise from Java tutorial

{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{

1.

The following code creates one array and one string object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection?

...
String[] students = new String[10];
String studentName = "Peter Parker";
students[0] = studentName;
studentName = null;
...

}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

The string object above called studentname should be garbage collected now since it
points to null. however the answer book on the above says neither objects will be garbage
collected. do you know what is really happening???
12 years ago
for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();)

In the above entry , entries appears to be an object from an inner anonymous class who's method inherited
from Enumeration is being called, with name hasMoreElements. But hasMoreElements has not been specified
anywhere in the code listed in the java tutorial.
What is happening here? How can the hasMoreElements method be called likewise as in above without being
specified and listed first with its code.
Please help.
Thanks,
MKZ
12 years ago