• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Question on Collections

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody :
I have a code here.

I have added a method called AddingThings ..... which gets the object to be added and checks whether it can be added to the collection. Since this is a set interface, if the object is already in the collection, it will return a false. I am not sure if that is the way to do it.
Can you help me out here?
Thank you very much
Pallavi
Edited by Corey McGlone: Added Code tags
[ March 18, 2003: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pallavi,
I could see errors in your code.
Look at this code.
import java.util.collections.*;
Do we have a package collections in util? Please check.
collection c = new collection;
Can you instantiate an interface?? Please check.
Please compile your code before posting. Note that java compiler is your first and best teacher.
 
Pallavi Chakraborty
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sarma,
Thank you for your reply. I am relatively new to Java and collections so got confused.
Actually I had picked up the code from a website
which I am not able to find now, and I tried to compile it on JCreator. It gave errors saying that it could not locate java.util.collections. I thought at the time that I have something missing.
However, even without the code my question regarding the AddThings method which I added remains which is:
To add elements to a set class, be it Hashset or Treeset, is it good to use the add methos to verify whether the element is already in the collection ?
Thank you very much
Pallavi
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pallavi,


To add elements to a set class, be it Hashset or Treeset, is it good to use the add methos to verify whether the element is already in the collection ?


Yes , you can safely use the 'add()' method of implementation classes of "Set" interface like Hashset or Treeset, to check if the element is already in the collection. Here is the method signature of add().


public boolean add(Object o)
Parameters:
o - element to be added to this set.
Returns:
true if this set did not already contain the specified element

 
Pallavi Chakraborty
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lakshmi,
Thank you very much for the confirmation.
Pallavi
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pallavi,
My point is - first try to understand the code. Try to compile the code. I prefer using simple textpad and some times just jdk1.4 command line. No GUI based tools.
Look at the code you posted.

{if(add(object do) returns false} /QUOTE]
add method takes an object. But you wouldn't call add(Object do) but you call add(do). It seems the code you picked up is psedo code.

 
Our first order of business must be this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic