# Java - 检查`LinkedList`中是否存在特定元素

> 原文： [https://beginnersbook.com/2014/07/java-check-if-a-particular-element-exists-in-linkedlist-example/](https://beginnersbook.com/2014/07/java-check-if-a-particular-element-exists-in-linkedlist-example/)

在这个例子中，我们将看到如何使用[`contains()`方法](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#contains(java.lang.Object))检查[`LinkedList`](https://beginnersbook.com/2013/12/linkedlist-in-java-with-example/)中是否存在特定元素：

`public boolean contains(Object o)`：如果此列表包含指定的元素，则返回`true`。更正式地，当且仅当此列表包含至少一个元素`e`时才返回`true`（`o == null? e == null: o.equals(e)`）。

```java
import java.util.LinkedList;

public class CheckLinkedList {
 public static void main(String[] args) {

    // Creating LinkedList of String Elements
    LinkedList<String> linkedlist = new LinkedList<String>();

    // Populating it with String values
    linkedlist.add("AA");
    linkedlist.add("BB");
    linkedlist.add("CC");
    linkedlist.add("DD");
    linkedlist.add("EE");

    // contains() method checks whether the element exists
    if (linkedlist.contains("CC")) {
       System.out.println("Element CC is present in List");
    } else {
       System.out.println("List doesn't have element CC");
     }

    //Checking for element FF
    if (linkedlist.contains("FF")) {
       System.out.println("Element FF is present in List");
    } else {
        System.out.println("List doesn't have element FF");
      }
 }
}
```

输出：

```java
Element CC is present in List
List doesn't have element FF
```