工作总结(一)

需求: 判断一个id是否在一个以逗号隔开的字符串中

ex: “1,3,55,2,33”

判断 3是否在这串字符串中

第一个任务时的写法:

	// 首先从数据库中查询出id
	String[] split = propValue.split(",");
            String id = //数据库中查出;
            for (String s : split) {
                if (s.equals(id)) {
                    // xxxxx
                    break;
                }
            }

第二个任务时的写法:

boolean flag = Arrays.stream(secIds.split(",")).anyMatch(secId -> secId.equals("数据库中查询出的id"));
if(flag) {
   // xxxx
}

第三个任务的写法:

 boolean flag = Arrays.asList(secIds.split(",")).contains(secId);
 if(flag) {
   // xxxx
 }

// 不知道还有没有其他写法

原文地址:https://www.cnblogs.com/ring2/p/13883765.html