From 783cb2c438716304769fb3861078e6e6fabf1a95 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 7 May 2015 16:02:48 +0200 Subject: [PATCH] StringUtils.commaDelimitedListToSet/removeDuplicateStrings preserves original order Issue: SPR-12003 --- .../java/org/springframework/util/StringUtils.java | 13 +++++++------ .../org/springframework/util/StringUtilsTests.java | 6 +++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index bad42bfbae..fda4d2a16a 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Locale; @@ -29,7 +30,6 @@ import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; import java.util.TimeZone; -import java.util.TreeSet; /** * Miscellaneous {@link String} utility methods. @@ -880,7 +880,7 @@ public abstract class StringUtils { /** * Remove duplicate strings from the given array. - *

Also sorts the array, as it uses a {@link TreeSet}. + *

As of 4.2, it preserves the original order, as it uses a {@link LinkedHashSet}. * @param array the {@code String} array * @return an array without duplicates, in natural sort order */ @@ -888,7 +888,7 @@ public abstract class StringUtils { if (ObjectUtils.isEmpty(array)) { return array; } - Set set = new TreeSet(); + Set set = new LinkedHashSet(); for (String element : array) { set.add(element); } @@ -1098,13 +1098,14 @@ public abstract class StringUtils { /** * Convert a comma delimited list (e.g., a row from a CSV file) into a set. - *

Note that this will suppress duplicates, and the elements in the - * returned set will be sorted, since a {@link TreeSet} is used internally. + *

Note that this will suppress duplicates, and as of 4.2, the elements in + * the returned set will preserve the original order in a {@link LinkedHashSet}. * @param str the input {@code String} * @return a set of {@code String} entries in the list + * @see #removeDuplicateStrings(String[]) */ public static Set commaDelimitedListToSet(String str) { - Set set = new TreeSet(); + Set set = new LinkedHashSet(); String[] tokens = commaDelimitedListToStringArray(str); for (String token : tokens) { set.add(token); diff --git a/spring-core/src/test/java/org/springframework/util/StringUtilsTests.java b/spring-core/src/test/java/org/springframework/util/StringUtilsTests.java index be40ac2d7c..22e979e455 100644 --- a/spring-core/src/test/java/org/springframework/util/StringUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/StringUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -419,8 +419,8 @@ public class StringUtilsTests { public void testRemoveDuplicateStrings() { String[] input = new String[] {"myString2", "myString1", "myString2"}; input = StringUtils.removeDuplicateStrings(input); - assertEquals("myString1", input[0]); - assertEquals("myString2", input[1]); + assertEquals("myString2", input[0]); + assertEquals("myString1", input[1]); } @Test