Iterating with Set.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #1

    Iterating with Set.

    As list can return the elements in which order they get stored.
    But in case of Set if I store the elements in an order the returning elements do not maintain the same order.

    [code=text]Actually here is my properties file.
    like this.....
    abc=123
    debasis=345
    hello=hello world


    debasis=i luv movie
    ..
    .
    .
    .[/code]

    Now what i am doing ....
    [code=java]
    Properties props = new Properties();
    props.load("c:/xxx.properties" );
    Set keys = props.keySet();
    [/code]

    Now i want the iterate the keys in which order they got stored.
    How?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Why are you using a Set if you don't want to sort the elements?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by r035198x
      Why are you using a Set if you don't want to sort the elements?
      No no.... I am not going to sort them.
      Suppose in my properties file ...
      a=b
      c=d
      1=2

      I want to get the keys as a,c&2 ..in this order i want to get.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dmjpro
        No no.... I am not going to sort them.
        Suppose in my properties file ...
        a=b
        c=d
        1=2

        I want to get the keys as a,c&2 ..in this order i want to get.
        You should type your messages a bit more accurately; I assume you meant the
        order a, c and 1. As you could've read from the API documentation a Properties
        class extends a simple Hashtable and a thing like that doesn't preserve any
        insertion order. A LinkedHashMap does so but a Properties class doesn't
        extend from that class. Why do you always want impossible things you don't
        use anyway? Either use a LinkedHashMap and do the reading from a file yourself
        or use a Properties object and let go of the insertion order.

        kind regards,

        Jos

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by JosAH
          You should type your messages a bit more accurately; I assume you meant the
          order a, c and 1. As you could've read from the API documentation a Properties
          class extends a simple Hashtable and a thing like that doesn't preserve any
          insertion order. A LinkedHashMap does so but a Properties class doesn't
          extend from that class. Why do you always want impossible things you don't
          use anyway? Either use a LinkedHashMap and do the reading from a file yourself
          or use a Properties object and let go of the insertion order.

          kind regards,

          Jos
          What i thought that a problem with keySet method,So the story of Properties missed ... :-)
          Anyway thanks ..... And i like impossible things but i use it ...
          Basically what we are doing that web service creation and deployment ..
          so for datasource we are using a properties file there we are checking any datasource duplicacy that's why we loaded the that into Properties context.
          Anyway thanks for your help ...!!!!

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dmjpro
            What i thought that a problem with keySet method,So the story of Properties missed ... :-)
            Anyway thanks ..... And i like impossible things but i use it ...
            Basically what we are doing that web service creation and deployment ..
            so for datasource we are using a properties file there we are checking any datasource duplicacy that's why we loaded the that into Properties context.
            Anyway thanks for your help ...!!!!
            As you could've read from the documentation a Properties class extends a
            Hashtable set which implements the Map interface. That interface allows you
            to 'retain' or 'remove' all elements also present in another Map through their
            respective EntrySets. There is no need to traverse these things in any order.

            kind regards,

            Jos

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by JosAH
              As you could've read from the documentation a Properties class extends a
              Hashtable set which implements the Map interface. That interface allows you
              to 'retain' or 'remove' all elements also present in another Map through their
              respective EntrySets. There is no need to traverse these things in any order.

              kind regards,

              Jos
              Ok ! I got your point ....Now I am trying another thing then if any problem is there then i ll come here :-)

              Comment

              Working...