You don't need two loops to do this, One loop is sufficient to fix it
ArrayList<String>> buttonresourceIds = new ArrayList<String>>();
ArrayList<String>> buttonresourceIds2 = new ArrayList<String>>();
int common=0;
for(int j=0;j<buttonresourceIds2.size();j++){
if(buttonresourceIds.contains(buttonresourceIds2.get(j))){
System.out.println("Exist : "+buttonresourceIds2.get(j));
common++;
}else{
System.out.println("Not Exist : "+buttonresourceIds2.get(j));
}
}
return common;
Answer from PotatoManager on Stack OverflowYou don't need two loops to do this, One loop is sufficient to fix it
ArrayList<String>> buttonresourceIds = new ArrayList<String>>();
ArrayList<String>> buttonresourceIds2 = new ArrayList<String>>();
int common=0;
for(int j=0;j<buttonresourceIds2.size();j++){
if(buttonresourceIds.contains(buttonresourceIds2.get(j))){
System.out.println("Exist : "+buttonresourceIds2.get(j));
common++;
}else{
System.out.println("Not Exist : "+buttonresourceIds2.get(j));
}
}
return common;
See Below Code :
ArrayList<String>> buttonresourceIds = new ArrayList<String>>();
ArrayList<String>> buttonresourceIds2 = new ArrayList<String>>();
for(int i=0;i<buttonresourceIds.size();i++){
if(buttonresourceIds2.contains(buttonresourceIds.get(i)))
{
System.out.println("Exist : "+buttonresourceIds.get(i));
}
else{
System.out.println("Not Exist : "+buttonresourceIds.get(i));
}
}
In Above code, only one Loop is required.
Comparing two arrays in java with for loop - Stack Overflow
comparing elements of two arrayList in java - Stack Overflow
java - Compare elements from an ArrayList - Stack Overflow
java - Compare every item to every other item in ArrayList - Stack Overflow
Convert Lists to Collection and use removeAll
Collection<String> listOne = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
Collection<String> listTwo = new ArrayList(Arrays.asList("a","b", "d", "e", "f", "gg", "h"));
List<String> sourceList = new ArrayList<String>(listOne);
List<String> destinationList = new ArrayList<String>(listTwo);
sourceList.removeAll( listTwo );
destinationList.removeAll( listOne );
System.out.println( sourceList );
System.out.println( destinationList );
Output:
[c, g]
[gg, h]
[EDIT]
other way (more clear)
Collection<String> list = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
List<String> sourceList = new ArrayList<String>(list);
List<String> destinationList = new ArrayList<String>(list);
list.add("boo");
list.remove("b");
sourceList.removeAll( list );
list.removeAll( destinationList );
System.out.println( sourceList );
System.out.println( list );
Output:
[b]
[boo]
This should check if two lists are equal, it does some basic checks first (i.e. nulls and lengths), then sorts and uses the collections.equals method to check if they are equal.
public boolean equalLists(List<String> a, List<String> b){
// Check for sizes and nulls
if (a == null && b == null) return true;
if ((a == null && b!= null) || (a != null && b== null) || (a.size() != b.size()))
{
return false;
}
// Sort and compare the two lists
Collections.sort(a);
Collections.sort(b);
return a.equals(b);
}
Try this code.
char[] firstArray = {'a', 'b', 'c'};
char[] secondArray = {'a', 'b', 'c'};
if (firstArray.length != secondArray.length) {
System.out.println("False.they are not equal");
} else {
boolean isEqual = true;
for (int i = 0; i < firstArray.length; i++) {
if (firstArray[i] != secondArray[i]) {
System.out.println("False.they are not equal");
isEqual = false;
break;
}
}
if (isEqual)
System.out.println("true.they are equal");
}
boolean isEqual = true;
for(int i=0;i<arrayLen;i++){
if(firstArray[i] != secondArray[i]){
System.out.println("False.they are not equal");
isEqual = false;
break;
}
}
if(isEqual){
System.out.println("true.they are equal");
}
If that not work, try to change (firstArray[i] != secondArray[i]) to (!(firstArray[i].equals(secondArray[i]))) or st like that.
Only iterate on first arraylist with larger length and check for contains in second arraylist , if found set one else do nothing
for(int counter = 0; counter < firstList.size(); counter++) {
if(secondList.contains(firstList.get(counter))) {
comparingList.set(counter,1);
}
}
Whole java program
Just try to run the below program in http://www.compileonline.com/compile_java_online.php
import java.util.ArrayList;
import java.util.List;
public class CompareArrayListTest{
public static void main(String[] args) {
ArrayList<String> firstList = new ArrayList<String>();
firstList.add("book1");
firstList.add("book2");
firstList.add("book3");
firstList.add("book4");
ArrayList<String> secondList = new ArrayList<String>();
secondList.add("book1");
secondList.add("book2");
secondList.add("book3");
List<Integer> comparingList = new ArrayList<Integer>();
// adding default values as one
for (int a = 0; a < firstList.size(); a++) {
comparingList.add(0);
}
for (int counter = 0; counter < firstList.size(); counter++) {
if (secondList.contains(firstList.get(counter))) {
comparingList.set(counter, 1);
}
}
System.out.println(comparingList);
}
BitSet bitset = new BitSet();
// adding default values as one
for (int a = 0; a < firstList.size(); a++) {
comparingList.add(0);
}
for (int counter = 0; counter < firstList.size(); counter++) {
for (int counter2 = 0; counter < secondList.size(); counter++) {
if (secondList.get(counter2).equals(firstList.get(counter))) {
bitset.set(counter, 1);
}
}
}
You can transform the lists to sets, and then use Set.retainAll method for intersection between the different sets. Once you intersect all sets, you are left with the common elements, and you can transform the resulting set back to a list.
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < list.size(); j++) {
// compare list.get(i) and list.get(j)
}
}
What's the problem with using for loop inside, just like outside?
for (int j = i + 1; j < list.size(); ++j) {
...
}
In general, since Java 5, I used iterators only once or twice.
Can I sort two arraylists containing objects so that I can compare them and their contents? I tried to type in:
Collections.sort.myArrayList but it wouldnt work!
you should use CollectionUtils.retainAll : Returns a collection containing all the elements in collection1 that are also in collection2.
ArrayList commonList = CollectionUtils.retainAll(list1,list2);
You should transform you first list into a HashSet. HashSet lookups are O(1), and List lookups are O(n). This makes the whole algorithm O(n) rather than O(n^2)
Set<Foo> set1 = new HashSet<Foo>(list1);
for (Foo foo : list2) {
if (set1.contains(foo)) {
matchedPixels++;
}
}
It is explicitly clear what your intent is with the C-like for loops - loop over every pair exactly once, so that the same pair with reversed members, e.g. (a, b) and (b, a) doesn't get processed again. That is what I would recommend; it works in Java just as well.
However, if you absolutely have to have the enhanced for loop, you can have the inner for loop operate over a sublist, with List's subList method, starting with the next element.
for (Object o1 : list) {
List<Object> subList = list.subList(list.indexOf(o1) + 1, list.size());
for (Object o2 : subList) {
//Do something with o1 and o2
}
}
The enhanced for loop is not appropriate in all situations. If you avoid using an index, just to use indexOf in the loop, your code will be less efficient (indexOf is a linear search) and non-idiomatic.
In my opinion, the best answer is to use the explicit indices.
for (i=0; i<n-1; i++) {
for (j=i+1; j<n; j++) {
// do something with list.get(i) and list.get(j)
}
}
One situation where you should avoid using get is if the List is a LinkedList, because get for a LinkedList is not a constant time operation. In this case I would do
List<Object> copy = new ArrayList<>(linkedList);
for (i=0; i<n-1; i++) {
for (j=i+1; j<n; j++) {
// do something with copy.get(i) and copy.get(j)
}
}
Try using a for loop to iterate through the first ArrayList and use the counter ('i' in the example below) from that for loop to compare each of the indices that you loop through using the get method provided by ArrayList.
for (int i = 0; i < aList1.size(); i++) {
if (!aList1.get(i).equals(aList2.get(i)))
//output whatever you want it to say
}
Edit: changed to .equals instead of == as suggestion. Good catch.
Compare their datatypes of both the elements of your list if they match then try to compare their contents For comparing the datatype follow the below mentioned code
int a = 10;
Object o = a;
System.out.println(o.getClass().getSimpleName());
If the datatype matches then try to compare their contents