Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



Get nth element of a Map

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 10-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 10-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
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.
Reply With Quote
  #3  
Old 10-02-2010
Member
 
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.
Reply With Quote
  #4  
Old 10-02-2010
Member
 
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.
Reply With Quote
  #5  
Old 10-02-2010
Member
 
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.
Reply With Quote
  #6  
Old 10-02-2010
Member
 
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());
    }
  }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Get nth element of a Map"
Thread Thread Starter Forum Replies Last Post
Unable to modify the parent container element before the child element is closed Flicker Software Development 5 18-06-2011 08:39 AM
Add Or Remove An Element From Jsp D_chapple Software Development 5 02-03-2010 12:20 PM
How to know whether I am on first element or not in c#? Constantinee Software Development 5 22-02-2010 09:58 PM
Iterate a sub-element Vodka Software Development 5 05-02-2010 12:23 AM
What is an XML Element? Solitario Software Development 4 29-01-2010 06:38 PM


All times are GMT +5.5. The time now is 04:15 AM.