Java Compare Two List's object values? - java

I have two list **ListA<MyData> listA = new ArrayList<MyData>()** and ListB<MyData> listB = new ArrayList<MyData>() both contain object of type MyData and MyData contain these variables.
MyData {
String name;
boolean check;
}
ListA and ListB both contains MyData objects ,now I have to compare both the list's object values here name as well check variable like if ListA contains these object values
ListA = ["Ram",true],["Hariom",true],["Shiv",true];
and ListB also contain
ListB = ["Ram",true],["Hariom",true],["Shiv",true];
then i have to compare lists and return false because both list are same
But if ListA contains
ListA = ["Ram",true],["Hariom",true],["Shiv",false];
and ListB Contain
ListB = ["Ram",true],["Hariom",true],["Shiv",true];
then I have to compare lists and return true because both list are not same
or vice-versa so any slight change in the any list values I have to return true.
One thing I have to mentioned here objects can be in any order.

It's not the most efficient solution but the most terse code would be:
boolean equalLists = listA.size() == listB.size() && listA.containsAll(listB);
Update:
#WesleyPorter is right. The solution above will not work if duplicate objects are in the collection.
For a complete solution you need to iterate over a collection so duplicate objects are handled correctly.
private static boolean cmp( List<?> l1, List<?> l2 ) {
// make a copy of the list so the original list is not changed, and remove() is supported
ArrayList<?> cp = new ArrayList<>( l1 );
for ( Object o : l2 ) {
if ( !cp.remove( o ) ) {
return false;
}
}
return cp.isEmpty();
}
Update 28-Oct-2014:
#RoeeGavriel is right. The return statement needs to be conditional. The code above is updated.

ArrayList already have support for this, with the equals method. Quoting the docs
...
In other words, two lists are defined to be equal if they contain the same elements in the same order.
It does require you to properly implement equals in your MyData class.
Edit
You have updated the question stating that the lists could have different orders. In that case, sort your list first, and then apply equals.

I got this solution for above problem
public boolean compareLists(List<MyData> prevList, List<MyData> modelList) {
if (prevList.size() == modelList.size()) {
for (MyData modelListdata : modelList) {
for (MyData prevListdata : prevList) {
if (prevListdata.getName().equals(modelListdata.getName())
&& prevListdata.isCheck() != modelListdata.isCheck()) {
return true;
}
}
}
}
else{
return true;
}
return false;
}
EDITED:-
How can we cover this...
Imagine if you had two arrays "A",true "B",true "C",true and "A",true "B",true "D",true. Even though array one has C and array two has D there's no check that will catch that(Mentioned by #Patashu)..SO for that i have made below changes.
public boolean compareLists(List<MyData> prevList, List<MyData> modelList) {
if (prevList!= null && modelList!=null && prevList.size() == modelList.size()) {
boolean indicator = false;
for (MyData modelListdata : modelList) {
for (MyData prevListdata : prevList) {
if (prevListdata.getName().equals(modelListdata.getName())
&& prevListdata.isCheck() != modelListdata.isCheck()) {
return true;
}
if (modelListdata.getName().equals(prevListdata.getName())) {
indicator = false;
break;
} else
indicator = true;
}
}
}
if (indicator)
return true;
}
}
else{
return true;
}
return false;
}

First, implement the MyData.equals(Object o) and MyData.hashCode() methods.
Once you implemented the equals method, you can iterate over the lists as follows:
if(ListA == null && ListB == null)
return false;
if(ListA == null && ListB != null)
return true;
if(ListA != null && ListB == null)
return true;
int max = ListA.size() > ListB.size() ? ListA.size() : ListB.size();
for(int i = 0; i < max; i++) {
myData1 = ListA.get(i);
myData2 = ListB.get(i);
if(!myData1.equals(myData2)) {
return true;
}
}
return false;

I found a very basic example of List comparison at List Compare
This example verifies the size first and then checks the availability of the particular element of one list in another.

This can be done easily through Java8 using forEach and removeIf method.
Take two lists. Iterate from listA and compare elements inside listB
Write any condition inside removeIf method.
Hope this will help
listToCompareFrom.forEach(entity -> listToRemoveFrom.removeIf(x -> x.contains(entity)));

Override the equals method in your class and use Collection#equals() method to check for equality.

See if this works.
import java.util.ArrayList;
import java.util.List;
public class ArrayListComparison {
public static void main(String[] args) {
List<MyData> list1 = new ArrayList<MyData>();
list1.add(new MyData("Ram", true));
list1.add(new MyData("Hariom", true));
list1.add(new MyData("Shiv", true));
// list1.add(new MyData("Shiv", false));
List<MyData> list2 = new ArrayList<MyData>();
list2.add(new MyData("Ram", true));
list2.add(new MyData("Hariom", true));
list2.add(new MyData("Shiv", true));
System.out.println("Lists are equal:" + listEquals(list1, list2));
}
private static boolean listEquals(List<MyData> list1, List<MyData> list2) {
if(list1.size() != list2.size())
return true;
for (MyData myData : list1) {
if(!list2.contains(myData))
return true;
}
return false;
}
}
class MyData{
String name;
boolean check;
public MyData(String name, boolean check) {
super();
this.name = name;
this.check = check;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (check ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyData other = (MyData) obj;
if (check != other.check)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}

You can subtract one list from the other using CollectionUtils.subtract, if the result is an empty collection, it means both lists are the same. Another approach is using CollectionUtils.isSubCollection or CollectionUtils.isProperSubCollection.
For any case you should implement equals and hashCode methods for your object.

Using java 8 removeIf to compare similar items
public int getSimilarItems(){
List<String> one = Arrays.asList("milan", "dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta");
List<String> two = new ArrayList<>(Arrays.asList("hafil", "iga", "binga", "mike", "dingo")); //Cannot remove directly from array backed collection
int initial = two.size();
two.removeIf(one::contains);
return initial - two.size();
}

Logic should be something like:
First step: For class MyData implements Comparable interface, override the compareTo method as per the per object requirement.
Second step: When it comes to list comparison (after checking for nulls),
2.1 Check the size of both lists, if equal returns true else return false, continue to object iteration
2.2 If step 2.1 returns true, iterate over elements from both lists and invoke something like,
listA.get(i).compareTo(listB.get(i))
This will be as per the code mentioned in step-1.

It's been about 5 years since then and luckily we have Kotlin now.
Comparing of two lists now looks is as simple as:
fun areListsEqual(list1 : List<Any>, list2 : List<Any>) : Boolean {
return list1 == list2
}
Or just feel free to omit it at all and use equality operator.

I know it's old question but in case anyone needs it. I use this in my application and it works well. i used it to check if the cart has been changed or not.
private boolean validateOrderProducts(Cart cart) {
boolean doesProductsChanged = false;
if (originalProductsList.size() == cart.getCartItemsList().size()) {
for (Product originalProduct : originalProductsList) {
if (!doesProductsChanged) {
for (Product cartProduct : cart.getCartProducts()) {
if (originalProduct.getId() == cartProduct.getId()) {
if (originalProduct.getPivot().getProductCount() != cartProduct.getCount()) {
doesProductsChanged = true;
// cart has been changed -> break from inner loop
break;
}
} else {
doesProductsChanged = false;
}
}
} else {
// cart is already changed -> break from first loop
break;
}
}
} else {
// some products has been added or removed (simplest case of Change)
return true;
}
return doesProductsChanged;
}

String myData1 = list1.toString();
String myData2 = list2.toString()
return myData1.equals(myData2);
where :
list1 - List<MyData>
list2 - List<MyData>
Comparing the String worked for me. Also NOTE I had overridden toString() method in MyData class.

I think you can sort both lists and convert to List if some of them was a HashSet colleciton.
java.utils.Collections package lets you do it.
List<Category> categoriesList = new ArrayList<>();
Set<Category> setList = new HashSet<>();
Collections.sort(categoriesList);
List<Category> fileCategories = new ArrayList<>(setList);
Collections.sort(fileCategories);
if(categoriesList.size() == fileCategories.size() && categoriesList.containsAll(fileCategories)) {
//Do something
}

Related

Is there any way to find the duplicate values i.e ArrayList from a hash map?

For eg:-
I have declared a hashMap in the form of:
Map<String, List<Tracks>> dupItems = new LinkedHashMap();
Tracks is a model class that contains name, address, and age.
and I added items in a Tracks
and I added Items as:-
dupItems.add("Project",tracks);
dupItems.add("Report",tracks);
and what I want is a list of duplicate tracks i.e how can I match the items on the basis of values of tracks i.e all values have to be similar. same name, same address, and same age.
If you are looking for the duplicate map values, that is, duplicate lists of Tracks:
One way you can do this is iterate over the values, putting them all in a Set data structure. If you find that the value was already in the set, then it's a duplicate, and you add it to a separate data structure that keeps a record of the duplicate values:
Set<List<Tracks>> findDuplicateValues(Map<String, List<Tracks>> dupItems) {
Set<List<Tracks>> allValues = new HashSet<>();
Set<List<Tracks>> duplicateValues = new HashSet<>();
for (List<Tracks> value : dupItems.values()) {
if (!allValues.add(value)) {
// It's a duplicate!
duplicateValues.add(value);
}
}
return duplicateValues;
}
For this to work reliably, the Tracks class has to implement the equals and hashCode methods. Comparing two objects with the same values using the equals method should return true.
On the other hand, if you are looking for the duplicate Tracks values, no matter which map value list contains them:
you just need to add a loop to the previous method:
Set<List<Tracks>> findDuplicateValues(Map<String, List<Tracks>> dupItems) {
Set<List<Tracks>> allValues = new HashSet<>();
Set<List<Tracks>> duplicateValues = new HashSet<>();
for (List<Tracks> value : dupItems.values()) {
for (Tracks value : values) {
if (!allValues.add(value)) {
// It's a duplicate!
duplicateValues.add(value);
}
}
}
return duplicateValues;
}
I'm assuming we search for duplicate Tracks instances.
public Set<Tracks> findDuplicates(Map<String, List<Tracks>> dupItems) {
Set<Tracks> all = new HashSet<>();
return dupItems.values().stream()
.flatMap(list -> list.stream()) // build a single list of Tracks
.filter(t -> !all.add(t)) // add track to all set but only continue if duplicate
.collect(Collectors.toSet()); // store the remaining tracks in a set (to avoid duplicates in the result
}
A Set will check on duplicates only if the equals() function is correctly implemented.
My eclipse editor generated :
private static class Tracks {
String name;
String address;
int age;
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Tracks other = (Tracks) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
You can also have a look at Apache Commons EqualsBuilder

Remove duplicate Tuples from an ArrayList- Java

I want to have an unique List of tuples without duplicates.
List <Tuple> newNonZeros = new ArrayList<>();
and in newNonZeroes is the result: [(0,2)(1,2)(1,2)(1,1)(2,2)(2,2)(2,1)]
Here's what I tried:
List <Tuple> newList = new ArrayList<>();
newList.add(newNonZeros.get(0));
for(int i=1; i < newNonZeros.size();i++){
if(newNonZeros.get(i-1)!= newNonZeros.get(i)){
newList.add(newNonZeros.get(i));
}
}
It doesn't work. Can someone help me...it's a very simple problem
I also tried the following method:
...newNonZeros.stream().distinct().collect(Collectors.toList());
Override equals and hashCode in your Tuple class:
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Tuple tuple = (Tuple) o;
return first == tuple.first &&
second == tuple.second;
}
#Override
public int hashCode() {
return Objects.hash(first, second);
}
Then you can use:
List<Tuple> newList = newNonZeros.stream()
.distinct()
.collect(Collectors.toList());
Or with a for-each loop:
List<Tuple> newList = new ArrayList<>();
for(Tuple tuple : newNonZeros) {
if(!newList.contains(tuple)) {
newList.add(tuple);
}
}

A Java Set for at most 2 objects

I'm writing a method that returns a Set<String>. The set may contain 0, 1, or 2 objects. The string keys are also quite small (maximum 8 characters). The set is then used in a tight loop with many iterations calling contains().
For 0 objects, I would return Collections.emptySet().
For 1 object, I would return Collections.singleton().
For 2 objects (the maximum possible number), a HashSet seems overkill. Isn't there a better structure? Maybe a TreeSet is slightly better? Unfortunately, I'm still using Java 7 :-( so can't use modern things like Set.of().
An array of 2 strings would probably give the best performance, but that's not a Set. I want the code to be self-documenting, so I really want to return a Set as that is the logical interface required.
Just wrap an array with an AbstractSet. You only have to implement 2 methods, assuming you want an unmodifiable set:
class SSet extends AbstractSet<String> {
private final String[] strings;
SSet(String[] strings) {
this.strings = strings;
}
#Override
public Iterator<String> iterator() {
return Arrays.asList(strings).iterator();
}
#Override
public int size() {
return strings.length;
}
}
If you want, you can store the Arrays.asList(strings) in the field instead of a String[]. You can also provide 0, 1 and 2-arg constructors if you want to constrain the array only to be that length.
You can also override contains:
public boolean contains(Object obj) {
for (int i = 0; i < strings.length; ++i) {
if (Objects.equals(obj, strings[i])) return true;
}
return false;
}
If you don't want to create a list simply to create an iterator, you can trivially implement one as an inner class:
class ArrayIterator implements Iterator<String> {
int index;
public String next() {
// Check if index is in bounds, throw if not.
return strings[index++];
}
public boolean hasNext() {
return index < strings.length;
}
// implement remove() too, throws UnsupportedException().
}
The set is then used in a tight loop with many iterations calling contains().
I would probably streamline it for this. Perhaps something like:
public static class TwoSet<T> extends AbstractSet<T> {
T a = null;
T b = null;
#Override
public boolean contains(Object o) {
return o.equals(a) || o.equals(b);
}
#Override
public boolean add(T t) {
if(contains(t)){
return false;
}
if ( a == null ) {
a = t;
} else if ( b == null ) {
b = t;
} else {
throw new RuntimeException("Cannot have more than two items in this set.");
}
return true;
}
#Override
public boolean remove(Object o) {
if(o.equals(a)) {
a = null;
return true;
}
if(o.equals(b)) {
b = null;
return true;
}
return false;
}
#Override
public int size() {
return (a == null ? 0 : 1) + (b == null ? 0 : 1);
}
#Override
public Iterator<T> iterator() {
List<T> list;
if (a == null && b == null) {
list = Collections.emptyList();
} else {
if (a == null) {
list = Arrays.asList(b);
} else if (b == null) {
list = Arrays.asList(a);
} else {
list = Arrays.asList(a, b);
}
}
return list.iterator();
}
}
You can achieve this by
Make a class that implements Set interface
Override add and remove method
Add value upon class initialisation by super.add(E element)
Use that class instead

Trying to compare the contents two Iterators, how?

EDIT: With your help I managed to fix my problem. I have edited my code to now show how I had to have it set up to get it working.
Currently I am having trouble coding a part which compares the content of two iterators. As part of the requirements for my assignment, I need to use a linkedlist to store the individual characters of the entered String. I have gotten to the point where I have two iterators which would contain the input one way and the reverse way.
String palindrom = input.getText();
String [] chara = palindrom.split (""); //this is successfully splitting them, tested.
int length = palindrom.length( ); // length == 8
System.out.println (length); //can use this for how many checks to do?
LinkedList ll = new LinkedList(Arrays.asList(chara));
Iterator iterator = ll.iterator();
Iterator desIterator = ll.descendingIterator();
/*while(iterator.hasNext() ){
System.out.println(iterator.next() );
}
while(desIterator.hasNext() ){
System.out.println(desIterator.next() );
}*/
boolean same = true;
while(iterator.hasNext()){
if(!iterator.next().equals(desIterator.next())){
same = false;
break;
}
}
And using the System.out I can see that they are being stored correctly, but I don't know how to check if the iterators store the same contents. What would be one of the simplest methods to compare the two iterators or convert them into something I can compare? To clarify I want to verify they contain the same elements in the same order.
boolean same = true;
while(iterator.hasNext()){
if(!desIterator.hasNext() || !iterator.next().equals(desIterator.next())){
same = false;
break;
}
}
System.out.println(same);
You need to iterate over both iterators simultaneously, i.e. with one loop. Here is a general comparison function (0 when equal, < 0 when A < B, > 0 when A > B):
static <T extends Comparable<S>, S> int compare(Iterator<T> a, Iterator<S> b) {
while (a.hasNext() && b.hasNext()) {
int comparison = a.next().compareTo(b.next());
if (comparison != 0) {
return comparison;
}
}
if (a.hasNext())
return 1;
if (b.hasNext())
return -1;
return 0;
}
To just check if they are equal, this can be simplified:
static <T, S> boolean equals(Iterator<T> a, Iterator<S> b) {
while (a.hasNext() && b.hasNext()) {
if (!a.next().equals(b.next())) {
return false;
}
}
if (a.hasNext() || b.hasNext()) {
// one of the iterators has more elements than the other
return false;
}
return true;
}
Guava implements this as Iterators.elementsEqual.
In both answers throw NullPointerException, if iterator.next() == null. This method is more optimal.
public static boolean equals(Iterator i1, Iterator i2) {
if (i1 == i2) {
return true;
}
while (i1.hasNext()) {
if (!i2.hasNext()) {
return false;
}
if (!Objects.equals(i1.next(), i2.next())) {
return false;
}
}
if (i2.hasNext()) {
return false;
}
return true;
}

Java HashMap Iterate Each Variable and Return Value

I have a class with various Booleans and Integers.
class Animal {
boolean mHappy = false;
boolean mHungry = false;
boolean mSleeping = false;
int mCost = 0;
int mWeight = 0;
boolean isEmpty() {
return !mHappy && !mHungry && !mSleeping && mCost == 0 && mWeight == 0;
}
}
The method boolean isEmpty() will tell me if all the values are empty.
Now, I want to move all my data into HashMaps:
class Animal {
HashMap<String, Boolean> mBools = new HashMap<String, Boolean>(){{
put("mHappy", false);
put("mHungry", false);
put("mSleeping", false);
}
};
HashMap<String, Integer> mInts = new HashMap<String, Integer>(){{
put("mCost", 0);
put("mWeight", 0);
}
};
boolean isEmpty() {
// MY QUESTION: How can I make this function iterate through each HashMap,
// regardless of size, and check to make sure it's "false" or "0" like this
// line did when I only was using static booleans and integers?
return !mHappy && !mHungry && !mSleeping && mCost == 0 && mWeight == 0;
}
}
My Question is about the "boolean isEmpty()" method, How can I make this function iterate through each HashMap, regardless of size, and check to make sure each value is "false" or "0"?
This will do it:
boolean isEmpty() {
for (int i : mInts.values()) if (i != 0) return false;
for (boolean b : mBools.values()) if (b) return false;
return true;
}
Read the map tutorial for more info about iterating through the contents of a map.
Call the values() method on each map; you can iterate through the returned Collection of values. Then you can check each value to see if they are 0 or false as the case may be.
Keep a boolean, initialized to true, if everything is "empty" so far. Set it to false if a value isn't 0 or false.
You just need to iterate over the values in your maps:
boolean isEmpty {
for (Integer i : mInts.values()) {
if (i > 0) {
return false;
}
}
for (Boolean b : mBools.values()) {
if (b) {
return false;
}
}
return true;
}

Categories