Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.LinkedHashMap;

import java.util.Map;

public class Main {

    public static <K, V> LinkedHashMap<K, V> headMap(LinkedHashMap<K, V> map, int endIndex) {
        return headMap(map, endIndex, false);
    }

    public static <K, V> LinkedHashMap<K, V> headMap(LinkedHashMap<K, V> map, int endIndex, boolean inclusive) {
        int index = endIndex;
        if (inclusive) {
            index++;
        }
        if ((map == null) || (map.size() == 0) || (map.size() < index) || (endIndex < 0)) {
            return new LinkedHashMap<K, V>(0);
        }
        LinkedHashMap<K, V> subMap = new LinkedHashMap<K, V>(index + 1);
        int i = 0;
        for (Map.Entry<K, V> entry : map.entrySet()) {
            if (i < index) {
                subMap.put(entry.getKey(), entry.getValue());
            }
            i++;
        }
        return subMap;
    }
}