Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Collections;

import java.util.Map;
import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class Main {
    /**
     * Returns an unmodifiable view of the specified {@code mapSet}. This
     *  method allows modules to provide users with "read-only" access to
     * {@link Map}, but also to the value {@link Set}.
     *
     * @param <K>
     *            the class of the map keys
     * @param <V>
     *            the class of the set values
     * @param mapSet
     *            the {@link Map} of {@link Set} for which an unmodifiable view
     *            is to be returned
     * @return an unmodifiable view of the specified map.
     *
     * @see Collections#unmodifiableMap(Map)
     * @see Collections#unmodifiableSet(Set)
     * @since 4.2
     */
    @Nonnull
    public static <K, V> Map<K, Set<V>> unmodifiableMapSet(@Nullable final Map<K, Set<V>> mapSet) {
        if (mapSet == null) {
            return Collections.emptyMap();
        }

        for (final Map.Entry<K, Set<V>> entry : mapSet.entrySet()) {
            final K key = entry.getKey();
            final Set<V> value = entry.getValue();

            mapSet.replace(key, Collections.unmodifiableSet(value));
        }

        return Collections.unmodifiableMap(mapSet);
    }
}