src/share/classes/java/util/Collections.java

Print this page




 907                         itr.set(newVal);
 908                         result = true;
 909                     }
 910                 }
 911             } else {
 912                 for (int i=0; i<size; i++) {
 913                     if (oldVal.equals(itr.next())) {
 914                         itr.set(newVal);
 915                         result = true;
 916                     }
 917                 }
 918             }
 919         }
 920         return result;
 921     }
 922 
 923     /**
 924      * Returns the starting position of the first occurrence of the specified
 925      * target list within the specified source list, or -1 if there is no
 926      * such occurrence.  More formally, returns the lowest index <tt>i</tt>
 927      * such that <tt>source.subList(i, i+target.size()).equals(target)</tt>,
 928      * or -1 if there is no such index.  (Returns -1 if
 929      * <tt>target.size() > source.size()</tt>.)
 930      *
 931      * <p>This implementation uses the "brute force" technique of scanning
 932      * over the source list, looking for a match with the target at each
 933      * location in turn.
 934      *
 935      * @param source the list in which to search for the first occurrence
 936      *        of <tt>target</tt>.
 937      * @param target the list to search for as a subList of <tt>source</tt>.
 938      * @return the starting position of the first occurrence of the specified
 939      *         target list within the specified source list, or -1 if there
 940      *         is no such occurrence.
 941      * @since  1.4
 942      */
 943     public static int indexOfSubList(List<?> source, List<?> target) {
 944         int sourceSize = source.size();
 945         int targetSize = target.size();
 946         int maxCandidate = sourceSize - targetSize;
 947 
 948         if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
 949             (source instanceof RandomAccess&&target instanceof RandomAccess)) {


 960             for (int candidate = 0; candidate <= maxCandidate; candidate++) {
 961                 ListIterator<?> ti = target.listIterator();
 962                 for (int i=0; i<targetSize; i++) {
 963                     if (!eq(ti.next(), si.next())) {
 964                         // Back up source iterator to next candidate
 965                         for (int j=0; j<i; j++)
 966                             si.previous();
 967                         continue nextCand;
 968                     }
 969                 }
 970                 return candidate;
 971             }
 972         }
 973         return -1;  // No candidate matched the target
 974     }
 975 
 976     /**
 977      * Returns the starting position of the last occurrence of the specified
 978      * target list within the specified source list, or -1 if there is no such
 979      * occurrence.  More formally, returns the highest index <tt>i</tt>
 980      * such that <tt>source.subList(i, i+target.size()).equals(target)</tt>,
 981      * or -1 if there is no such index.  (Returns -1 if
 982      * <tt>target.size() > source.size()</tt>.)
 983      *
 984      * <p>This implementation uses the "brute force" technique of iterating
 985      * over the source list, looking for a match with the target at each
 986      * location in turn.
 987      *
 988      * @param source the list in which to search for the last occurrence
 989      *        of <tt>target</tt>.
 990      * @param target the list to search for as a subList of <tt>source</tt>.
 991      * @return the starting position of the last occurrence of the specified
 992      *         target list within the specified source list, or -1 if there
 993      *         is no such occurrence.
 994      * @since  1.4
 995      */
 996     public static int lastIndexOfSubList(List<?> source, List<?> target) {
 997         int sourceSize = source.size();
 998         int targetSize = target.size();
 999         int maxCandidate = sourceSize - targetSize;
1000 
1001         if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
1002             source instanceof RandomAccess) {   // Index access version




 907                         itr.set(newVal);
 908                         result = true;
 909                     }
 910                 }
 911             } else {
 912                 for (int i=0; i<size; i++) {
 913                     if (oldVal.equals(itr.next())) {
 914                         itr.set(newVal);
 915                         result = true;
 916                     }
 917                 }
 918             }
 919         }
 920         return result;
 921     }
 922 
 923     /**
 924      * Returns the starting position of the first occurrence of the specified
 925      * target list within the specified source list, or -1 if there is no
 926      * such occurrence.  More formally, returns the lowest index <tt>i</tt>
 927      * such that {@code source.subList(i, i+target.size()).equals(target)},
 928      * or -1 if there is no such index.  (Returns -1 if
 929      * {@code target.size() > source.size()})
 930      *
 931      * <p>This implementation uses the "brute force" technique of scanning
 932      * over the source list, looking for a match with the target at each
 933      * location in turn.
 934      *
 935      * @param source the list in which to search for the first occurrence
 936      *        of <tt>target</tt>.
 937      * @param target the list to search for as a subList of <tt>source</tt>.
 938      * @return the starting position of the first occurrence of the specified
 939      *         target list within the specified source list, or -1 if there
 940      *         is no such occurrence.
 941      * @since  1.4
 942      */
 943     public static int indexOfSubList(List<?> source, List<?> target) {
 944         int sourceSize = source.size();
 945         int targetSize = target.size();
 946         int maxCandidate = sourceSize - targetSize;
 947 
 948         if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
 949             (source instanceof RandomAccess&&target instanceof RandomAccess)) {


 960             for (int candidate = 0; candidate <= maxCandidate; candidate++) {
 961                 ListIterator<?> ti = target.listIterator();
 962                 for (int i=0; i<targetSize; i++) {
 963                     if (!eq(ti.next(), si.next())) {
 964                         // Back up source iterator to next candidate
 965                         for (int j=0; j<i; j++)
 966                             si.previous();
 967                         continue nextCand;
 968                     }
 969                 }
 970                 return candidate;
 971             }
 972         }
 973         return -1;  // No candidate matched the target
 974     }
 975 
 976     /**
 977      * Returns the starting position of the last occurrence of the specified
 978      * target list within the specified source list, or -1 if there is no such
 979      * occurrence.  More formally, returns the highest index <tt>i</tt>
 980      * such that {@code source.subList(i, i+target.size()).equals(target)},
 981      * or -1 if there is no such index.  (Returns -1 if
 982      * {@code target.size() > source.size()})
 983      *
 984      * <p>This implementation uses the "brute force" technique of iterating
 985      * over the source list, looking for a match with the target at each
 986      * location in turn.
 987      *
 988      * @param source the list in which to search for the last occurrence
 989      *        of <tt>target</tt>.
 990      * @param target the list to search for as a subList of <tt>source</tt>.
 991      * @return the starting position of the last occurrence of the specified
 992      *         target list within the specified source list, or -1 if there
 993      *         is no such occurrence.
 994      * @since  1.4
 995      */
 996     public static int lastIndexOfSubList(List<?> source, List<?> target) {
 997         int sourceSize = source.size();
 998         int targetSize = target.size();
 999         int maxCandidate = sourceSize - targetSize;
1000 
1001         if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
1002             source instanceof RandomAccess) {   // Index access version