Results 1 to 6 of 6

Thread: Get nth element of a Map

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Get nth element of a Map

    Hello,
    Here is my map
    Code:
     Map <String, Map <String,Integer>> fl2 = new HashMap <String, Map <String,Integer>>();
    I would like for example after finding the first branch, to enumerate all the items related to this sector as a String. I've wanted to do some work by retrieving the size of the map, but after it no longer works with get. Here is my code
    Code:
      else if (fl2.containsKey (dp) & &! fl2.get (dp). containsKey (ar))
        (
            / / one of them to start
            this.tf_answer.setText ( "this flight is not direct");
            int msz = fl2.get (dp). size ();
            for (int i = 0; i <msz; i + +)
            (
                System.out.println ( "fl2.get (dp). Get (i). ToS:" + fl2.get (dp). get (i). toString ());
            }
    But it does not work, I can 't find the method. Basically i want to find the nth element of the Map. Any help in this.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Get nth element of a Map

    Hello,
    Elements of a Map did not order, then the "nth element that has no meaning.
    By cons you can iterate over the contents of the Map. Here is the code you can refer to.
    Code:
    for (Integer item: fl2.get(dp).values()) {
      / / Do something with item qq
    }
    If you need any help in this then please do post back.

  3. #3
    Join Date
    Dec 2009
    Posts
    296

    Re: Get nth element of a Map

    Hello,
    Your last technique allows me to juggle it does element by element? I will have then needed to test all the elements he presents are all the same to this this. I will try it in my code , I have any idea for it that is how to use it in the code. Until then, if you have any alternative method for doing the same task then please do post back with that, I am interested to work on it.

  4. #4
    Join Date
    Dec 2009
    Posts
    292

    Re: Get nth element of a Map

    Hello,
    I am trying the same kind of a code, but this does not work, even the getItem returns null. Here is my code, please verify it.
    Code:
    for (int Item: fl2.get(dp).values()) {
    System.out.System.out.println("Test"+ fl2.get(dp).get(Item) );
    System.out.System.out.println("Key"+ Item ); / / IT WORKS j have a key
    System.out.System.out.println("Key values"+ fl2.get(ar).get(Item).function toString() {
        [native code]
    }() ); / / But in fact the same getItem returns null
            }
    Any help on this is highly appreciated. Thanks in advance.

  5. #5
    Join Date
    Nov 2009
    Posts
    335

    Re: Get nth element of a Map

    Hello,
    In Your Map, item is a value (see. Values ()), no key. If you want to iterate over the keys, what i am trying to say is this
    Code:
    for (String key: fl2.get(dp).keySet()) {
      System.out.System.out.println(key);
      System.out.System.out.println(fl2.get(dp).get(key));
    }
    Just try this code in your program and then see if your problem is sorted. if not then you can post back with the error.

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Get nth element of a Map

    Hello,
    I am new to this concept but I have code with me, just have a look at it, it may help you. Here is the code
    Code:
    public class MainClass {
      public static void main(String[] a) {
    
        Map m = new HashMap();
        m.put("key1", "value1");
        m.put("key2", "value2");
        m.put("key3", "value3");
        Set set = m.entrySet();
    
        Iterator ir = set.irator();
    
        while (ir.hasNext()) {
          Map.Entry entry = (Map.Entry) ir.next();
          System.out.println(entry.getKey() + " -- " + entry.getValue());
        }
      }
    }

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. Iterate a sub-element
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 12:23 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,713,551,760.50305 seconds with 17 queries