Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8299524

Class ArrayList enhance indexOfRange and lastIndexOfRange

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Java: 19; OpenJDK 64-Bit Server VM 19+36-2238
      Runtime: OpenJDK Runtime Environment 19+36-2238
      System: Windows 11 version 10.0 running on amd64; UTF-8; en_US (nb)

      A DESCRIPTION OF THE PROBLEM :
      if we have a class like this
      public class SomeClass {
      String string_property;
      @Override
          public boolean equals(Object o) {
              if (o instanceof String) {
      //First If Statement
                  String external_string_value = (String)o;
                  return string_property.equalsIgnoreCase(external_string_value);
              }
      if (o instanceof SomeClass) {
      //Second If Statement
      return string_property.equalsIgnoreCase(((SomeClass)o).string_property);
      }
      return false;
      }
      }

      and ArrayList like this

      ArrayList<SomeClass> some_class_list = new ArrayList<>();
      //Add some runtime objects
      //some_class_list.add()

      When I try to search inside ArrayList using methods like contains()
      some_class_list.contains("select some object where search string equals SomeClass.string_property");//returns false since First if Statement will never ever be called
      some_class_list.contains(some_class_object);//return true ssince Second If Statement

      Why contains method returns false, it becuase of these lines of code inside indexOfRange and lastIndexOfRange
      if (o.equals(es[i])) {
      return i;
      }

      this lines of code needs a small modification to allow Java primitive objects to be used in conjunction between Object.equals() and ArrayList.contains()
      if (es[i] != null && es[i].equals(o)) {
      return i;
      }

      now Java ArrayList has the freedom to pass any object kind to perform matching against.

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: