Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Arrays;

import java.util.Iterator;
import java.util.List;

import java.util.function.Predicate;

public class Main {
    /**
     * Generates array and sorts equally.
     * 
     * @param <T>
     *            Class for array where you get random item.
     * @param array
     *            Refers to array to be created
     * @param equally
     *            sorts equally
     * @return The actual array is created and returned
     */
    public static <T> int timesFound(T[] array, T equally) {
        return timesFound(Arrays.asList(array), equally);
    }

    /**
     * Generates a random number and returns outcome of for loop.
     * 
     * @param <T>
     *            Class for list where you get random item.
     * @param list
     *            Refers to item list
     * @param equally
     *            A random number
     * @return returns outcome of for loop
     */
    public static <T> int timesFound(List<T> list, T equally) {
        int ret = 0;
        for (T item : list) {
            if (item.equals(equally)) {
                ++ret;
            }
        }
        return ret;
    }

    /**
     * Lists and filters data in array and returns with applied filter.
     * 
     * @param <T>
     *            Class for array where you get random item.
     * @param array
     *            Lists data in array.
     * @param filter
     *            Applies filter to array.
     * @return Array is returned with applied filter.
     */
    public static <T> int timesFound(T[] array, Predicate<T> filter) {
        return (int) Arrays.stream(array).filter(filter).count();
    }

    /**
     * Draws Premade list and returns filtered list.
     * 
     * @param <T>
     *            Class for list where you get random item.
     * @param list
     *            Draws data from premade list.
     * @param filter
     *            Represents a boolean from the predicate statement.
     * @return Filtered list.
     */
    public static <T> int timesFound(List<T> list, Predicate<T> filter) {
        return (int) list.stream().filter(filter).count();
    }

    /**
     * Draws data and returns items in array list.
     * 
     * @param list
     *            Draws data from premade list.
     * @param filter
     *            Filters data to lower case.
     * @return Returns items in the form of an array list.
     */
    public static String[] filter(String[] list, String filter) {
        String[] items = new String[0];
        for (String item : list) {
            if (item.toLowerCase().contains(filter.toLowerCase())) {
                items = Arrays.copyOf(items, items.length + 1);
                items[items.length - 1] = item;
            }
        }
        return items;
    }

    /**
     * Returns itrator and filters for lower case.
     * 
     * @param <T>
     *            Class for list where you get random item.
     * @param list
     *            Returns and itrator over the elements of T.
     * @param filter
     *            Filters for lower case.
     */
    public static <T extends Iterable<String>> void filter(T list, String filter) {
        Iterator<? extends String> strI = list.iterator();
        while (strI.hasNext()) {
            String str = strI.next();
            if (!str.toLowerCase().contains(filter.toLowerCase())) {
                strI.remove();
            }
        }
    }

    /**
     * Selects specific items from list and returns outcome of boolean.
     * 
     * @param <T>
     *            Class for list where you get random item.
     * @param list
     *            Items in list
     * @param item
     *            specific item selected from list
     * @return returns outcome of boolean
     */
    public static <T> boolean contains(T[] list, T item) {
        boolean b = false;
        for (T listItem : list) {
            if (listItem.equals(item)) {
                b = true;
                break;
            }
        }
        return b;
    }
}