apache/druid · error · ClassCastException

Cannot remove non Integer from integer BitSet

Error message

Cannot remove non Integer from integer BitSet

What it means

IntegerSet only stores Integer elements; remove(Object) performs an instanceof check and throws ClassCastException when the argument is any other type. Because Set.remove takes Object, the compiler cannot catch wrong-type removals, so this runtime guard enforces the element type.

Source

Thrown at processing/src/main/java/org/apache/druid/collections/IntegerSet.java:112

    }
    boolean isSet = mutableBitmap.get(integer);
    mutableBitmap.add(integer.intValue());
    return !isSet;
  }

  @Override
  public boolean remove(Object o)
  {
    if (o == null) {
      throw new NullPointerException("BitSet cannot contain null values");
    }
    if (o instanceof Integer) {
      Integer integer = (Integer) o;
      boolean isSet = mutableBitmap.get(integer);
      mutableBitmap.remove(integer);
      return isSet;
    } else {
      throw new ClassCastException("Cannot remove non Integer from integer BitSet");
    }
  }

  @Override
  public boolean containsAll(Collection<?> c)
  {
    Iterator<?> it = c.iterator();
    while (it.hasNext()) {
      if (!this.contains(it.next())) {
        return false;
      }
    }
    return true;
  }

  @Override
  public boolean addAll(Collection<? extends Integer> c)
  {

View on GitHub (pinned to 9b90983fd2)

Solutions

  1. Ensure the argument is boxed Integer: convert with ((Number) value).intValue() before removing
  2. Convert collections of other numeric types: list.stream().map(Number::intValue).collect(Collectors.toList())
  3. Fix generic types so the collection is typed List<Integer>/Set<Integer>, letting the compiler catch mismatches
  4. If Long values are the norm, switch to a bitmap/collection that supports longs

Example fix

// before
set.remove(longValue); // ClassCastException
// after
set.remove(Integer.valueOf(longValue.intValue()));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(o instanceof Integer)) { o = ((Number) o).intValue(); }
set.remove(o);

Type guard

static Integer toInteger(Object o) { return (o instanceof Integer) ? (Integer) o : (o instanceof Number ? ((Number) o).intValue() : null); }

Try / catch

try { set.remove(o); } catch (ClassCastException e) { /* convert numeric types before removing */ }

Prevention

When it happens

Trigger: Calling remove() with a non-Integer object such as a Long, String, or primitive-boxed other type (e.g. remove(Long.valueOf(5))), or removeAll() with a collection of Longs that looks like ints but boxes to Long.

Common situations: Mixing numeric types: data read from JSON/protobuf arrives as Long, or a List<Long> of IDs is passed to an IntegerSet API; also raw-typed collections losing generic type safety.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/a45214bdc5e9f1b4. Report an issue: GitHub.