Utility like Arrays.asList() - Android java.lang

Android examples for java.lang:array convert

Description

Utility like Arrays.asList()

Demo Code

/*//w  w  w  . j av a2 s  . com
 *******************************************************************************
 * Copyright (C) 1996-2015, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
import java.util.HashMap;
import java.util.Map;

public class Main {
  /**
   * Utility like Arrays.asList()
   * 
   * @param source
   * @param target
   * @param reverse
   * @param <T>
   * @return
   */
  public static <T> Map<T, T> asMap(T[][] source, Map<T, T> target,
      boolean reverse) {
    int from = 0, to = 1;
    if (reverse) {
      from = 1;
      to = 0;
    }
    for (int i = 0; i < source.length; ++i) {
      target.put(source[i][from], source[i][to]);
    }
    return target;
  }

  /**
   * @param <T>
   * @param source
   * @return
   */
  public static <T> Map<T, T> asMap(T[][] source) {
    return asMap(source, new HashMap<T, T>(), false);
  }
}

Related Tutorials