0

I am creating dynamic tabs.generateTab() method help to set tabs to be shown or not. Like Property to be shown, Info not to be shown, etc. But I am getting stuck in some point. I want to display Property tab. I am passing it like this:

String nextTab = getNextTab("Property");

Then it will check in getNextTab method if Property is true or not. If it is false then the method has to return the next true value.

public LinkedHashMap generateTab() {
    LinkedHashMap selectedTabs = new LinkedHashMap();
    selectedTabs.put("People", true);
    selectedTabs.put("Property", true);
    selectedTabs.put("Info", false);
    selectedTabs.put("Fixture", true);
    selectedTabs.put("Fee", true);
    selectedTabs.put("Process", true);
}

public String getNextTab(String currentTab) {

    String nextTab = null;
    Map<String, Boolean> tabList = generateTab();
    Iterator iterator = tabList.keySet().iterator();

    while (iterator.hasNext()) {
        Map.Entry entry = (Map.Entry) iterator.next();
        if (entry.getKey().equals(currentTab)) {
            if (tabList.get(entry.getKey()).equals(true)) {
                nextTab = entry.getKey().toString();
                break;
            }
        }
    }
    return nextTab;
}

can you help me to get next key which i am checked in else part?

Brian
  • 17,079
  • 6
  • 43
  • 66
psisodia
  • 1,117
  • 5
  • 17
  • 36
  • What problem are you facing?? A code should either be working or should have a problem.. You need to give us either of them.. (Preferably the 2nd one) – Rohit Jain Oct 05 '12 at 06:10
  • 1
    The code I see now returns the same tab, because after you find current tab, you return it. – popfalushi Oct 05 '12 at 06:11

4 Answers4

1

There seems to be multiple problems with your code

1)public LinkedHashMap generateTab() : It should ideally return a LinkedHashMap. (I am assuming its a copy paste error)

2)iterator.next() : This should be returning a ClassCastException for casting String to Map.Entry

3)To return nextTab, you must call iterator.next() once to move the iterator to next item.

Change your getNextTab() function to look something like below.

public static String getNextTab(String currentTab) {

    String nextTab = null;
    Map<String,Boolean> tabList = generateTab();
    Iterator iterator = tabList.keySet().iterator();

    while (iterator.hasNext() ){
        String entry =(String) iterator.next();

         if(entry.equals(currentTab))
         {
             if(tabList.get(entry).equals(true))
             {

                 nextTab = entry;
                 break;
             } 
             else
             {
                nextTab =(String) iterator.next();
                break;
             }

         }

     }
     return nextTab;
}
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
1

Raw Types

Okay, your first problem here is that you use a raw LinkedHashMap, raw Iterator, and raw Map.Entry. This will lead to all sorts runtime errors, especially in your code. If you had started by typing everything with the <>, the compiler would have given you errors that would help you solve a couple problems.

First, let's change that LinkedHashMap to be typed:

public LinkedHashMap<String, Boolean> generateTab() {
    LinkedHashMap<String, Boolean> selectedTabs = new LinkedHashMap<String, Boolean>();
    selectedTabs.put("People", true);
    selectedTabs.put("Property", true);
    selectedTabs.put("Info", false);
    selectedTabs.put("Fixture", true);
    selectedTabs.put("Fee", true);
    selectedTabs.put("Process", true);
    return selectedTabs;
}

Next, we'll add it to the Iterator. From your code, it's clear that you're expecting a Map.Entry from the Iterator, so let's start with that.

Iterator<Map.Entry> iterator = tabList.keySet().iterator();

When you enter this into your code, you're going to get an error:

Type mismatch: cannot convert from Iterator<String> to Iterator<Map.Entry>

This means that your iterator is not typed on Map.Entry like you thought it was. This is because keySet returns a Set<K> from the LinkedHashMap, not Set<Entry<K, V>> like you thought. What you want is tabList.entrySet().iterator(). However, if you enter this, it will tell you:

Type mismatch: cannot convert from Iterator<Map.Entry<String,Boolean>> to Iterator<Map.Entry>

So we have to add the <String, Boolean> to the Map.Entry as well:

Iterator<Map.Entry<String, Boolean>> iterator = tabList.entrySet().iterator();

A couple other little things. Now that we've changed this, we can a) replace your getKey().toString() call with just getKey() (since it's a string now) and b) replace your tabList.get(entry.getKey()).equals(true) with just entry.getValue() (since it's a boolean now).

There, now that we've sorted that out, your code will look like this (before addressing your problem):

public LinkedHashMap<String, Boolean> generateTab() {
    LinkedHashMap<String, Boolean> selectedTabs = new LinkedHashMap<String, Boolean>();
    selectedTabs.put("People", true);
    selectedTabs.put("Property", true);
    selectedTabs.put("Info", false);
    selectedTabs.put("Fixture", true);
    selectedTabs.put("Fee", true);
    selectedTabs.put("Process", true);
    return selectedTabs;
}

public String getNextTab(String currentTab) {
    String nextTab = null;
    Map<String, Boolean> tabList = generateTab();
    Iterator<Map.Entry<String, Boolean>> iterator = tabList.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, Boolean> entry = iterator.next();
        if (entry.getKey().equals(currentTab)) {
            if (entry.getValue()) {
                nextTab = entry.getKey();
                break;
            }
        }
    }
    return nextTab;
}

Getting the Next Tab

So if I understand correctly, you want to find the current tab, then return the tab after that in the map, correct?

Edit: I see now, you want the next one whose hide/show value is true.

To do this, you already have the iterator, so you can just use that. If you're on the current tab:

  1. Check if there's something left in the iterator
  2. If there is, get the next value in the iterator (the next tab, as it were)
  3. If there isn't, refresh the iterator get the first value (we've reached the end, so loop back around to the beginning)
  4. Is it visible? If so, return it, otherwise, continue.

This will return the first tab that is visible after the currently selected one. If the tab only visible tab is the one that's selected, this will return that tab again. Be careful about calling this if no tabs are visible. Make sure this won't happen since it will create an infinite loop if all tabs are hidden.

This would change your loop to look like this:

while (iterator.hasNext()) {
    Map.Entry<String, Boolean> entry = iterator.next();
    if (entry.getKey().equals(currentTab)) {
        if (entry.getValue()) {
            while (true) {
                if (iterator.hasNext()) {
                    entry = iterator.next();
                } else {
                    // Get a fresh iterator and get its first element
                    iterator = tabList.iterator();
                    entry = iterator.next();
                }
                if (entry.getValue()) {
                    return entry.getKey();
                }
            }
        }
    }
}

And there you go! Note that I had to use return instead of break, since breaking out of two loops requires either a) a boolean variable to make the first loop exit after the second one exits, or b) a label for the break statement to break to, neither of which are good options, really.

Brian
  • 17,079
  • 6
  • 43
  • 66
0

If removed your unnecessary logic the code looks like below

public String getNextTab(String currentTab) {
    Map<String, Boolean> tabList = generateTab();
    boolean shouldStart = false;
    for (Map.Entry<String, Boolean> value : tabList.entrySet()) {
        if (value.getKey().equals(currentTab)) {
            shouldStart = true;//to check if we have found entry7
            continue;
        }
        if (shouldStart) {
            if (value.getValue().equals(true))
                return value.getKey();//return the key which has next value true
        }
    }
    return null;
}

Output

getNextTab("Property")-->Fixture
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • My current tab is People if it is true it will give me People tab but I want to get next key.its not unnecessarily – psisodia Oct 05 '12 at 06:25
  • @psisodia Logic needed some minor fixes please check answer. – Amit Deshpande Oct 05 '12 at 06:32
  • I dont want null value I want only next Tab value.Like when i run your code I get output - People Property null Fixture Fee Process BUt according to me,my output will be People Property Fixture Fee Process If it is null it will give me next tab that is Fixture. – psisodia Oct 05 '12 at 06:34
  • @psisodia Yes As said in earlier comment logic needed some minor fixes now it works properly – Amit Deshpande Oct 05 '12 at 06:36
0

Try this..

for(Map.Entry<String,Boolean> arr : tabList.entrySet()){

     if(arr.getKey().equals("Property")){


             if(arr.getValue()){

                 // Do something is Property is True

            }else{

                 // Do something is Property is False

            }


}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • My current tab is property .If it is true it will give me Property tab.but if it is false then I want to get next one tab which is Info – psisodia Oct 05 '12 at 06:25