collections - How can Boolean/boolean serve as the key of a HashMap in Java? -


i have interview question asks if boolean can serve key of hashmap in java. wasn't sure how possible, , explanation.

it unclear if boolean or boolean meant in question. perhaps both should handled in answer.

that's great (and pretty funny!) question. you're going have 2 items, can mash multiple items each value. example:

    import  java.util.arraylist;     import  java.util.arrays;     import  java.util.list;     import  java.util.map;     import  java.util.treemap; /**    <p>{@code java booleankeyedmapxmpl}</p>  **/ public class booleankeyedmapxmpl  {    public static final void main(string[] igno_red)  {          system.out.println("<boolean,string>:");             map<boolean,string> mbs = new treemap<boolean,string>();              mbs.put(true, "hello");              mbs.put(false, "goodbye");              system.out.println("true: " + mbs.get(true));             system.out.println("false: " + mbs.get(false));             system.out.println();          system.out.println("<boolean,arraylist<string>>:");             map<boolean,list<string>> mbls = new treemap<boolean,list<string>>();              mbls.put(true, new arraylist<string>());             mbls.put(false, new arraylist<string>());              list<string> lstrue = mbls.get(true);                 lstrue.add("hello1");                 lstrue.add("hello2");                 lstrue.add("hello3");                 lstrue.add("hello4");                 lstrue.add("hello5");              list<string> lsfalse = mbls.get(false);                 lsfalse.add("goodbye1");                 lsfalse.add("goodbye2");                 lsfalse.add("goodbye3");                 lsfalse.add("goodbye4");                 lsfalse.add("goodbye5");              system.out.println("true: " + arrays.deeptostring(lstrue.toarray()));             system.out.println("false: " + arrays.deeptostring(lsfalse.toarray()));    } } 

output:

[c:\java_code\]java booleankeyedmapxmpl <boolean,string>: true: hello false: goodbye  <boolean,arraylist<string>>: true: [hello1, hello2, hello3, hello4, hello5] false: [goodbye1, goodbye2, goodbye3, goodbye4, goodbye5] 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -