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.

Results 1 to 20 of 20

Thread: How would i remove overlapping lists

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How would i remove overlapping lists

    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.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How would i remove overlapping lists

    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).

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would i remove overlapping lists

    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would i remove overlapping lists

    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.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would i remove overlapping lists

    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
    Quote Originally Posted by Norm View Post
    ArrayList<TestCode5> anAL = new ArrayList<TestCode5>();
    ...
    System.out.println("anAL=" + anAL);
    // anAL=[TestCode5@3e25a5, TestCode5@19821f, TestCode5@addbf1]
    whats testcod5? I don't really know what that is.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would i remove overlapping lists

    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.

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would i remove overlapping lists

    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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would i remove overlapping lists

    I'm not allowed to show you because it contains emails address
    Make a test case that you can post.

    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.

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would i remove overlapping lists


  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would i remove overlapping lists

    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?

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would i remove overlapping lists

    Im calling it from another java, its on a public class.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would i remove overlapping lists

    Im calling it from another java, its on a public class.
    Please explain. I can not understand what you said here.
    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?

  13. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would i remove overlapping lists

    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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would i remove overlapping lists

    What is the name of the class for the items being stored in the arraylist?

  15. #15
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would i remove overlapping lists

    its a string

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would i remove overlapping lists

    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?

  17. #17
    Junior Member
    Join Date
    Jul 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would i remove overlapping lists

    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!!!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would i remove overlapping lists

    What happened in post#3? That was a confusion.

  19. #19
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How would i remove overlapping lists

    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.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would i remove overlapping lists

    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 <

Similar Threads

  1. Overlapping multiple periods
    By Atei in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 3rd, 2011, 11:35 AM
  2. [SOLVED] Linked Lists
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 08:39 AM
  3. Array Lists help!!
    By lilika in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 4th, 2011, 09:21 AM
  4. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM
  5. Overlapping windows? in a Jframe?
    By chronoz13 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 21st, 2009, 12:01 PM