I have two ArrayLists that each contain a list of unique String objects.I am trying to count the number of words that are common between both lists,
E.G have one list that contains the words,("house", "cat", "broom", "car", "chair"),
and 2nd list have ,("house", "cat")
i want the output in 3rd arrylist ("broom", "car", "chair"),

do you have sample code
please do the needful.

8 Years
Discussion Span
Last Post by jon.kiparsky

Check the API of the ArrayList class. There is a remove method that you can use.
Or you can use the Vector class that is similar to the ArrayList. Check their APIs to find a method "remove" that is suitable for you.


Sometimes, googling is your best friend. I dont hesitate to give out code, it is just you really should try yourself. My suggestion again, use an IDE like Eclipse and take advantage of the auto-completion, it lists for you the API of a class and what each method does. Here is a sample code I got, try and use it for your own needs:

import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;

public class Repeated {
    public static void main( String  [] args ) {
        Collection listOne = new ArrayList(Arrays.asList("milan","dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));
        Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));

        listOne.retainAll( listTwo );
        System.out.println( listOne );

Edited by WargRider: Better code


My suggestion again, use an IDE like Eclipse and take advantage of the auto-completion, it lists for you the API of a class and what each method does.

There are numerous studies which suggest that babies who are given baby walkers tend to develop their walking, crawling skills much later than the ones deprived of them.
Same way if you are going to use stilts in the form of IDEs while taking your first baby steps in programming, you are bound to go dependent on the stilts for walking,

Masijade's post here makes the point perfectly clear.

It may seem helpful to a beginner, but it's not. IDE's are for people who already know what they're doing in order to increase their effeciency. You need to understand the tools and the processes behind everything and an IDE shields that from you.

Also if the O.P. could not write even a basic outline of code on his own, I doubt he would find his way around in Eclipse. No insult directed at the O.P., this comes from personal experience, learning how to use an IDE effectively is no Joke, so first get the basics of Java and then launch into an IDE. The "Starting Java" thread actually covers the ideal way you should go about learning the language.

Edited by stephen84s: n/a


I've used the same analogy a number of times, Stephen, and I agree with your views on this. vi and a compiler is my development environment of choice.
It's funny, I'm just having a similar debate on an Irish music site - there's a really cool piece of software that identifies a tune on the fly and provides title, key, and a reference to sheet music. Unfortunately, since the ability to learn tunes on the fly is critical to playing trad music, it winds up being a terrible handicap for the user, since they never learn that trick.
Oh, well, I suppose everybody's got the right to go to their own hell in their own handbasket.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.