This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Iterate over two ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterate over two ArrayLists" Watch "Iterate over two ArrayLists" New topic
Author

Iterate over two ArrayLists

Alexander Volkov
Greenhorn

Joined: Sep 01, 2012
Posts: 8
Hello Guys

Straight to the question. What would be the proper way to iterate over two araysLists and perform equals check against each element from one list to another.
Build in methods work fine add


Found out that loop with in the loop it's like testing against the last element ...




Will appreciate any help I can get ....

Sincerely,
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

Alexander Volkov wrote:Straight to the question. What would be the proper way to iterate over two araysLists and perform equals check against each element from one list to another.

The proper way would be not to do it at all. If you want a simple equality check for contents, use:
list1.equals(list2)

But it doesn't sound like that is what you want, so perhaps you could describe the requirements a bit more clearly.

Is it perhaps that you want a third List that contains all elements that are not in one of the Lists? And, if that is the case, how do you want to handle duplicate elements?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Alexander Volkov
Greenhorn

Joined: Sep 01, 2012
Posts: 8
Thank you Winston for the reply ..

Let me set the conditions.
Lists are of a different size so equals are not going to work in this situation

I This would work fine but i would like to know all of the duplicates that were discarded and perform operation on dublicate items ..


Sincerely,
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

Alexander Volkov wrote:This would work fine but i would like to know all of the duplicates that were discarded and perform operation on dublicate items

I'm afraid it won't, because a HashSet (indeed any Set) doesn't allow duplicates.

What you could do is use a HashMap<AsignoreTypes, AtomicInteger>, where the AtomicInteger is the count of the times the element was found. Unfortunately, with that arrangement, you lose the order in which they were found. If that isn't important (and I suspect it isn't), I'd suggest you go down that path.

Winston

BTW: AsignoreTypes seems an odd name for a class. Class names are usually singular, unless they're utility classes (eg, Arrays, Collections...). It's not an absolute requirement, but it is a fairly widely adopted practise.
Alexander Volkov
Greenhorn

Joined: Sep 01, 2012
Posts: 8
Once again Thank you for the reply Winston

I wanted to implement guava or apache commons equal
Sample of guava


That's the main point why i wanted to iterate element of one array agains the list of another and if true perform an operation ....
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

Alexander Volkov wrote:That's the main point why i wanted to iterate element of one array agains the list of another and if true perform an operation...

Right, so you want to go through all the elements of one array and preform an operation if the element ALSO exists in the other array...yes?

You still haven't answered the question of how you deal with duplicates. Suppose the first array (the one you want to iterate) is
{ 72, 23, 46 12, 23, 72, 23 }
and the other one is
{ 14, 62, 23, 72, 8, 23 }
describe what you want to happen when you hit the 2nd '72' and the 2nd and 3rd '23'.

I also don't understand the relevance of the equals() method. You can use any code you like to see if two AsignoreType's match, providing it follows the rules specified by Object.equals(); and what you have seems quite reasonable.

The following is JUST ONE possible solution:and it may not be right for you because you still haven't explained exactly the behaviour you expect of your 'iteration' method.

Winston
Alexander Volkov
Greenhorn

Joined: Sep 01, 2012
Posts: 8
Nice thank you sir for taking time and replying to each of my questions ..

Fist let me answer why I wanted to iterate I need to perform a hibernate db operation on each so i need to know which ones are the duplicates. I wanted to perform an equal operation because with one of the libraries because you can @Overite equal and heshcode with needed for comprising fields and keep the and if equal is true add the heshcode to the set.. well that was the idea in my head not shure about the quality and proper implementation .. If the is an alternative or a better way to do it (As you just showed me ) i would do it the other way..

So the idea was pull felds from db create two lists @Overite equals method with fields I need to be equals and ignore the rest then perform db update on equal objects and insert on unique ...
Sincerely appreciate your sample code ... thank you very much ..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

Alexander Volkov wrote:Sincerely appreciate your sample code ... thank you very much ..

You're welcome, but be sure you don't just copy and paste it. Make sure you understand what it's doing completely.

Winston
Alexander Volkov
Greenhorn

Joined: Sep 01, 2012
Posts: 8
I think I get it
1. We pass on two lists and a boolean to the method
2. Put elements from list2 and AutomicInteger into ( searchElements )

Here I have a question
Why do we pass boolean dupsAllowed what does it do



3. Create list matches with the size of the list1
4. decrementAndGet element from the list
5. SearchElements from the map we created earlier from list two ..

I understand for you it seems generic but in my eyes it is pretty complicated stuff ..

Sincerely,
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

Alexander Volkov wrote:I understand for you it seems generic but in my eyes it is pretty complicated stuff ..

No probs, and it's good that you're checking.

1. If you want to check if an element exists in a List, you have to potentially go through every element of that List (in fact you must go through all elements if the value doesn't exist). Checking if an element exists in a HashMap or HashSet is MUCH faster - in fact usually not much more than checking a single element in a List.

2. Sets and Maps have an annoying restriction: their values (for a Set) or keys (for a Map) must be unique. No such limitation exists for values in a List.

3. So, from (2) above, we want some way of storing the "logical contents" of list2 in a structure that is:
(a) fast to search.
(b) represents ALL the elements in list2 (including, if needed, duplicates).
so my idea was to store it in a HashMap that holds:
  • each distinct element in list2 as a key.
  • a count of the number of times it was found in list2 as the value.

  • 4. Why AtomicInteger? Because it's mutable (ie, it's value can be changed). An Integer can't. That means that every time we want to add one to an Integer, we have to replace it with a new one with the incremented value. With AtomicInteger, all we need to do is to call its incrementAndGet() method.

    5. Put it all together and you have the method I wrote above. The only wrinkle I added was the dupsAllowed parameter, because I still wasn't sure exactly how you wanted to do your check. If every element in both lists (including duplicates) is significant, you pass true; if you only want a list of the distinct elements in list1 that are in list2 (ie, ignoring any duplicates), you pass false.

    Hope that helps to explain.

    Winston
    Alexander Volkov
    Greenhorn

    Joined: Sep 01, 2012
    Posts: 8
    Thank you sir for all of your help
    I wouldn't even thought the are people left who would take time and grind down to the bone the explanation

    Sincerely,
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7484
        
      18

    Alexander Volkov wrote:Thank you sir for all of your help
    I wouldn't even thought the are people left who would take time and grind down to the bone the explanation

    That's because you haven't been on this site for very long; there are plenty of us.

    However, you're very welcome, and good luck with the rest of your project.

    Winston
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Iterate over two ArrayLists
     
    Similar Threads
    creating a DOMTree
    jboss arjuna transactions
    Generic data structures and wildcard types
    Read file from directory, update contents of the each file
    Object Equality Google gwava and Apache Commons