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

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

java - Remove null elements from list

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

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

1 Answer

0 votes
by (71.8m points)

This should work:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...