Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class Main {
    /**
     * add a value to a value list in a Map.
     * 
     * @param map
     *            the map.
     * @param key
     *            the key.
     * @param value
     *            the value to be added to the list.
     * @since 0.1
     */
    public static void addValue(final Map map, final Object key, final Object value) {
        List values;

        if ((values = (List) map.get(key)) == null) {
            values = new ArrayList();
        }

        values.add(value);
        map.put(key, values);
    }

    /**
     * add a value to a value list in a Map that has a limited capacity. If the
     * capacity is exceeded, the oldest entry is discarded.
     * 
     * @param map
     *            the map.
     * @param key
     *            the key.
     * @param value
     *            the value to be added.
     * @param maxEntries
     *            the capacity of the value list.
     * @since 0.6
     */
    public static void addValue(final Map map, final Object key, final Object value, final int maxEntries) {
        List values;

        if ((values = (List) map.get(key)) == null) {
            values = new ArrayList();
        }

        values.add(value);
        while (values.size() > maxEntries) {
            values.remove(0);
        }
        map.put(key, values);
    }
}