Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I see two inefficiencies in your code:

  • You call .indexIn() twice in quick succession. You should have saved the return value from the first call.
  • You create a substring with each iteration. Starting with Java 7 Starting with Java 7, that would be a performance problem, since each substring is a copy of part of the original string.

Also, it would be no additional work for you to generalize the method to accept any CharSequence.

public static SortedSet<Integer> findWrappingIndices(CharSequence s) {
 SortedSet<Integer> indices = Sets.newTreeSet();
 for (int i = -1; -1 != (i = WRAP_CHAR_MATCHER.indexIn(s, i + 1)); ) {
 indices.add(i); 
 }
 return indices;
}

Another option, using just the built-in Java library, would be to use a regex, with [- ] as the pattern.

I see two inefficiencies in your code:

  • You call .indexIn() twice in quick succession. You should have saved the return value from the first call.
  • You create a substring with each iteration. Starting with Java 7, that would be a performance problem, since each substring is a copy of part of the original string.

Also, it would be no additional work for you to generalize the method to accept any CharSequence.

public static SortedSet<Integer> findWrappingIndices(CharSequence s) {
 SortedSet<Integer> indices = Sets.newTreeSet();
 for (int i = -1; -1 != (i = WRAP_CHAR_MATCHER.indexIn(s, i + 1)); ) {
 indices.add(i); 
 }
 return indices;
}

Another option, using just the built-in Java library, would be to use a regex, with [- ] as the pattern.

I see two inefficiencies in your code:

  • You call .indexIn() twice in quick succession. You should have saved the return value from the first call.
  • You create a substring with each iteration. Starting with Java 7, that would be a performance problem, since each substring is a copy of part of the original string.

Also, it would be no additional work for you to generalize the method to accept any CharSequence.

public static SortedSet<Integer> findWrappingIndices(CharSequence s) {
 SortedSet<Integer> indices = Sets.newTreeSet();
 for (int i = -1; -1 != (i = WRAP_CHAR_MATCHER.indexIn(s, i + 1)); ) {
 indices.add(i); 
 }
 return indices;
}

Another option, using just the built-in Java library, would be to use a regex, with [- ] as the pattern.

added 110 characters in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

I see two inefficiencies in your code:

  • You call .indexIn() twice in quick succession. You should have saved the return value from the first call.
  • You create a substring with each iteration. Starting with Java 7, that would be a performance problem, since each substring is a copy of part of the original string.

Also, it would be no additional work for you to generalize the method to accept any CharSequence.

public static SortedSet<Integer> findWrappingIndices(CharSequence s) {
 SortedSet<Integer> indices = Sets.newTreeSet();
 for (int i = -1; -1 != (i = WRAP_CHAR_MATCHER.indexIn(s, i + 1)); ) {
 indices.add(i); 
 }
 return indices;
}

Another option, using just the built-in Java library, would be to use a regex, with [- ] as the pattern.

I see two inefficiencies in your code:

  • You call .indexIn() twice in quick succession. You should have saved the return value from the first call.
  • You create a substring with each iteration. Starting with Java 7, that would be a performance problem, since each substring is a copy of part of the original string.

Also, it would be no additional work for you to generalize the method to accept any CharSequence.

public static SortedSet<Integer> findWrappingIndices(CharSequence s) {
 SortedSet<Integer> indices = Sets.newTreeSet();
 for (int i = -1; -1 != (i = WRAP_CHAR_MATCHER.indexIn(s, i + 1)); ) {
 indices.add(i); 
 }
 return indices;
}

I see two inefficiencies in your code:

  • You call .indexIn() twice in quick succession. You should have saved the return value from the first call.
  • You create a substring with each iteration. Starting with Java 7, that would be a performance problem, since each substring is a copy of part of the original string.

Also, it would be no additional work for you to generalize the method to accept any CharSequence.

public static SortedSet<Integer> findWrappingIndices(CharSequence s) {
 SortedSet<Integer> indices = Sets.newTreeSet();
 for (int i = -1; -1 != (i = WRAP_CHAR_MATCHER.indexIn(s, i + 1)); ) {
 indices.add(i); 
 }
 return indices;
}

Another option, using just the built-in Java library, would be to use a regex, with [- ] as the pattern.

Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

I see two inefficiencies in your code:

  • You call .indexIn() twice in quick succession. You should have saved the return value from the first call.
  • You create a substring with each iteration. Starting with Java 7, that would be a performance problem, since each substring is a copy of part of the original string.

Also, it would be no additional work for you to generalize the method to accept any CharSequence.

public static SortedSet<Integer> findWrappingIndices(CharSequence s) {
 SortedSet<Integer> indices = Sets.newTreeSet();
 for (int i = -1; -1 != (i = WRAP_CHAR_MATCHER.indexIn(s, i + 1)); ) {
 indices.add(i); 
 }
 return indices;
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /