java.util.HashMap.init()方法的使用及代码示例

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

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

HashMap.init介绍

[英]This method is called from the pseudo-constructors (clone and readObject) prior to invoking constructorPut/constructorPutAll, which invoke the overridden constructorNewEntry method. Normally it is a VERY bad idea to invoke an overridden method from a pseudo-constructor (Effective Java Item 17). In this case it is unavoidable, and the init method provides a workaround.
[中]在调用constructorPut/constructorPutAll(调用重写的constructorNewEntry方法)之前,从伪构造函数(clone和readObject)调用此方法。通常,从伪构造函数调用重写的方法是一个非常糟糕的主意(有效的Java项目17)。在这种情况下,这是不可避免的,init方法提供了一种解决方法。

代码示例

代码示例来源: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

/**
 * Returns a shallow copy of this map.
 *
 * @return a shallow copy of this map.
 */
@SuppressWarnings("unchecked")
@Override public Object clone() {
  /*
   * This could be made more efficient. It unnecessarily hashes all of
   * the elements in the map.
   */
  HashMap<K, V> result;
  try {
    result = (HashMap<K, V>) super.clone();
  } catch (CloneNotSupportedException e) {
    throw new AssertionError(e);
  }
  // Restore clone to empty state, retaining our capacity and threshold
  result.makeTable(table.length);
  result.entryForNullKey = null;
  result.size = 0;
  result.keySet = null;
  result.entrySet = null;
  result.values = null;
  result.init(); // Give subclass a chance to initialize itself
  result.constructorPutAll(this); // Calls method overridden in subclass!!
  return result;
}

代码示例来源:origin: org.apidesign.bck2brwsr/emul

/**
 * Constructs an empty <tt>HashMap</tt> with the default initial capacity
 * (16) and the default load factor (0.75).
 */
public HashMap() {
  this.loadFactor = DEFAULT_LOAD_FACTOR;
  threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
  table = new Entry[DEFAULT_INITIAL_CAPACITY];
  init();
}

代码示例来源:origin: jtulach/bck2brwsr

/**
 * Constructs an empty <tt>HashMap</tt> with the default initial capacity
 * (16) and the default load factor (0.75).
 */
public HashMap() {
  this.loadFactor = DEFAULT_LOAD_FACTOR;
  threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
  table = new Entry[DEFAULT_INITIAL_CAPACITY];
  init();
}

代码示例来源:origin: jtulach/bck2brwsr

/**
 * Constructs an empty <tt>HashMap</tt> with the specified initial
 * capacity and load factor.
 *
 * @param  initialCapacity the initial capacity
 * @param  loadFactor      the load factor
 * @throws IllegalArgumentException if the initial capacity is negative
 *         or the load factor is nonpositive
 */
public HashMap(int initialCapacity, float loadFactor) {
  if (initialCapacity < 0)
    throw new IllegalArgumentException("Illegal initial capacity: " +
                      initialCapacity);
  if (initialCapacity > MAXIMUM_CAPACITY)
    initialCapacity = MAXIMUM_CAPACITY;
  if (loadFactor <= 0 || Float.isNaN(loadFactor))
    throw new IllegalArgumentException("Illegal load factor: " +
                      loadFactor);
  // Find a power of 2 >= initialCapacity
  int capacity = 1;
  while (capacity < initialCapacity)
    capacity <<= 1;
  this.loadFactor = loadFactor;
  threshold = (int)(capacity * loadFactor);
  table = new Entry[capacity];
  init();
}

代码示例来源:origin: org.apidesign.bck2brwsr/emul

/**
 * Constructs an empty <tt>HashMap</tt> with the specified initial
 * capacity and load factor.
 *
 * @param  initialCapacity the initial capacity
 * @param  loadFactor      the load factor
 * @throws IllegalArgumentException if the initial capacity is negative
 *         or the load factor is nonpositive
 */
public HashMap(int initialCapacity, float loadFactor) {
  if (initialCapacity < 0)
    throw new IllegalArgumentException("Illegal initial capacity: " +
                      initialCapacity);
  if (initialCapacity > MAXIMUM_CAPACITY)
    initialCapacity = MAXIMUM_CAPACITY;
  if (loadFactor <= 0 || Float.isNaN(loadFactor))
    throw new IllegalArgumentException("Illegal load factor: " +
                      loadFactor);
  // Find a power of 2 >= initialCapacity
  int capacity = 1;
  while (capacity < initialCapacity)
    capacity <<= 1;
  this.loadFactor = loadFactor;
  threshold = (int)(capacity * loadFactor);
  table = new Entry[capacity];
  init();
}

代码示例来源:origin: com.mobidevelop.robovm/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);
    }
  }
}

代码示例来源: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: 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: 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);
    }
  }
}

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

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.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);
    }

    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: org.apidesign.bck2brwsr/emul

/**
 * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and
 * values themselves are not cloned.
 *
 * @return a shallow copy of this map
 */
public Object clone() {
  HashMap<K,V> result = null;
  try {
    result = (HashMap<K,V>)super.clone();
  } catch (CloneNotSupportedException e) {
    // assert false;
  }
  result.table = new Entry[table.length];
  result.entrySet = null;
  result.modCount = 0;
  result.size = 0;
  result.init();
  result.putAllForCreate(this);
  return result;
}

代码示例来源:origin: jtulach/bck2brwsr

/**
 * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and
 * values themselves are not cloned.
 *
 * @return a shallow copy of this map
 */
public Object clone() {
  HashMap<K,V> result = null;
  try {
    result = (HashMap<K,V>)super.clone();
  } catch (CloneNotSupportedException e) {
    // assert false;
  }
  result.table = new Entry[table.length];
  result.entrySet = null;
  result.modCount = 0;
  result.size = 0;
  result.init();
  result.putAllForCreate(this);
  return result;
}

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

/**
 * Returns a shallow copy of this map.
 *
 * @return a shallow copy of this map.
 */
@SuppressWarnings("unchecked")
@Override public Object clone() {
  /*
   * This could be made more efficient. It unnecessarily hashes all of
   * the elements in the map.
   */
  HashMap<K, V> result;
  try {
    result = (HashMap<K, V>) super.clone();
  } catch (CloneNotSupportedException e) {
    throw new AssertionError(e);
  }
  // Restore clone to empty state, retaining our capacity and threshold
  result.makeTable(table.length);
  result.entryForNullKey = null;
  result.size = 0;
  result.keySet = null;
  result.entrySet = null;
  result.values = null;
  result.init(); // Give subclass a chance to initialize itself
  result.constructorPutAll(this); // Calls method overridden in subclass!!
  return result;
}

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

/**
 * Returns a shallow copy of this map.
 *
 * @return a shallow copy of this map.
 */
@SuppressWarnings("unchecked")
@Override public Object clone() {
  /*
   * This could be made more efficient. It unnecessarily hashes all of
   * the elements in the map.
   */
  HashMap<K, V> result;
  try {
    result = (HashMap<K, V>) super.clone();
  } catch (CloneNotSupportedException e) {
    throw new AssertionError(e);
  }
  // Restore clone to empty state, retaining our capacity and threshold
  result.makeTable(table.length);
  result.entryForNullKey = null;
  result.size = 0;
  result.keySet = null;
  result.entrySet = null;
  result.values = null;
  result.init(); // Give subclass a chance to initialize itself
  result.constructorPutAll(this); // Calls method overridden in subclass!!
  return result;
}

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

/**
 * Returns a shallow copy of this map.
 *
 * @return a shallow copy of this map.
 */
@SuppressWarnings("unchecked")
@Override public Object clone() {
  /*
   * This could be made more efficient. It unnecessarily hashes all of
   * the elements in the map.
   */
  HashMap<K, V> result;
  try {
    result = (HashMap<K, V>) super.clone();
  } catch (CloneNotSupportedException e) {
    throw new AssertionError(e);
  }
  // Restore clone to empty state, retaining our capacity and threshold
  result.makeTable(table.length);
  result.entryForNullKey = null;
  result.size = 0;
  result.keySet = null;
  result.entrySet = null;
  result.values = null;
  result.init(); // Give subclass a chance to initialize itself
  result.constructorPutAll(this); // Calls method overridden in subclass!!
  return result;
}

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

/**
 * Returns a shallow copy of this map.
 *
 * @return a shallow copy of this map.
 */
@SuppressWarnings("unchecked")
@Override public Object clone() {
  /*
   * This could be made more efficient. It unnecessarily hashes all of
   * the elements in the map.
   */
  HashMap<K, V> result;
  try {
    result = (HashMap<K, V>) super.clone();
  } catch (CloneNotSupportedException e) {
    throw new AssertionError(e);
  }
  // Restore clone to empty state, retaining our capacity and threshold
  result.makeTable(table.length);
  result.entryForNullKey = null;
  result.size = 0;
  result.keySet = null;
  result.entrySet = null;
  result.values = null;
  result.init(); // Give subclass a chance to initialize itself
  result.constructorPutAll(this); // Calls method overridden in subclass!!
  return result;
}

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

/**
 * Returns a shallow copy of this map.
 *
 * @return a shallow copy of this map.
 */
@SuppressWarnings("unchecked")
@Override public Object clone() {
  /*
   * This could be made more efficient. It unnecessarily hashes all of
   * the elements in the map.
   */
  HashMap<K, V> result;
  try {
    result = (HashMap<K, V>) super.clone();
  } catch (CloneNotSupportedException e) {
    throw new AssertionError(e);
  }
  // Restore clone to empty state, retaining our capacity and threshold
  result.makeTable(table.length);
  result.entryForNullKey = null;
  result.size = 0;
  result.keySet = null;
  result.entrySet = null;
  result.values = null;
  result.init(); // Give subclass a chance to initialize itself
  result.constructorPutAll(this); // Calls method overridden in subclass!!
  return result;
}

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

/**
 * Returns a shallow copy of this map.
 *
 * @return a shallow copy of this map.
 */
@SuppressWarnings("unchecked")
@Override public Object clone() {
  /*
   * This could be made more efficient. It unnecessarily hashes all of
   * the elements in the map.
   */
  HashMap<K, V> result;
  try {
    result = (HashMap<K, V>) super.clone();
  } catch (CloneNotSupportedException e) {
    throw new AssertionError(e);
  }
  // Restore clone to empty state, retaining our capacity and threshold
  result.makeTable(table.length);
  result.entryForNullKey = null;
  result.size = 0;
  result.keySet = null;
  result.entrySet = null;
  result.values = null;
  result.init(); // Give subclass a chance to initialize itself
  result.constructorPutAll(this); // Calls method overridden in subclass!!
  return result;
}

相关文章