Compare List Java
For immutableuse equallist we simple use list.equal list it return true if content same. (compare index)
//case true
List<Integer> listOne = new ArrayList<>(Arrays.asList(1, 4, 6, 3, 66, 3, 33));
List<Integer> listTwo = new ArrayList<>(Arrays.asList(1, 4, 6, 3, 66, 3, 33));
System.out.println("WrapperClass listOne.equals(listTwo): " + listOne.equals(listTwo));
// case false
List<Integer> listOne = new ArrayList<>(Arrays.asList(1, 4, 6, 3, 66, 3, 33));
List<Integer> listTwo = new ArrayList<>(Arrays.asList(4, 1, 6, 3, 66, 3, 33));
System.out.println("WrapperClass listOne.equals(listTwo): " + listOne.equals(listTwo));
We can compare list withlist by various ways:
Operator ==: true if two list similar memory (oparator =) example below.
List a = list b
return a == b return true
Equal Method (compare index): True if element is immutable class, if class is customize(self definition). we must override Equal & hascode method of class element, if not return false all case.
removeAllmethod then check size: It method dependent method equal check delete so we must override Equal & hashcode method of class element. (not distinguish index). Notice must copy list original by new ArrayList<>(listoriginal) before compare if not list original will be remove some element.
retainAllmethodthen compare size: It method dependent method equal check delete defference so we must override Equal & hashcode method of class element. (not distinguish index) . Notice
must copy list original by new ArrayList<>(listoriginal) before
compare if not list original will be remove some elements.
Stream filterListCheckContain Collect: Use predicate of inside method fillter of Stream. it indepent of method equal & hashcode of class element so we must override them. (not distinguish index)
0 comments :
About Me

- Peter
- Tân An, Long An, Vietnam
- Hello everyone, I love programming, I love making friends with people all over the world.
Contact
Popular Posts
-
The one of Design Pattern Series, this is factory design for createnal pattern. Please read in the linked document. Link document: https:...
-
youtube tutorial: https://www.youtube.com/watch?v=IEf1KAcK6A8 1 let and scope, 2 const, 3 array function 4 default value 5 Object iteral ext...
-
1 Copy Object in java Use = for immutable class ex: Integer , String or Wrapper classes Object Use Contructor has paramater copy, ObjectA...
-
JPA find() & getReference() in javax.persistence.EntityManager * find (): always call select from DB Use for select one entity from DB...
-
Preface Trong java có 3 kiểu so sánh đặc trưng như sau: + Sử dụng toán tử == : return Boolean Primitive thì so sánh giá trị thực, Reference ...
-
Persistence Context has two implemnet is JPA EntityManage and Hiberbate Session Example use Session of hiberate has exist github you can vi...
-
1 check Null and Undefined And primative datatype if(object){ // object is not null, undefined, 0, '', false. but it can empty({})...
-
1) CRUD có thể sử dung find Compositekey hoặc findBy compositeket + column in composite 2) CRUD findBy column And column 3 Not LazyLoa...
-
This post use language vietnames is, the future is will introduce english version. The clean code for java also application for the others. ...
-
Parse LocalDateTime to String to pattern year : yyyy || 2021 month of year: M / MMM / MMMM || 06 / Jun / June da...
Post a Comment