Skip to content

Commit 284c051

Browse files
cwangg897fmbenhassine
authored andcommitted
Improve indexOf() performance in DefaultFieldSet
Replaced linear List#indexOf with a precomputed Map-based index lookup This reduces the time complexity from O(n) to O(1), improving performance for field lookups in wide CSV files. This change preserves existing behavior and error messages Resolves: #4930 Signed-off-by: Choi Wang Gyu <dhkdrb897@gmail.com> (cherry picked from commit 480a343)
1 parent 9ed1796 commit 284c051

File tree

1 file changed

+13
-5
lines changed
  • spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform

1 file changed

+13
-5
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DefaultFieldSet.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2024 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,9 +25,10 @@
2525
import java.util.Arrays;
2626
import java.util.Date;
2727
import java.util.List;
28+
import java.util.HashMap;
2829
import java.util.Locale;
2930
import java.util.Properties;
30-
31+
import java.util.Map;
3132
import org.springframework.lang.Nullable;
3233
import org.springframework.util.Assert;
3334
import org.springframework.util.StringUtils;
@@ -40,6 +41,7 @@
4041
* @author Rob Harrop
4142
* @author Dave Syer
4243
* @author Mahmoud Ben Hassine
44+
* @author Choi Wang Gyu
4345
*/
4446
public class DefaultFieldSet implements FieldSet {
4547

@@ -60,6 +62,8 @@ public class DefaultFieldSet implements FieldSet {
6062

6163
private List<String> names;
6264

65+
private Map<String, Integer> nameIndexMap;
66+
6367
/**
6468
* The {@link NumberFormat} to use for parsing numbers. If unset the {@link Locale#US}
6569
* will be used ('.' as decimal place).
@@ -137,6 +141,10 @@ public DefaultFieldSet(String[] tokens, String[] names, @Nullable DateFormat dat
137141
}
138142
this.tokens = tokens.clone();
139143
this.names = Arrays.asList(names);
144+
this.nameIndexMap = new HashMap<>(names.length);
145+
for (int i = 0; i < names.length; i++) {
146+
this.nameIndexMap.put(names[i], i);
147+
}
140148
setDateFormat(dateFormat == null ? getDefaultDateFormat() : dateFormat);
141149
setNumberFormat(numberFormat == null ? getDefaultNumberFormat() : numberFormat);
142150
}
@@ -450,11 +458,11 @@ protected String readAndTrim(int index) {
450458
* @throws IllegalArgumentException if a column with given name is not defined.
451459
*/
452460
protected int indexOf(String name) {
453-
if (names == null) {
461+
if (nameIndexMap == null) {
454462
throw new IllegalArgumentException("Cannot access columns by name without meta data");
455463
}
456-
int index = names.indexOf(name);
457-
if (index >= 0) {
464+
Integer index = nameIndexMap.get(name);
465+
if (index != null) {
458466
return index;
459467
}
460468
throw new IllegalArgumentException("Cannot access column [" + name + "] from " + names);

0 commit comments

Comments
 (0)