Home  >  Article  >  Java  >  Java determines whether list contains a certain value

Java determines whether list contains a certain value

王林
王林Original
2019-11-18 13:26:3211172browse

Java determines whether list contains a certain value

contains method is used to determine whether the specified element is included in the list. Returns true if the specified element is contained in the list, false otherwise.

Syntax:

contains(Object o);

o: To determine whether an element exists in the list.

Specific usage examples: traverse list data and filter out data with the same time

try {
    List list = new ArrayList();
    List equipmentLocationList = locationService.getUserTrajectoryList();
    List list2 = new ArrayList();
    for (Location location : equipmentLocationList) {
        // 过滤时间相同的数据
        if (list2.contains(location.getDeviceTime().getTime())) {
            continue;
        }
        EcgUser user = new EcgUser();
        user.setLatitude(location.getLatitude());
        user.setLongitude(location.getLongitude());
        list.add(user);
        list2.add(location.getDeviceTime().getTime());
    }
    System.out.println(list2);
    return getResponse(list);
} catch (Exception e) {
    logger.error("", e);
    return getResponse(ErrorCodeConstant.FAIL.getCode());
}

Recommended tutorial: Java tutorial

The above is the detailed content of Java determines whether list contains a certain value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn