Reputation: 333
I have two array. I want to compare two array and return an array which is the first array with elements equal or not equal to the other array. For example:
int[] arr1 = {1,2,3,4,5};
int[] arr2 = {1,3};
I want to return an array or hashmap that shows which elements are equal or not.
boolean[] arr = {true, false, true, false, false};
OR
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "true");
map.put(2, "false");
map.put(3, "true");
map.put(4, "false");
map.put(5, "false");
Output: {1=true, 2=false, 3=true, 4=false, 5=false}
This is my code. This only shows equal values.
for (Int arr1 : arr1) {
for (Int arr2 : arr2) {
if (arr2.equals(arr1)) {
booleanValue = true;
System.err.println(arr2 + ", " + arr1 + ", " + booleanValue);
}
}
}
Please help me out with the logic. Thank You
Upvotes: 2
Views: 2600
Reputation: 34480
There's a nice way to do this with streams:
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {1, 3};
Set<Integer> set2 = new HashSet<>();
for (int a : arr2) {
set2.add(a);
}
Map<Integer, Boolean> map = Arrays.stream(arr1).boxed()
.collect(Collectors.toMap(Function.identity(), set2::contains));
System.out.println(map); // {1=true, 2=false, 3=true, 4=false, 5=false}
First we create a HashSet
from arr2
, then we stream the elements of arr1
, we box them (so we can collect them in the next step), and we finally collect the Integer
elements of the stream into a map: the key is the actual Integer
element, and the value is a boolean indicating whether this element belongs to set2
or not.
EDIT: If you want the values of the map to be strings instead of booleans, you can just change the value mapper function a little bit:
Map<Integer, String> map2 = Arrays.stream(arr1).boxed()
.collect(Collectors.toMap(e -> e, e -> String.valueOf(set2.contains(e))));
System.out.println(map2); // {1=true, 2=false, 3=true, 4=false, 5=false}
Upvotes: 0
Reputation: 3984
In the second loop when comparing both matrices if they are equal you must set the flag to true and break the second loop, so the first loop will compare the next element, for example:
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {1, 3};
HashMap<Integer, Boolean> map = new HashMap<>();
for (int i : arr1) {
for (int j : arr2) {
if (j == i) {
map.put(i, true);
break;
} else {
map.put(i, false);
}
}
}
System.out.println(Arrays.asList(map));
Output:
[{1=true, 2=false, 3=true, 4=false, 5=false}]
Upvotes: 0
Reputation: 357
I would do something like
public static void main(String[] args) {
HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {1, 3};
for (int a : arr1) {
map.put(a, isElementInArray(a, arr2));
}
System.out.println(Arrays.asList(map));
}
public static boolean isElementInArray(int element, int[] array) {
for (int a : array) {
if (a == element) {
return true;
}
}
return false;
}
Output:
[{1=true, 2=false, 3=true, 4=false, 5=false}]
Upvotes: 0
Reputation: 12870
Instead of using foreach loop use fori loop. For each element of arr1 check for each element of arr2 if match then set the arr of that index true and break the loop.
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {1, 3};
boolean[] arr = new boolean[arr1.length];
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
arr[i] = true;
}
}
}
System.out.println(Arrays.toString(arr));
Upvotes: 1