Implement equals, hashcode in User
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof User))
return false;
User u = (User) obj;
return this.empCode == null ? false : this.empCode
.equals(u.empCode);
}
@Override
public int hashCode() {
return this.empCode == null ? 0 : this.empCode.hashCode();
}
@Override
public String toString() {
return "Emp Code: " + this.empCode;
}
Then use retainAll
list2.retainAll(list1);-->EMP01, EMP02, EMP09, EMP10
Answer from Amit Deshpande on Stack OverflowComparing two Arraylists
arraylist - How to compare two array lists for similar objects which differ in at least one property in java? - Stack Overflow
java - Comparing two object arraylists - Stack Overflow
A question about the comparison of elements in two ArrayLists in java.
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!
Implement equals, hashcode in User
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof User))
return false;
User u = (User) obj;
return this.empCode == null ? false : this.empCode
.equals(u.empCode);
}
@Override
public int hashCode() {
return this.empCode == null ? 0 : this.empCode.hashCode();
}
@Override
public String toString() {
return "Emp Code: " + this.empCode;
}
Then use retainAll
list2.retainAll(list1);-->EMP01, EMP02, EMP09, EMP10
I think this what you should do -
for(User user1 : list1) {
for(User user2 : list2) {
if(user1.getEmpCode().equals(user2.getEmpCode())) {
if(!user1.getFirstName().equals(user2.getFirstName()) ||
!user1.getLastName().equals(user2.getLastName()) ||
!user1.getEmail().equals(user2.getEmail())) {
resultList.add(user1);
}
}
}
}
It might not make sense for the User to override equal and hashCode only to serve this purpose. They should be overriden in the way in which it makes more sense domain-wise.
You can do something like this:
for(User u1 : l1) {
boolean unique = true;
for(User u2: l2) {
if(u1.getUserEnteredValue().equals(u2.getValueReturnedFromDatabase())) {
unique = false;
break;
}
}
if(unique){
l3.add(u1);
}
}
You can use the contains() method from java.util.ArrayList to determine whether your list contains the object.
Example:
for(User u1:l1) {
if(!l2.contains(u1)) {
l3.add(u1);
}
}
I'm trying to do my homework
I created two ArraLists of Integer, and the content in the list is as follows:
List1:
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150
List2:
150 300 450 600 750
when i use
list1. get(list1. size() - 1) == list2. get(0);
I get false;
I have to use
list1.get(list1.size() - 1).equals(list2.get(0));
to get true;
Then I try the following code:
ArrayList<Integer> arrList1 = new ArrayList<Integer>();
ArrayList<Integer> arrList2 = new ArrayList<Integer>();
arrList1. add(10);
arrList2.add(10);
System.out.println(arrList1.get(0) == arrList2.get(0));
I get true;
I know that == can only be used to compare values, or Primitive types, while .equals() is used to compare references.
I'm sure what I'm storing in the ArrayList is integer, int, so why can't I use == for comparison? (And I'm also wondering why comparing 10 with == gets true and 150 gets false)
I wonder why this is happening?
Thanks in advance to everyone who helped me.
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);
}
You have already got the Product out of the List<Product> in the following statement:
System.out.println(products.get(i));
Now, that you have got Product, now to get it's id, you can just call it's getId() method:
if (product.get(i).getId() == productId) {
// Return this product.
}
I would also suggest you to use enhanced for-loop instead of the traditional loop like this:
for (Product product: products) {
// Now you have the product. Just get the Id
if (product.getId() == productId) {
return product;
}
}
Also, you should change the type of productId from Integer to int. You don't need a wrapper type there.
Have you considered using a HashMap (or LinkedHashMap) instead of an Array. This way you can use the productId as the key and the product as the value?
This will let you get the object without having to loop through the entire array.