Java Code Examples for com.helger.commons.collection.impl.CommonsLinkedHashMap#put()

The following examples show how to use com.helger.commons.collection.impl.CommonsLinkedHashMap#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static <ELEMENTTYPE> CommonsLinkedHashMap <ELEMENTTYPE, ELEMENTTYPE> newOrderedMap (@Nullable final ELEMENTTYPE... aValues)
{
  if (ArrayHelper.isEmpty (aValues))
    return newOrderedMap (0);

  if ((aValues.length % 2) != 0)
    throw new IllegalArgumentException ("The passed array needs an even number of elements!");

  final CommonsLinkedHashMap <ELEMENTTYPE, ELEMENTTYPE> ret = newOrderedMap (aValues.length / 2);
  for (int i = 0; i < aValues.length; i += 2)
    ret.put (aValues[i], aValues[i + 1]);
  return ret;
}
 
Example 2
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve a map that is ordered in the way the parameter arrays are passed
 * in. Note that key and value arrays need to have the same length.
 *
 * @param <KEYTYPE>
 *        The key type.
 * @param <VALUETYPE>
 *        The value type.
 * @param aKeys
 *        The key array to use. May not be <code>null</code>.
 * @param aValues
 *        The value array to use. May not be <code>null</code>.
 * @return A {@link CommonsLinkedHashMap} containing the passed key-value
 *         entries. Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> CommonsLinkedHashMap <KEYTYPE, VALUETYPE> newOrderedMap (@Nullable final KEYTYPE [] aKeys,
                                                                                            @Nullable final VALUETYPE [] aValues)
{
  final int nKeys = ArrayHelper.getSize (aKeys);
  final int nValues = ArrayHelper.getSize (aValues);

  // Check for identical size
  if (nKeys != nValues)
    throw new IllegalArgumentException ("The passed arrays have different length (" +
                                        nKeys +
                                        " keys and " +
                                        nValues +
                                        " values)!");

  // Are both empty?
  if (nKeys == 0)
    return newOrderedMap (0);

  final CommonsLinkedHashMap <KEYTYPE, VALUETYPE> ret = newOrderedMap (nKeys);
  for (int i = 0; i < aKeys.length; ++i)
    ret.put (aKeys[i], aValues[i]);
  return ret;
}
 
Example 3
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> CommonsLinkedHashMap <KEYTYPE, VALUETYPE> newOrderedMap (@Nullable final KEYTYPE aKey,
                                                                                            @Nullable final VALUETYPE aValue)
{
  final CommonsLinkedHashMap <KEYTYPE, VALUETYPE> ret = newOrderedMap (1);
  ret.put (aKey, aValue);
  return ret;
}
 
Example 4
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> CommonsLinkedHashMap <KEYTYPE, VALUETYPE> newOrderedMap (@Nullable final Collection <? extends KEYTYPE> aKeys,
                                                                                            @Nullable final Collection <? extends VALUETYPE> aValues)
{
  final int nKeys = getSize (aKeys);
  final int nValues = getSize (aValues);

  // Check for identical size
  if (nKeys != nValues)
    throw new IllegalArgumentException ("The passed arrays have different length (" +
                                        nKeys +
                                        " keys and " +
                                        nValues +
                                        " values)!");

  // Are both empty?
  if (nKeys == 0)
    return newOrderedMap (0);

  final CommonsLinkedHashMap <KEYTYPE, VALUETYPE> ret = newOrderedMap (nKeys);
  final Iterator <? extends KEYTYPE> itk = aKeys.iterator ();
  final Iterator <? extends VALUETYPE> itv = aValues.iterator ();
  while (itk.hasNext ())
    ret.put (itk.next (), itv.next ());
  return ret;
}