Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

List<String> list = new ArrayList<String>();
  list.add("One");
  list.add(null);
  list.add("Two!");
  list.add(null);
  list.add("Three");
  list.add(null);
  list.add("Four");
  list.add(null);

I have a list containing null elements. Is there any way to remove the null elements from the current collection without using any iterations?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
168 views
Welcome To Ask or Share your Answers For Others

1 Answer

This should work:

list.removeAll(Collections.singleton(null));  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...