• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Getting Common Values From Two ArrayLists of Different Sizes

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working with two ArrayLists of different sizes (though at times they may be the same size). My expectation is that matching entries will be added to a new ArrayList, and I will end up with an ArrayList of just matches.
That said, my current code is getting matches, but in duplicate entries.



Here is the output:



Why am I getting all these duplicates?
 
Saloon Keeper
Posts: 5657
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have these lists: {a, a} and {a, b, c} what would you expect as outcome? Is the operation commutative? Are the elements implemeting the equals method correctly? And do have a look at the method 'retainAll'.
 
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't given us enough information to go on. You are only calling Log.v() once, yet your output shows it being called multiple times. Each time this section of code is executed the (presumably) list switchTheseOn is being added to. If you are not starting with a new list each time, or clearing out the old list, then I would expect the results your getting.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You haven't given us enough information to go on. You are only calling Log.v() once, yet your output shows it being called multiple times. Each time this section of code is executed the (presumably) list switchTheseOn is being added to. If you are not starting with a new list each time, or clearing out the old list, then I would expect the results your getting.



So, if I ax the loop I should fix the problem?
 
Carey Brown
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Wentz wrote:So, if I ax the loop I should fix the problem?

I doubt it but you can always comment it out and try it.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Adam Wentz wrote:So, if I ax the loop I should fix the problem?

I doubt it but you can always comment it out and try it.



Right after I submitted the reply, it dawned on me that I cannot do so, since I wouldn't have the int to get the index with.

What I did try was adding an inner conditional:



I am still getting the wrong output:



Presumably, it is because of the different sizes of the currentPkgSelections and pkgListStrings ArrayLists.
There are 10 entries in pkgListStrings, which would explain the 10 lines from the Log.
 
Carey Brown
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NOT ENOUGH INFO
We need to see more of the code. How is this section of code invoked? How about answering Piet's question on your desired behavior?
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a broader look at what is happening:



I think my first loop is possibly throwing off my second loop. The first loop is creating a new ArrayList<String> from an ArrayList<PackageInfo>.
I was having issues comparing packageList to currentPkgSelections since they are of different types.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:If you have these lists: {a, a} and {a, b, c} what would you expect as outcome? Is the operation commutative? Are the elements implemeting the equals method correctly? And do have a look at the method 'retainAll'.



From those lists, I would expect a new list of {a}. For the record, though, {a, a} would never be a possibility in my case.
 
Carey Brown
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need to see how you are setting up switchOnThese. Every time you call this block of code this list (?) will get longer. If you don't re-initialize it you will get duplicates.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Need to see how you are setting up switchOnThese. Every time you call this block of code this list (?) will get longer. If you don't re-initialize it you will get duplicates.





switchTheseOn is initialized in its declaration. packageList is passed through the constructor, as is currentPkgSelections.
 
Piet Souris
Saloon Keeper
Posts: 5657
214
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then do a simple second test. Say, compare the lists A and B.
 
Carey Brown
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+Piet
I was going to suggest something like that. You beat me to it.

Anyway, making a very small stand alone program to test behavior outside of your project is often helpful. Perhaps playing with something like this...

I'm not sure why your code isn't working. If the list doesn't contain X, then add X. You might try adding some print statements to see what the code is actually processing.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Then do a simple second test. Say, compare the lists A and B.



I tried this:



I got this:



And I believe that it is due to my first loop messing up my comparison.
 
Piet Souris
Saloon Keeper
Posts: 5657
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Carey, but better two replies than none!

Hmm... but what do you get with
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Sorry Carey, but better two replies than none!

Hmm... but what do you get with





 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But look what I get when:



Output:

 
Piet Souris
Saloon Keeper
Posts: 5657
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
10 lines, while each line has 10 elements. Are these lines printed in a loop or so? I would expect to see just a single line.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:10 lines, while each line has 10 elements. Are these lines printed in a loop or so? I would expect to see just a single line.



Yes, there is a loop involved.

Here is what I am trying to accomplish:

1. I have List<PackageInfo> packageList, which is a ListArray containing system-generated information about applications installed on the device. It has various bits of information, like name, package name, etc., etc.
2. This list is displayed in a RecyclerView that includes a Switch in each row.
3. The user clicks a Switch on and the package name for the relevant row is written to an ArrayList<String>.
4. The ArrayList<String> currentPkgSelections contains all of these selected package names.
5. When the user goes back to that same Activity at a later time/date, the aforementioned values are queried from a database for the purpose of setting relevant switches on, based on what's already been saved by the user.
6. However, before that can happen, packageList is once again populated by the system with applications currently installed on the device. There needs to be a comparison performed between currentPkgSelections and packageList
   to weed out any of the previously-selected applications that are no longer installed (User uninstalled between then and now). Otherwise, once I run a loop to set the relevant Switches on, I would get a NullPointerException for
   those packages that no longer exist.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Wentz wrote:Yes, there is a loop involved.



I forgot to say that before I can do a comparison, I must get the package names from packageList as Strings. I use a loop to do that:



That's what's producing 10 lines of 10 packages, and throwing off the later comparison.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried different code to get a clean ArrayList, but I still get the 10 x 10 output:



Output:

 
Carey Brown
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if pkgStrings was a Set instead of a List?
 
Adam Wentz
Ranch Hand
Posts: 84
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like I finally nailed this thing down.

I eliminated the pkgStrings ArrayList altogether and just did a comparison between the currentPkgSelections and the packageName field of the packageList (which is a String).

I declare a new ArrayList<String> at the top of my class:


Then, in my onCreateViewHolder() method (I'm working in my custom RecyclerView Adapter class), I first clear switchTheseOn and then run a loop with a nested conditional:


I am still getting a weird output in my log, but the new array contains only those packages that are similar between currentPkgSelections and packageList. That fixes my problem at hand!

Apart from that, I did mention that I needed the common values to be able to set the initial state of the switches in my RecyclerView's row layout. If you're interested in knowing how I handled that part of the equation,
In my adapter's onBindViewHolder() method, after I set the package TextView, but before I create the onClickListener for the Switch view, I defined a loop and nested two conditionals inside of it, one to check the value of the appPackage TextView against the values in my switchTheseOn ArrayList<String> and the other to check the current state of the Switch view:


I still have no idea why the output in my log is all messed up but the actual functionality of my ArrayList<String> is fine, so I'm moving on.

Thanks to everyone who took the time to offer valuable insight!!  

 
Piet Souris
Saloon Keeper
Posts: 5657
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Adam,

you're welcome!

Though I'm not sure how we helped you, I'm glad it works for you. Mind you, I downloaded Android Strudio yesterday to see if I can get a little into Android, so I may come back on this issue later, some time coming!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic