Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search Coderanch
Advance search
Google search
Register / Login
Vlad Jigounov
Greenhorn
+ Follow
1
Posts
0
Threads
0
Cows
since Jul 23, 2010
Merit badge:
grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
Ranch Hand Scavenger Hunt
Number Posts (1/100)
Number Threads Started (0/100)
Number Cows Received (0/5)
Number Likes Received (0/10)
Number Likes Granted (0/20)
Set bumper stickers in profile (0/3)
Report a post to the moderators (0/1)
Edit a wiki page (0/1)
Create a post with an image (0/2)
Greenhorn Scavenger Hunt
First Post
Number Posts (1/10)
Number Threads Started (0/10)
Number Likes Received (0/3)
Number Likes Granted (0/3)
Set bumper stickers in profile (0/1)
Set signature in profile
Search for a post/thread (0/3)
Set a watch on a thread
Save thread as a bookmark
Create a post with an image (0/1)
Recent posts by Vlad Jigounov
How to subtract two arrayLists?
list1.removeAll(list2); will do the job, but it cost you N1*N2 operations (very slow).
If you work with big arrays you can improve performance like this:
public void testSubtract() { List<Long> list1 = new ArrayList<Long>(); list1.add(1L); list1.add(2L); List<Long> list2 = new ArrayList<Long>(); list2.add(2L); list2.add(3L); List<Long> result = subtract(list1, list2); assertEquals(1, result.size()); assertEquals(1L, (long) result.get(0)); } public <T> List<T> subtract(List<T> list1, List<T> list2) { List<T> result = new ArrayList<T>(); Set<T> set2 = new HashSet<T>(list2); for (T t1 : list1) { if( !set2.contains(t1) ) { result.add(t1); } } return result; }
show more
13 years ago
Beginning Java