java判断list集合是否存在某个值

  • Post author:
  • Post category:java


public class Test {
	public static void main(String[] args) {
	List<String> nameList = new ArrayList<String>();
	nameList.add("zhangfei");
	nameList.add("huamulan");
	nameList.add("lubu");
	
		//1. Contains 工具类 ,底层使用for循环去比较
		boolean list = nameList.contains("zhangfei");
		System.out.println(list);
		
		//2.forEach
		boolean list = false;
		for(String name :nameList) {
			if("zhangfei".equals(name)) {
				list = true;
			}
		}
		System.out.println(list);
		
		//3.lambda表达式
		boolean list = nameList.stream().anyMatch(name -> "zhangfei".equals(name));
		System.out.println(list);

		//4.迭代器
		boolean list = false;
		Iterator<String> iterator = nameList.iterator();
		while (iterator.hasNext()) {
			if("zhangfei".equals(iterator.next())) {
				list = true;
			}
		}
		System.out.println(list);
		
		//5.for循环
		boolean list = false;
		Iterator<String> iterator = 
		for(int i = 0; i< nameList.size(); i++) {
			if("zhangfei".equals(nameList.get(i))) {
				list = true;
			}
		}
		System.out.println(list);
	}
}




版权声明:本文为qq_45117337原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。