net.i2p.util

## Class ObjectCounterUnsafe<K>

- java.lang.Object
- - net.i2p.util.ObjectCounterUnsafe<K>

- Type Parameters:
    `K` - type of objects being counted

      public class ObjectCounterUnsafe<K>
      extends Object

  Non-thread-safe counter for tracking occurrences of objects.

  This class provides a lightweight alternative to ObjectCounter for single-threaded scenarios. It offers better performance with reduced object churn by using primitive integers instead of AtomicInteger.

  Key differences from ObjectCounter:

  - NOT thread-safe - use only in single-threaded contexts
  - Lower memory overhead and better performance
  - Additional add() method for incrementing by arbitrary values
  - sortedObjects() method for retrieving objects by count

  Typical use cases include UI components and Sybil attack detection where thread safety is not required.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ObjectCounterUnsafe()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `add(K h, int val)` Add a value
    `void` | `clear()` Start over.
    `void` | `clear(K h)` Reset the count for this key to zero
    `int` | `count(K h)`
    `int` | `increment(K h)` Add one.
    `Set<K>` | `objects()`
    `List<K>` | `sortedObjects()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ObjectCounterUnsafe

          public ObjectCounterUnsafe()

  - ### Method Detail

    - #### add

          public int add(K h,
                         int val)

      Add a value
      Returns:
        count after adding

    - #### clear

          public void clear()

      Start over. Reset the count for all keys to zero.

    - #### clear

          public void clear(K h)

      Reset the count for this key to zero

    - #### count

          public int count(K h)

      Returns:
        current count

    - #### increment

          public int increment(K h)

      Add one.
      Returns:
        count after increment

    - #### objects

          public Set<K> objects()

      Returns:
        set of objects with counts > 0

    - #### sortedObjects

          public List<K> sortedObjects()

      Returns:
        list of objects reverse sorted by count, highest to lowest