Results 1 to 7 of 7
  1. #1
    LeoKringer is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default A SortedSet iterator into a MAP

    Good night,

    I'm trying to iterate over a SortedSet, but I can not. Below are the classes and etc. ...
    Estou tentando iterar sobre um SortedSet, mas não consigo. Segue abaixo as classes e etc...

    Class DocumentationElement (DocElement)
    Java Code:
    public class DocumentationElement implements Serializable, Comparable {
    	private Integer id;
    	private Field field;
    	private String value;
           
            + gets e sets
    Hibernate
    Java Code:
    <class name="package.DocumentationElement" table="T_DOCELEMENT">
        	<id name="id" type="integer" column="id" unsaved-value="0">
            	<generator class="identity"/>
            </id>
    		<property name="value" column="value" type="string"/>
    		<many-to-one name="field" cascade="none" column="id_field" class="package.Field"/>
    	</class>
    UNTIL HERE, EVERYTHING ALL RIGHT !! I CAN INSERT INTO DATABASE PERFECTLY AND RECOVER THEM!!

    Servlet
    Java Code:
    //Take the component. There is a set of documentationElement to each component
    Collection<Component> components = dao.findComponents(user);
    			SortedSet<DocumentationElement> docele = null;
    
    			Map<Integer,SortedSet<DocumentationElement>> aux = new HashMap<Integer,SortedSet<DocumentationElement>>();
    			SortedSet<DocumentationElement> loop = null;
    			
    			for (Component component : components)
    			{
                                    //Take a list of DocElement from the current component
    				docele = component.getDocElements();
                                    //Put the id component and the list of DocElement into the MAP
    				aux.put(component.getId(), docele);
    			}
    
    			// MAP Iterator
    			for (Integer key : aux.keySet()) 
    			{
    				System.out.println("Key " + key);
    				//Take the DocElement from the  MAP
    				loop = aux.get(key);
    				Iterator it = loop.iterator();
    				while (it.hasNext())
    				{
                                           //THE PROBLEM IS HERE! I NEED TO GET THE PROPERTY "VALUE" OF DocElement BUT I CAN NOT!!
    					
    					it.next();
    					//break;
    				}
    			}
    Please, help me !!!
    Thanks for any help...

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,942
    Rep Power
    4

    Default Re: A SortedSet iterator into a MAP

    How do you know it isn't working? When you use an iterator, the next() method returns a value. But you just call it.next() without assigning the returned value to a variable.

    Regards,
    Jim
    Last edited by jim829; 09-23-2013 at 02:31 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: A SortedSet iterator into a MAP

    Java Code:
    Iterator<DocumentationElement> it = loop.iterator();
    And this will save you having to cast to DocumentationElement.
    Please do not ask for code as refusal often offends.

  4. #4
    LeoKringer is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: A SortedSet iterator into a MAP

    Quote Originally Posted by jim829 View Post
    How do you know it isn't working? When you use an iterator, the next() method returns a value. But you just call it.next() without assigning the returned value to a variable.

    Regards,
    Jim
    When I wrote " System.out.printf(it.next()); " the console printed "DocumentationElement@1745afhga75"

    And I didn't have this option: " it.next().getValue(); "

  5. #5
    LeoKringer is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: A SortedSet iterator into a MAP

    Quote Originally Posted by Tolls View Post
    Java Code:
    Iterator<DocumentationElement> it = loop.iterator();
    And this will save you having to cast to DocumentationElement.
    I'll try it... Thanks

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: A SortedSet iterator into a MAP

    jim is talking about assinging what is returned by next() to a local variable in the same way you assigned the value returned by getDocElements() to the docele variable.
    Please do not ask for code as refusal often offends.

  7. #7
    LeoKringer is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: A SortedSet iterator into a MAP

    Quote Originally Posted by Tolls View Post
    jim is talking about assinging what is returned by next() to a local variable in the same way you assigned the value returned by getDocElements() to the docele variable.
    Thank you guys... I've got the solution!

    You can close this topic, please...

Similar Threads

  1. SortedSet and my own Integer comparator
    By trixter in forum New To Java
    Replies: 2
    Last Post: 03-20-2011, 01:23 PM
  2. SortedSet Interface
    By Lil_Aziz1 in forum New To Java
    Replies: 14
    Last Post: 08-03-2010, 03:15 PM
  3. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  4. SortedSet and Collections.binarySearch
    By ninoid in forum New To Java
    Replies: 4
    Last Post: 03-22-2010, 03:28 PM
  5. Use different comparator for SortedSet
    By linus_k in forum New To Java
    Replies: 0
    Last Post: 11-21-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •