is List Monotonous - Java java.util

Java examples for java.util:List Operation

Description

is List Monotonous

Demo Code


//package com.java2s;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List aList = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(isMonotonous(aList));
    }//from  w w  w.  j  a v a2s.  c om

    public static boolean isMonotonous(List<Integer> aList) {
        return aList.equals(sort(aList))
                || aList.equals(reverse(sort(aList)));
    }

    public static List<Integer> sort(List<Integer> aList) {
        List<Integer> tmp = new LinkedList<>(aList);
        Collections.sort(tmp);
        return tmp;
    }

    public static List<Integer> reverse(List<Integer> aList) {
        List<Integer> tmp = new LinkedList<>(aList);
        Collections.reverse(tmp);
        return tmp;
    }
}

Related Tutorials