java 判断两个list中的对象是否完全一致
List<Object> list2;
需要比较list1是否完全包含list2;
所谓的包含,就是list1中的对象与list2完全一致(对象的属性也必须一致);
尽量不用双重for循环...在线等,求大神指教,谢谢! 展开
使用java判断两个list中的对象是否完全一致的代码如下:
public class Test {public static void main(String[] args)
{List<Integer> a = Arrays.asList(1, 2, 3, 4)
List<Integer> b = Arrays.asList(4, 3, 2, 1)System.out.println(compare(a, b))
public static <T extends Comparable<T>> boolean compare(List<T> a, List<T> b) {if (a.size() != b.size())
return falseCollections.sort(a)Collections.sort(b)for (int i = 0; i < a.size(); i++) {if (!a.get(i).equals(b.get(i)))return false;}return true;}}
使用java判断是否有新增数据的代码如下:
public List<MonitoringFlight> isSaveOrUpdate
(List<MonitoringFlight> oldList, List<MonitoringFlight> newList)
{<br datafiltered="filtered">List<MonitoringFlight>
monitoringFlights = new ArrayList<MonitoringFlight>()
<br data-filtered="filtered">for (int i = 0; i < oldList.size(); i++)
{<br data-filtered="filtered"> for (int j = 0; j < newList.size(); j++)
{<br data-filtered="filtered">//判断是否有新增<br datafiltered="filtered">
if(oldList.get(i).getId().equals(newList.get(j).getId()))
{<br data-filtered="filtered">//判断是否有更新<br data-filtered="filtered">if()
{<br data-filtered="filtered"><br data-filtered="filtered">}else
{<br data-filtered="filtered"><br data-filtered="filtered">}
<br data-filtered="filtered"><br data-filtered="filtered">}
else{<br data-filtered="filtered">//有新增<br data-filtered="filtered">
<br data-filtered="filtered">}<br data-filtered="filtered">}
<br data-filtered="filtered">}<br data-filtered="filtered">
<br data-filtered="filtered"><br data-filtered="filtered"><br datafiltered="filtered">
return monitoringFlights;<br data-filtered="filtered">}
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> a = Arrays.asList(1, 2, 3, 4);
List<Integer> b = Arrays.asList(4, 3, 2, 1);
System.out.println(compare(a, b));
}
public static <T extends Comparable<T>> boolean compare(List<T> a, List<T> b) {
if (a.size() != b.size())
return false;
Collections.sort(a);
Collections.sort(b);
for (int i = 0; i < a.size(); i++) {
if (!a.get(i).equals(b.get(i)))
return false;
}
return true;
}
}
楼主说的LIST集合是对象类型,需要比较的还有属性
重写实体类的equals方法
集合A,B分别调用ContainsAll()方法