I was wondering how i would remove emails that appear on 2 arrayLists. I tried to figure it out by myself but i dont have a lot of experience in java.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I was wondering how i would remove emails that appear on 2 arrayLists. I tried to figure it out by myself but i dont have a lot of experience in java.
If you mean you want to remove messages from one list that are duplicated in the other, you can call removeAll(..) on one list, passing the other as the argument: list1.removeAll(list2); This will remove from list1 any items that are also in list2.
If you want to merge the two, removing items duplicated between the lists, follow removeAll(list2) with addAll(list2). This will add all list2 items to list1.
If you want to merge the lists, removing all duplicates, create a new Set (e.g. TreeSet) and add both lists by calling set.addAll(list1) followed by set.addAll(list2).
Hi dlorde, what I want to do is the first one you mentioned. I tried doing it but list1 is still the same after I put "list1.removeAll(list2);" . When i counted the size it was still the same as before.
Are the objects in the two lists actually the same exact objects or are they different objects with the same content?
The two array lists contain references to the objects they contain.
Are the same objects (the duplicates) referred to from the two list?
Print out the two array lists and see if the output has the same objects in it. This won't work if the objecs in the list have toString() methods.
ArrayList<TestCode5> anAL = new ArrayList<TestCode5>();
...
System.out.println("anAL=" + anAL);
// anAL=[TestCode5@3e25a5, TestCode5@19821f, TestCode5@addbf1]
Last edited by Norm; July 20th, 2011 at 11:22 AM.
Hi Norm, Im kinda confused about the first part of what you said. Okay my 2 arrayList are the objects correct, and there is some same content from arraylist 1 and arraylist 2. List 1 has few content from arraylist2. What im aiming for is that In arraylist1, I remove the ones that appear in arraylist2.
the second part of what you said about printing out the array lists, there are some same objects/content in. I'm pretty sure that im not using toString(). Im not entirely clear on what that is. But if it has to do with making the arrayLists, I first used bufferedReader to bring in a text file1, then used stringTokenizer and contains, to just get out the emails then add file1 emails to arraylist1, then repeated that for file2 and to make arraylist2.
for the last part
whats testcod5? I don't really know what that is.
Sorry the type of the arraylist was from my class: TestCode5. It was just an example.
Can you print out the arraylists using a println similar to the one I posted. You should get a printed result similar to the one I posted (classname followed by @ and hex numbers) with your class names substituted for mine.
The name@hexNumber will uniquely identify each object in each arraylist.
I doubt if there are any the same between the two array list print outs.
We'll know when you post the print outs.
Last edited by Norm; July 20th, 2011 at 12:53 PM.
Okay I printed them out and they looks similar like yours. Its like you said but with my class names. Im not sure if you wanted me to print out the results, but I didn't think I'm allowed to show you because it contains emails address for people.
so what should I do after that?
Make a test case that you can post.I'm not allowed to show you because it contains emails address
My point is that the objects in the two lists are DIFFERENT objects with the same contents.
To remove ones with the same content, you can either use a nested loop to read one from the first list and then search the other list for one with the same content
Or change the equals() method for the class so that two messages with the same content are considered equal. There can be side effects doing this depending on what types of collections you want to put the class in.
this is my test case results I made.
myList =[testemail1@test.com, testemail2@test.com, testemail3@test.com, testemail4@test.com, testemail5@test.com, testemail6@test.com]
myList2 =[testemail3@test.com, testemail4@test.com, testemail5@test.com, testemail6@test.com, testemail7@test.com, testemail8@test.com, testemail9@test.com]
Your class appears to have the toString() method. The print out shows the contents of the class.
Compare with the printout of my class: TestCode5@19821f
with yours: testemail7@test.com
Mine shows the name of the class followed by @ followed by a hex address.
Your shows the contents of the object.
What class are you storing in the array lists?
Im calling it from another java, its on a public class.
Please explain. I can not understand what you said here.Im calling it from another java, its on a public class.
I asked about the definition of the class whose objects are stored in the array list.
What is the name of the class?
Does it have a toString() method?
Does it have an equals() method?
no it does not have a toString() method, or an equals() method. The objects stored in the array list is from stringTokenizer.
Last edited by redzero36; July 20th, 2011 at 03:43 PM.
What is the name of the class for the items being stored in the arraylist?
its a string
Then removeAll() should work.
Can you make a small, simple test program with two array lists and use the removeAll method that demonstrates your problem?
this is the results I did. cool thanks its working
myList =[testemail1@test.com, testemail2@test.com] It removed testmails3,4,5,6.
AWESOME thanks man!!!
What happened in post#3? That was a confusion.
Yes, like Norm says, if they are Strings, it should have worked from the start...
The point about these methods is that they need a way to decide if the two objects are the same or not. To do this, the equals(..) method of one of the objects is called, passing the other as an argument. The equals method returns true or false according to whether it considers that the objects are 'the same' or not. By default, the equals method checks that it is comparing an object with itself and returns true if that is the case. If you want two different objects that have the same contents to compare equal, you have to override the equals method so it compares the contents of the two objects and returns true if the contents are the same.
The String class does override the equals method to compare the contents.
Last edited by dlorde; July 21st, 2011 at 02:42 PM.
Are All the objects put in the array lists: Strings?
How do you verify that the same items are in both lists? Visually or via program code?
If visually are you sure there are not leading or trailing spaces or other unprintable characters that can make the strings different.
Write a test loop to go thru the lists and print out the String with leading and trailing delimiters:
System.out.println(">" + theString + "<");
Then look at the what is printed to see if there are any spaces by the > and <