2

What is the easiest way to add the keys of a LinkedHashMap to an ArrayList? I would like to increase performance by removing loops. Is there a more efficient way?

for(String key : map.keySet()){
 array.add(key);
}
LOG_TAG
20.7k12 gold badges75 silver badges105 bronze badges
asked Jan 9, 2014 at 9:43
4
  • 2
    Why not array.addAll()? Commented Jan 9, 2014 at 9:44
  • FYI: You are not necessarily improving performance with shorter code, especially when it comes to for loops, the compiler now a days is very good at optimizing those for you. Commented Jan 9, 2014 at 9:48
  • @ErnirErlingsson If for example the method addAll contains the same loop but has null checks, it is slower?? Commented Jan 9, 2014 at 9:50
  • 1
    addAll() will be faster or the same, but should not be slower although you never know what the compiler does to your code without directly checking for it. My point is just that shorter code does not always mean faster code as I felt you were implying with your question. Commented Jan 9, 2014 at 9:54

1 Answer 1

13
List array = new ArrayList(map.keySet());
answered Jan 9, 2014 at 9:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.