• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

collection

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which methods we have to remeber for collections of classes arraylist,
vector,hashset,hashmap...etc
as in k&b no method are given for such classes
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From my experience with the exam, you need to know hierarchy of Collection and Map classes (also note the differences of Collections and Collection).
You also should be able identify the collection classes associated with their features like: sorted, key/map, ordered etc. Understand the specific interfaces they implement.
Most of the questions I got on Collections on the real exam was to identify the class names by giving their features - like of the following:
a) Which collection class allows you to grow or shrink its size and provides index access to its elemenets, and ...bla..bla..
b) Which collections class allows you to associate its elements with key values, and allows you to...bla..bla..
I don't think you really need to know the methods and their features..
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manu
Study about Iterator as well.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
If the questions are only that way, it is well and good. I went through few mock exams, where there were questions like whether Arrays.asList() or Arrays.toList() valid???
and another question on Set interface. The program declares a TreeSet and two Integer objects as
Integer stock1 = new Integer(6);
Integer stock2 = new Integer(6);
and adds these objects to the TreeSet. I guessed that they will be considered as objects (since, both the declarations are two different objects) and assumed both will be stored in the set. But, after excuting the code, i could find only one element added. How will the duplicates be checked if objects are added?? Does all the wrapper classes behave like Strings (using the values/references from stringpool??)
Regards,
Uma...
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Uma,
As you may be aware of the fact that "set and treeset" doesnot allow duplicates. To check for the uniqueness they use toString() of Object class and compare string representation of objects and not the object references.
In the example you have written, when both objects are compared , you will find that they represent string values which are same.
Regards,
Chetan M
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chetan,
i guess you are little bit wrong in your explanation. set and treeset doesnt allow duplicates. but how they implement is different from what you said. if you add string objects, then while adding it uses the string representation to compare whether they are equal or not(to determine duplicates).
But if you add wrapper objects(Integer,Long etc),they use the compareTo method to determine whether they are equal and not the string representation.
just consider this code snippet:
Integer io = new Integer("100");
Long lo = new Long("100");
TreeSet ts = new TreeSet();
ts.add(io);
ts.add(lo);
Iterator it = ts.iterator();
while (it.hasNext()) {
System.out.println("Set Value is : " + it.next());
}

this code complies fine, but throws a runtime exception classcastException,since it cant compare an Integer and a Long objects.
Correct me if i am wrong.
Thanks.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manu -
Unni and Anupam are correct about what you need to know about collections for the exam.
The K&B book (whoever those guys are ), is focused on the exam. It's not a complete Java reference. There are lots of important and useful Java topics that we don't cover in the book, because they're not on the exam. However, if we feel a topic needs some background information, we give it, because we hope to help the reader understand all of the topics that are covered on the exam.
Hope that helps.
- Bert
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posted by Arun Subbu:

Hi Chetan,
i guess you are little bit wrong in your explanation. set and treeset doesnt allow duplicates. but how they implement is different from what you said. if you add string objects, then while adding it uses the string representation to compare whether they are equal or not(to determine duplicates).
But if you add wrapper objects(Integer,Long etc),they use the compareTo method to determine whether they are equal and not the string representation.
just consider this code snippet:
Integer io = new Integer("100");
Long lo = new Long("100");
TreeSet ts = new TreeSet();
ts.add(io);
ts.add(lo);
Iterator it = ts.iterator();
while (it.hasNext()) {
System.out.println("Set Value is : " + it.next());
}

code:
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
this code complies fine, but throws a runtime exception classcastException,since it cant compare an Integer and a Long objects.
Correct me if i am wrong.
Thanks.


Hi Arun:
The reason your code is giving runtime error is not because set do not allow duplicates. Just change TreeSet to HashSet:

This will run fine. TreeSet gives runtime error because it is trying to order its element and it can not compare a Long with an Integer.
Thanks
Barkat
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a detail about what Barkat has said:

TreeSet gives runtime error because it is trying to order its element and it can not compare a Long with an Integer.

TreeSet is using compareTo() to determine order. This is the method which throws the ClassCastException. HashSet only cares about uniqueness and tests with equals(), which, instead of complaining about incompatible objects passed to it, simply returns false (when implemented correctly).
 
Arun Subbu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bakrat Said:
Hi Arun:
The reason your code is giving runtime error is not because set do not allow duplicates. Just change TreeSet to HashSet:

Steve Said:
HashSet only cares about uniqueness and tests with equals(), which, instead of complaining about incompatible objects passed to it, simply returns false (when implemented correctly).

Hi Steve
Thanks for your explanation. Becasue I was so confused looking at Bakrats reply that sets allow duplicates. Your explanation cleared my doubt.Thanks much.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arun,
I am sorry that my explaination mislead you...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic