as Read Only List via AbstractList - Java java.util

Java examples for java.util:List Element

Description

as Read Only List via AbstractList

Demo Code


//package com.book2s;
import java.util.AbstractList;

import java.util.Collections;
import java.util.List;

public class Main {
    public static <T> List<T> asReadOnlyList(final T[] array) {
        if (array.length == 0)
            return Collections.emptyList();

        return new AbstractList<T>() {
            @Override/*from   w  w w.j  ava  2 s  .co  m*/
            public T get(int index) {
                return array[index];
            }

            @Override
            public int size() {
                return array.length;
            }
        };
    }
}

Related Tutorials