Java List Starts with startsWith(List string, List prefix)

Here you can find the source of startsWith(List string, List prefix)

Description

Checks if the list starts with specific elements.

License

Open Source License

Parameter

Parameter Description
T the element type
string the list
prefix the starting elements of the list

Return

true if and only if string starts with prefix , false otherwise

Declaration

public static <T> boolean startsWith(List<T> string, List<T> prefix) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    /**//from w  ww  .ja v  a2 s . c  o  m
     * Checks if the list starts with specific elements.
     *
     * @param <T> the element type
     * @param string the list
     * @param prefix the starting elements of the list
     * @return {@code true} if and only if {@code string} starts with
     * {@code prefix}, {@code false} otherwise
     */
    public static <T> boolean startsWith(List<T> string, List<T> prefix) {
        final int stringSize = string.size();
        final int prefixSize = prefix.size();
        if (prefixSize > stringSize) {
            return false;
        }
        return prefix.equals(string.subList(0, prefixSize));
    }

    /**
     * Check equality in a null-friendly fashion.
     *
     * @param a an object
     * @param b an object to compare with a
     * @return true if and only if a equals b, false otherwise
     */
    public static boolean equals(Object a, Object b) {
        return (a == b) || (a != null && a.equals(b));
    }
}

Related

  1. startsWith(List left, List right, boolean equals)
  2. startsWith(List left, List right, boolean equals)
  3. startsWith(List list0, List list1)
  4. startsWithAny(String s, List prefixes)
  5. startsWithWord(String aTarget, List aExpectWordList)