简体   繁体   中英

Validate two lists together using Stream

Wish to compare, two (object) lists for

  1. Not null
  2. Not empty
  3. Equal Size
  4. Nth Element Field values are same

Possible?

String A = "one,two,three|four,five,six|seven,eight,nine"
String B = "three,six,nine"

List L1 = List.of(A.split("\\|"));
List L2 = List.of(B.split(","));

Give object of List L1, if the third sub value of an element matches with element of the List L2.

Note: This answered the question when it was:

Wish to compare, two (object) lists for
1. Not null
2. Not empty
3. Equal Size
4. Nth Element Field values are same
Possible?

Since then, it was significantly changed...


Seems like you can go with Objects.equals(list1, list2);

  1. When only one of the lists is null, it returns false.

2./3. When the sizes are different, it will return false.

  1. When the elements differ, it will also return false.

In any other case, it will return true.

Disclaimer: This works for the standard Lists in the Collections Framework. There might be other implementations which implement equals() differently (and therefore behave differently when applied to Objects.equals() ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM