0

Okay, I'm SURE there is a better way to do this. What I have is a nested HashMap which contains any number of K,V pairs as imported from an XML file. However for each Key in the parent HashMap, there is only 1 child K,V pair.

I'm wondering if there is a "better" way to code this than what I have.

HashMap<String, HashMap<String,String>> mQueues = Global.mQueues;
for (Map.Entry<String, HashMap<String, String>> eQueues: mQueues.entrySet()) {
    // There is only 1 K,V for each key iterated above
    // This is where I am wondering if there is a better way to do this.  e.g. directly access the Key and Value of the subordinate HashMap
    for (Map.Entry<String, String> sQueue : eQueues.getValue().entrySet()) {
        strGroup = sQueue.getKey();
        strPattern = sQueue.getValue();
    }
    if (eQueues.getKey() == "Default") {
        strDefGroup = strGroup;
    } else {
        if (strParts[0] == strPattern) {
            msg_grp = strGroup;
            boolPatternMatch = true;
        }
    }
Beeker
  • 383
  • 4
  • 20
  • 3
    Don't use `==` to compare `String`s. – Elliott Frisch Jun 01 '17 at 23:01
  • What would you suggest for comparing Strings? I'm not native to Java so please pardon my ignorance. – Beeker Jun 01 '17 at 23:07
  • Instead of having a HashMap> which is quite heavy, try implementing a HashMap, String> – BladeCoder Jun 01 '17 at 23:18
  • @Aaron K. Baxter, it's a good idea when learning Java to work through the official Tutorial. It has a chapter on comparing `String` instances, http://docs.oracle.com/javase/tutorial/java/data/comparestrings.html . Read the Fine Manual. – Lew Bloch Jun 01 '17 at 23:46
  • Indeed. On my insanely long list of things to do. Sadly, everything with my job is a rush and we often don't have time to learn the correct way. :( What's even sadder you might ask? I'm the only one with any coding experience.... I do appropriate all of the help though! – Beeker Jun 02 '17 at 02:48

1 Answers1

0

Well, you could do this:

Map<String, String> m = eQueues.getValue();
if (!m.isEmpty()) {
    Map.Entry<String, String> e = m.entrySet().iterator().next();
}

That's not really any better than what you're doing, though. It seems like instead of a Map<String, Map<String, String>> you should have a Map<String, Map.Entry<String, String>>, or even just create a small object to describe whatever it is that's in your single entry:

public class GroupPattern {
    private String group;
    private String pattern;
    public GroupPattern(String group, String pattern) {
        this.group   = group;
        this.pattern = pattern;
    }
    public String getGroup() {
        return group;
    }
    public String getPattern() {
        return pattern;
    }
    // Optionally setters, if it makes sense.
    public void setGroup(String group) {
        this.group = group;
    }
    public void setPattern(String pattern) {
        this.pattern = pattern;
    }
}

Then you have a Map<String, GroupPattern>.


Also, what Elliott is saying in the comments is right. See e.g. How do I compare strings in Java?

Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • Okay, so to make sure I have this "Java Correct" `public class GroupPattern { String strGroup; String strPattern; } GroupPattern gp = new GroupPattern(); gp.strGroup = eQueuePattern.getAttribute("message_group"); gp.strPattern = eQueuePattern.getAttribute("pattern"); mQueues.put(eleName, gp);` – Beeker Jun 02 '17 at 21:07
  • Well now it **functionally** works but the question remains is the above code "correct" for Java conventions which only the great powers that be in this forum can answer :) – Beeker Jun 02 '17 at 22:47
  • Ah, see my edit for the correct Java conventions. I was away all day today, sorry. The Java convention is to have private fields with setters and getters. – Radiodef Jun 03 '17 at 05:36
  • no problems man. I appreciate the help. I see where using public variables could cause issues. I've also switched to using .equals() and the equivalent for string comparison. So, with the above, could I do a `mQueues.put(eleName, new GroupPattern(eQueuePattern.getAttribute("message_group"), eQueuePattern.getAttribute("pattern"))` or would that be sinful or not work? – Beeker Jun 06 '17 at 16:25