Results 1 to 6 of 6

Thread: Accessing the element of a set

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Accessing the element of a set

    Hello!
    Here is my question, I think the answer is very short, perhaps because it self-evident and my source is not very fresh this morning. How do I access the element of a set,
    this is what I want from you guys
    Code:
    Set set = new HashSet ();
    That is any explanation on this will be highly appreciated. Thank you in advance friends.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Accessing the element of a set

    Hello,
    A set is an unordered collection therefore you can not access a particular element directly cons you can retrieve an Iterator over and browse the entire collection, but the javadoc better that you tell me what you can do on a HashSet. I strongly recommend you to read the set on the sun's official site. First do not start coding, but have a look at the documentation, understand it and then implement the coding part.

  3. #3
    Join Date
    Nov 2009
    Posts
    335

    Re: Accessing the element of a set

    Hello,
    Well iterate on the set is one method:
    Code:
    Iterator = it set.iterator();
    while (it.hasNext())
    {
       Object e = it.next();
       //...
    }
    Just try this method in your code and see what is the output you get. If any help needed then please do post back and we will try to find a solution for it.

  4. #4
    Join Date
    Dec 2009
    Posts
    213

    Re: Accessing the element of a set

    Hello,
    Ok thank you, that's why I do not see a line of code explicit. I started by making an iterator and I had changed his mind, in case it might be easier, but too bad ... I'll use this method. Any other information on this topic is interesting and I am read for it, so, if you have an more knowledge on this topic then please do post back. Thank you very much.

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Accessing the element of a set

    Hello,
    It is better to declare elem outside the loop, to reuse the same memory space each loop rather than doing unnecessary work
    Code:
    Iterator = it set.iterator();
    while (it.hasNext())
    {
       Object e = it.next();
       //...
    }
    with a for:
    Code:
    Object elem;
    for (Iterator i = set.iterator()I.hasNext(); Elem = it.next();)
    {
       //...
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    359

    Re: Accessing the element of a set

    Hello,
    For your information and addition to this topic, since java 5
    Code:
    for (object elem: set)
     {/ / processing on elem)
    You can use the new for loop and compile your code. this is very easy and handy to use. And even your code remain clean and tidy.

Similar Threads

  1. Replies: 1
    Last Post: 10-05-2012, 11:55 AM
  2. Replies: 5
    Last Post: 18-06-2011, 07:39 AM
  3. How to know whether I am on first element or not in c#?
    By Constantinee in forum Software Development
    Replies: 5
    Last Post: 22-02-2010, 09:58 PM
  4. Get nth element of a Map
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 12:25 AM
  5. What is an XML Element?
    By Solitario in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 06:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,843,854.64821 seconds with 16 queries