Compare two arrayList get common value and it should it not affect original values
Compare two arrayList get common value and it should it not affect original values
public void commonId(ArrayList<ArrayList<String>> stylistIdList) {
if (stylistIdList.size() > 1) {
array1=stylistIdList.get(0);
array2=stylistIdList.get(1);
booleanStylistid=array1.retainAll(array2);
} else {
array1=stylistIdList.get(0);
}
}
Here I am having ArrayList<ArrayList<String>> stylistIdList contains two arraylist i want get common values from the two list.Its returning the common values but after getting common values the ArrayList<ArrayList<String>> stylistIdList value is changed into the values which is common.I want same values in ArrayList<ArrayList<String>> stylistIdList after getting common values.
ArrayList<ArrayList<String>> stylistIdList
arraylist
ArrayList<ArrayList<String>> stylistIdList
ArrayList<ArrayList<String>> stylistIdList
Example
ArrayList<ArrayList<String>> stylistIdLis = 0(0["abcdefgh"], ["abc"]), 1(0["abcdefgh"]);
output stylistIdList=("abcdefgh");
stylistIdList=("abcdefgh");
after getting output the ArrayList<ArrayList<String>> stylistIdLis values is changed into ArrayList<ArrayList<String>> stylistIdLis = 0("abcdefgh"), 1("abcdefgh");
But it i want like this
ArrayList<ArrayList<String>> stylistIdLis
ArrayList<ArrayList<String>> stylistIdLis = 0("abcdefgh"), 1("abcdefgh");
ArrayList<ArrayList<String>> stylistIdLis = 0(0["abcdefgh"],["abc"]), 1(0["abcdefgh"]);
1 Answer
1
retainAll modifies the list it is called on. If you want to get the common IDs without modifying the original lists, you need to return a new list.
retainAll
public ArrayList<String> commonIds(ArrayList<ArrayList<String>> lists) {
List<String> common = new ArrayList<>(lists.get(0));
if (lists.size() > 1) common.retainAll(lists.get(1));
return common;
}
its working thank you
– deepa
Jun 30 at 7:14
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
K i will try this
– deepa
Jun 30 at 7:04