Vote Up 0 Vote Down

Find keys from both the Linked HashMap and store it in a list alternatively

We would like to know how to find keys from both the Linked HashMap and store it in a list alternatively.

answer


Vote Up 0 Vote Down
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
//  w  w  w.javascrapbook.com/getanswer
public class Main {

  public static void main(String[] args) {
    Map<String, String> m1 = new LinkedHashMap<String, String>();
    m1.put("1", "One");
    m1.put("3", "Three");

    Map<String, String> m2 = new LinkedHashMap<String, String>();
    m2.put("2", "Two");
    m2.put("4", "Four");

    List<String> list = new ArrayList<String>();
    list.addAll(m1.keySet());
    list.addAll(m2.keySet());
    for (String s : list) {
      System.out.println(s);
    }
  }

}
flag | link

your answer

Who Are You?

Have an account already? Login before answering!