Check if a String is in an ArrayList of Strings

后端 未结 3 913
感情败类
感情败类 2020-12-14 14:31

How can I check if a String is there in the List?

I want to assign 1 to temp if there is a result, 2

3条回答
  •  一生所求
    2020-12-14 14:57

        List list1 = new ArrayList();
        list1.add("one");
        list1.add("three");
        list1.add("four");
    
        List list2 = new ArrayList();
        list2.add("one");
        list2.add("two");
        list2.add("three");
        list2.add("four");
        list2.add("five");
    
    
        list2.stream().filter( x -> !list1.contains(x) ).forEach(x -> System.out.println(x));
    

    The output is:

    two
    five
    

提交回复
热议问题