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
94 views
in Technique[技术] by (71.8m points)

java - find out the elements of an arraylist which is not present in another arraylist

I have to find a best way to find out that elements which is not presented in the second arraylist. suppose

Arraylist a,b, 

Arraylist a={1,2,3,4,5};
Arraylist b={2,3,4};

So basically what I want is to find out that elements of a which is not present in arraylist b.

So what is the best solutions to do that?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
List<Integer> c = new ArrayList<>(a);
c.removeAll(b);

Also consider to use Sets instead of Lists.


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

...