Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

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

import java.util.RandomAccess;

public class Main {
    public static <E> void swapAll(final List<E> list) {
        if (list instanceof RandomAccess) {
            for (int i = 0, j = list.size() - 1; i < j; i++, j--) {
                list.set(i, list.set(j, list.get(i)));
            }
        } else {
            final ListIterator<E> iteratorI = list.listIterator();
            final ListIterator<E> iteratorJ = list.listIterator(list.size());
            E tmp;

            while (iteratorI.nextIndex() < iteratorJ.nextIndex()) {
                tmp = iteratorI.next();
                iteratorI.set(iteratorJ.previous());
                iteratorJ.set(tmp);
            }
        }
    }

    public static <E> void swapAll(final List<E> list, final int off, final int len) {
        if (list instanceof RandomAccess) {
            final int last = off + len - 1;
            for (int i = off, j = last; i < j; i++, j--) {
                list.set(i, list.set(j, list.get(i)));
            }
        } else {
            final int end = off + len;
            final ListIterator<E> iteratorI = list.listIterator(off);
            final ListIterator<E> iteratorJ = list.listIterator(end);
            E tmp;

            while (iteratorI.nextIndex() < iteratorJ.nextIndex()) {
                tmp = iteratorI.next();
                iteratorI.set(iteratorJ.previous());
                iteratorJ.set(tmp);
            }
        }
    }

    public static <V> void get(final Map<?, ? extends V> map, final Collection<? extends Object> keys,
            final Collection<? super V> values) {
        for (final Object key : keys) {
            if (!map.containsKey(key)) {
                continue;
            }

            final V value = map.get(key);
            values.add(value);
        }
    }

    public static <V> void get(final Map<?, ? extends V> map, final Collection<? super V> values,
            final Object... keys) {
        for (final Object key2 : keys) {
            final Object key = key2;
            if (!map.containsKey(key)) {
                continue;
            }

            final V value = map.get(key);
            values.add(value);
        }
    }
}