java.util.Collections.roundUpToPowerOfTwo()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(14.0k)|赞(0)|评价(0)|浏览(135)

本文整理了Java中java.util.Collections.roundUpToPowerOfTwo()方法的一些代码示例,展示了Collections.roundUpToPowerOfTwo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Collections.roundUpToPowerOfTwo()方法的具体详情如下:
包路径:java.util.Collections
类名称:Collections
方法名:roundUpToPowerOfTwo

Collections.roundUpToPowerOfTwo介绍

[英]Returns the smallest power of two >= its argument, with several caveats: If the argument is negative but not Integer.MIN_VALUE, the method returns zero. If the argument is > 2^30 or equal to Integer.MIN_VALUE, the method returns Integer.MIN_VALUE. If the argument is zero, the method returns zero.
[中]

代码示例

代码示例来源:origin: robovm/robovm

/**
 * Constructs a new {@code Hashtable} using the specified capacity and the
 * default load factor.
 *
 * @param capacity
 *            the initial capacity.
 */
public Hashtable(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashtableEntry<K, V>[] tab = (HashtableEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: robovm/robovm

/**
 * Constructs a new {@code HashMap} instance with the specified capacity.
 *
 * @param capacity
 *            the initial capacity of this hash map.
 * @throws IllegalArgumentException
 *                when the capacity is less than zero.
 */
public HashMap(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashMapEntry<K, V>[] tab = (HashMapEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: robovm/robovm

private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    int capacity = stream.readInt();
    if (capacity < 0) {
      throw new InvalidObjectException("Capacity: " + capacity);
    }
    if (capacity < MINIMUM_CAPACITY) {
      capacity = MINIMUM_CAPACITY;
    } else if (capacity > MAXIMUM_CAPACITY) {
      capacity = MAXIMUM_CAPACITY;
    } else {
      capacity = Collections.roundUpToPowerOfTwo(capacity);
    }
    makeTable(capacity);

    int size = stream.readInt();
    if (size < 0) {
      throw new InvalidObjectException("Size: " + size);
    }

    for (int i = 0; i < size; i++) {
      @SuppressWarnings("unchecked") K key = (K) stream.readObject();
      @SuppressWarnings("unchecked") V val = (V) stream.readObject();
      constructorPut(key, val);
    }
  }
}

代码示例来源:origin: robovm/robovm

private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    int capacity = stream.readInt();
    if (capacity < 0) {
      throw new InvalidObjectException("Capacity: " + capacity);
    }
    if (capacity < MINIMUM_CAPACITY) {
      capacity = MINIMUM_CAPACITY;
    } else if (capacity > MAXIMUM_CAPACITY) {
      capacity = MAXIMUM_CAPACITY;
    } else {
      capacity = Collections.roundUpToPowerOfTwo(capacity);
    }
    makeTable(capacity);

    int size = stream.readInt();
    if (size < 0) {
      throw new InvalidObjectException("Size: " + size);
    }

    init(); // Give subclass (LinkedHashMap) a chance to initialize itself
    for (int i = 0; i < size; i++) {
      @SuppressWarnings("unchecked") K key = (K) stream.readObject();
      @SuppressWarnings("unchecked") V val = (V) stream.readObject();
      constructorPut(key, val);
    }
  }
}

代码示例来源:origin: robovm/robovm

int newCapacity = Collections.roundUpToPowerOfTwo(capacityForInitSize(numMappings));
HashMapEntry<K, V>[] oldTable = table;
int oldCapacity = oldTable.length;

代码示例来源:origin: robovm/robovm

int newCapacity = Collections.roundUpToPowerOfTwo(capacityForInitSize(numMappings));
HashtableEntry<K, V>[] oldTable = table;
int oldCapacity = oldTable.length;

代码示例来源:origin: MobiVM/robovm

/**
 * Constructs a new {@code HashMap} instance with the specified capacity.
 *
 * @param capacity
 *            the initial capacity of this hash map.
 * @throws IllegalArgumentException
 *                when the capacity is less than zero.
 */
public HashMap(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashMapEntry<K, V>[] tab = (HashMapEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: MobiVM/robovm

/**
 * Constructs a new {@code Hashtable} using the specified capacity and the
 * default load factor.
 *
 * @param capacity
 *            the initial capacity.
 */
public Hashtable(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashtableEntry<K, V>[] tab = (HashtableEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: com.jtransc/jtransc-rt

/**
 * Constructs a new {@code Hashtable} using the specified capacity and the
 * default load factor.
 *
 * @param capacity
 *            the initial capacity.
 */
public Hashtable(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashtableEntry<K, V>[] tab = (HashtableEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: com.mobidevelop.robovm/robovm-rt

/**
 * Constructs a new {@code HashMap} instance with the specified capacity.
 *
 * @param capacity
 *            the initial capacity of this hash map.
 * @throws IllegalArgumentException
 *                when the capacity is less than zero.
 */
public HashMap(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashMapEntry<K, V>[] tab = (HashMapEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: ibinti/bugvm

/**
 * Constructs a new {@code HashMap} instance with the specified capacity.
 *
 * @param capacity
 *            the initial capacity of this hash map.
 * @throws IllegalArgumentException
 *                when the capacity is less than zero.
 */
public HashMap(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashMapEntry<K, V>[] tab = (HashMapEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: com.mobidevelop.robovm/robovm-rt

/**
 * Constructs a new {@code Hashtable} using the specified capacity and the
 * default load factor.
 *
 * @param capacity
 *            the initial capacity.
 */
public Hashtable(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashtableEntry<K, V>[] tab = (HashtableEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: FlexoVM/flexovm

/**
 * Constructs a new {@code Hashtable} using the specified capacity and the
 * default load factor.
 *
 * @param capacity
 *            the initial capacity.
 */
public Hashtable(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashtableEntry<K, V>[] tab = (HashtableEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: com.jtransc/jtransc-rt

/**
 * Constructs a new {@code HashMap} instance with the specified capacity.
 *
 * @param capacity
 *            the initial capacity of this hash map.
 * @throws IllegalArgumentException
 *                when the capacity is less than zero.
 */
public HashMap(int capacity) {
  if (capacity < 0) {
    throw new IllegalArgumentException("Capacity: " + capacity);
  }
  if (capacity == 0) {
    @SuppressWarnings("unchecked")
    HashMapEntry<K, V>[] tab = (HashMapEntry<K, V>[]) EMPTY_TABLE;
    table = tab;
    threshold = -1; // Forces first put() to replace EMPTY_TABLE
    return;
  }
  if (capacity < MINIMUM_CAPACITY) {
    capacity = MINIMUM_CAPACITY;
  } else if (capacity > MAXIMUM_CAPACITY) {
    capacity = MAXIMUM_CAPACITY;
  } else {
    capacity = Collections.roundUpToPowerOfTwo(capacity);
  }
  makeTable(capacity);
}

代码示例来源:origin: MobiVM/robovm

private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    int capacity = stream.readInt();
    if (capacity < 0) {
      throw new InvalidObjectException("Capacity: " + capacity);
    }
    if (capacity < MINIMUM_CAPACITY) {
      capacity = MINIMUM_CAPACITY;
    } else if (capacity > MAXIMUM_CAPACITY) {
      capacity = MAXIMUM_CAPACITY;
    } else {
      capacity = Collections.roundUpToPowerOfTwo(capacity);
    }
    makeTable(capacity);

    int size = stream.readInt();
    if (size < 0) {
      throw new InvalidObjectException("Size: " + size);
    }

    for (int i = 0; i < size; i++) {
      @SuppressWarnings("unchecked") K key = (K) stream.readObject();
      @SuppressWarnings("unchecked") V val = (V) stream.readObject();
      constructorPut(key, val);
    }
  }
}

代码示例来源:origin: com.bugvm/bugvm-rt

private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    int capacity = stream.readInt();
    if (capacity < 0) {
      throw new InvalidObjectException("Capacity: " + capacity);
    }
    if (capacity < MINIMUM_CAPACITY) {
      capacity = MINIMUM_CAPACITY;
    } else if (capacity > MAXIMUM_CAPACITY) {
      capacity = MAXIMUM_CAPACITY;
    } else {
      capacity = Collections.roundUpToPowerOfTwo(capacity);
    }
    makeTable(capacity);

    int size = stream.readInt();
    if (size < 0) {
      throw new InvalidObjectException("Size: " + size);
    }

    for (int i = 0; i < size; i++) {
      @SuppressWarnings("unchecked") K key = (K) stream.readObject();
      @SuppressWarnings("unchecked") V val = (V) stream.readObject();
      constructorPut(key, val);
    }
  }
}

代码示例来源:origin: ibinti/bugvm

private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    int capacity = stream.readInt();
    if (capacity < 0) {
      throw new InvalidObjectException("Capacity: " + capacity);
    }
    if (capacity < MINIMUM_CAPACITY) {
      capacity = MINIMUM_CAPACITY;
    } else if (capacity > MAXIMUM_CAPACITY) {
      capacity = MAXIMUM_CAPACITY;
    } else {
      capacity = Collections.roundUpToPowerOfTwo(capacity);
    }
    makeTable(capacity);

    int size = stream.readInt();
    if (size < 0) {
      throw new InvalidObjectException("Size: " + size);
    }

    for (int i = 0; i < size; i++) {
      @SuppressWarnings("unchecked") K key = (K) stream.readObject();
      @SuppressWarnings("unchecked") V val = (V) stream.readObject();
      constructorPut(key, val);
    }
  }
}

代码示例来源:origin: ibinti/bugvm

private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    int capacity = stream.readInt();
    if (capacity < 0) {
      throw new InvalidObjectException("Capacity: " + capacity);
    }
    if (capacity < MINIMUM_CAPACITY) {
      capacity = MINIMUM_CAPACITY;
    } else if (capacity > MAXIMUM_CAPACITY) {
      capacity = MAXIMUM_CAPACITY;
    } else {
      capacity = Collections.roundUpToPowerOfTwo(capacity);
    }
    makeTable(capacity);

    int size = stream.readInt();
    if (size < 0) {
      throw new InvalidObjectException("Size: " + size);
    }

    init(); // Give subclass (LinkedHashMap) a chance to initialize itself
    for (int i = 0; i < size; i++) {
      @SuppressWarnings("unchecked") K key = (K) stream.readObject();
      @SuppressWarnings("unchecked") V val = (V) stream.readObject();
      constructorPut(key, val);
    }
  }
}

代码示例来源:origin: MobiVM/robovm

private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    int capacity = stream.readInt();
    if (capacity < 0) {
      throw new InvalidObjectException("Capacity: " + capacity);
    }
    if (capacity < MINIMUM_CAPACITY) {
      capacity = MINIMUM_CAPACITY;
    } else if (capacity > MAXIMUM_CAPACITY) {
      capacity = MAXIMUM_CAPACITY;
    } else {
      capacity = Collections.roundUpToPowerOfTwo(capacity);
    }
    makeTable(capacity);

    int size = stream.readInt();
    if (size < 0) {
      throw new InvalidObjectException("Size: " + size);
    }

    init(); // Give subclass (LinkedHashMap) a chance to initialize itself
    for (int i = 0; i < size; i++) {
      @SuppressWarnings("unchecked") K key = (K) stream.readObject();
      @SuppressWarnings("unchecked") V val = (V) stream.readObject();
      constructorPut(key, val);
    }
  }
}

代码示例来源:origin: com.gluonhq/robovm-rt

private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    int capacity = stream.readInt();
    if (capacity < 0) {
      throw new InvalidObjectException("Capacity: " + capacity);
    }
    if (capacity < MINIMUM_CAPACITY) {
      capacity = MINIMUM_CAPACITY;
    } else if (capacity > MAXIMUM_CAPACITY) {
      capacity = MAXIMUM_CAPACITY;
    } else {
      capacity = Collections.roundUpToPowerOfTwo(capacity);
    }
    makeTable(capacity);

    int size = stream.readInt();
    if (size < 0) {
      throw new InvalidObjectException("Size: " + size);
    }

    init(); // Give subclass (LinkedHashMap) a chance to initialize itself
    for (int i = 0; i < size; i++) {
      @SuppressWarnings("unchecked") K key = (K) stream.readObject();
      @SuppressWarnings("unchecked") V val = (V) stream.readObject();
      constructorPut(key, val);
    }
  }
}

相关文章

微信公众号

最新文章

更多

Collections类方法