赞
踩
import lombok.Data; import java.util.ArrayList; import java.util.List; @Data public class MyTest { public String name; public String sex; public MyTest(String name, String sex) { this.name = name; this.sex = sex; } public static void main(String[] args) { List<MyTest> list = new ArrayList(); list.add(new MyTest("小明","男")); list.add(new MyTest("小红","女")); list.add(new MyTest("小丽","女")); System.err.println("==="+list.size()); String param = "小明"; //法1:判断list集合里的属性值是否与传入值相等 for (MyTest test : list) { if (test.getName().equals(param)){ System.err.println(test+"名字重复"); } } //法2:判断list集合里的属性值是否与传入值相等 if (list.size() > 0){ boolean flag = list.stream().anyMatch(obj -> obj.getName().equals(param)); if (flag){ System.err.println("名字重复--"); } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。