手写一个泛型接口和是实现类

package com.study.service;

import java.util.Iterator;

/**
 * @Author:Liku
 * @Date:2022/12/3-17:19
 * @Name:JavaSE
 * @Since:jdk1.8
 * @Description:
 */
public interface DynamicArray<E> {
    /**
     * size
     *
     * @return
     */
    int size();

    /**
     * assert it's empty
     *
     * @return
     */
    boolean isEmpty();

    /**
     * judge whether or not have e
     *
     * @param e
     * @return
     */
    boolean contains(E e);

    /**
     * get iterator
     *
     * @return iterator
     */
    Iterator<E> iterator();

    /**
     * @return array
     */
    Object[] toArray();


    /**
     * add element
     *
     * @param e
     * @return
     */
    boolean add(E e);

    /**
     * 指定位置插入
     *
     * @param index
     * @param e
     * @return
     */
    boolean add(int index, E e);

    /**
     * delete element
     *
     * @param e
     * @return
     */
    boolean remove(E e);

    /**
     * remove first elements
     *
     * @return
     */
    boolean remove();

    /**
     * remove element at specified the position
     *
     * @param index
     * @return
     */
    boolean remove(int index);

    /**
     * used to compare elements
     */
    void sort();

    /**
     * remove all elements from array
     */
    void clear();

    /**
     * compare the specified element with another varies
     *
     * @param e
     * @return
     */
    boolean equal(E e);

    /**
     * get hashcode
     *
     * @return hashcode
     */
    int hashcodes();

    /**
     * @param index
     * @return the elements at the specified position
     */
    E get(int index);

    /**
     * @param e
     * @return element's index of the first occurrence
     */
    int indexOf(E e);

    /**
     * for-each
     */
//    void foreach();

    String toString();
}

对应的实现类:

package com.study;

import com.study.service.DynamicArray;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @Author:Liku
 * @Date:2022/12/3-11:02
 * @Name:JavaSE
 * @Since:jdk1.8
 * @Description:
 */
public class Demo {
    public static void main(String[] args) {
        DynamicArray<String> strings=new DynamicArrayImpl<>(10);
        strings.add("hello");
        strings.add(0,"liku,");
        System.out.println(strings);
    }


    private static <T> List<T> subList1(List<T> list, int startIndex, int offIndex) {
        return list.subList(startIndex, offIndex);
    }


    private static void demo4() {
        List<String> list = Arrays.asList("12", "liku", "nak", "hello", "world");
        List<String> list1 = subList1(list, 1, 4);
        System.out.println(list1);

    }

    private static <T> void show(T[] array) {
        for (T t1 : array) {
            System.out.print(t1 + "  ");
        }
    }

    private static <T> T[] add(T[] array, T t) {
        array = Arrays.copyOf(array, array.length + 1);
        array[array.length - 1] = t;
        return array;
    }

    private static void demo3() {
        String[] strings = {"12", "liku", "nak", "hello", "world"};
        add(strings, "你好");
        show(strings);

    }

    private static void demo2() {
        List<String> list = new ArrayList<>();
        Collections.addAll(list, "sad", "asdfr", "asf", "as", "a34", "liku", "egf", "sad", "asdfr", "asf", "as", "a34", "liku", "egf");
        List<String> a = list.stream().limit(10).filter(s -> s.startsWith("a")).distinct().collect(Collectors.toList());
        System.out.println(a);
        List<Product> productList = new ArrayList<>(10);
        Collections.addAll(productList, new Product(23, "薯片", 35.0, 4, 23),
                new Product(21, "薯片", 356, 4, 77),
                new Product(3, "薯片", 38, 4, 52),
                new Product(14, "薯片", 39, 4, 22),
                new Product(10, "薯片", 40.0, 4, 88));
        Map<String, Double> collect = (productList).parallelStream().limit(3).collect(Collectors.toMap(Product::getName, Product::getPrice, (product1, product2) -> product2));
        System.out.println(collect);
        Map<Product, Double> collect1 = productList.parallelStream().filter(product -> product.getStore() > 50).map(product ->
        {
            product.setPrice(55);
            return product;
        }).peek(product -> product.getDiscount()).
                sorted((produt1, product2) -> product2.getId().compareTo(produt1.getId())).
                collect(Collectors.toMap(Function.identity(), Product::getPrice, (product1, product2) -> product2));
        collect1.forEach((k, v) -> System.out.println(k + "  " + v));
    }

    private static void demo1() {
        List<String> list = Arrays.asList("sad", "asdfr", "asf", "as");
        List<String> list1 = Arrays.asList("34", "liku", "egf");
        //Collections.copy(list,list1);
        Collections.copy(list1, list);
        System.out.println(list);
        System.out.println(list1);
    }
}
posted @ 2022-12-03 18:57  Liku007  阅读(33)  评论(0编辑  收藏  举报