Find out whether all the elements in a List are equal. - Java java.util

Java examples for java.util:List Element

Description

Find out whether all the elements in a List are equal.

Demo Code


//package com.book2s;

import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(allEquals(list));
    }//ww  w.j  a  va 2 s  .c  o m

    /**
     * Find out whether all the elements in a List are equal.
     * 
     * @param <T>
     * @param list
     * @return
     */
    public static <T> boolean allEquals(List<T> list) {
        if (list == null || list.size() <= 0)
            return false;

        for (int i = 0; i < list.size() - 1; i++) {
            T t_i = list.get(i);
            T t_j = list.get(i + 1);
            if (!t_i.equals(t_j))
                return false;
        }

        return true;
    }
}

Related Tutorials