如何有效地小写集合中的每个元素?
将列表或集合中每个元素小写的最有效方法是什么?
我对列表的想法:
final List<String> strings = new ArrayList<String>();
strings.add("HELLO");
strings.add("WORLD");
for(int i=0,l=strings.size();i<l;++i)
{
strings.add(strings.remove(0).toLowerCase());
}
有更好、更快的方法吗?这个例子对于 Set 来说会是什么样子?由于目前没有方法对 Set(或 List)的每个元素应用操作,是否可以在不创建额外的临时 Set 的情况下完成?
像这样的东西会很好:
Set<String> strings = new HashSet<String>();
strings.apply(
function (element)
{ this.replace(element, element.toLowerCase();) }
);
谢谢,
What's the most efficient way to lower case every element of a List or Set?
My idea for a List:
final List<String> strings = new ArrayList<String>();
strings.add("HELLO");
strings.add("WORLD");
for(int i=0,l=strings.size();i<l;++i)
{
strings.add(strings.remove(0).toLowerCase());
}
Is there a better, faster way? How would this example look like for a Set? As there is currently no method for applying an operation to each element of a Set (or List) can it be done without creating an additional temporary Set?
Something like this would be nice:
Set<String> strings = new HashSet<String>();
strings.apply(
function (element)
{ this.replace(element, element.toLowerCase();) }
);
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
另一种解决方案,但使用 Java 8 及更高版本:
Yet another solution, but with Java 8 and above:
这似乎是一个相当干净的列表解决方案。它应该允许使用特定的 List 实现来提供对于线性时间内的列表遍历和恒定时间内的字符串替换都是最佳的实现。
这是我能想到的最好的套装。正如其他人所说,由于多种原因,该操作无法在集合中就地执行。小写字符串可能需要放置在集合中与其替换的字符串不同的位置。此外,如果小写字符串与已添加的另一个小写字符串相同(例如,“HELLO”和“Hello”都将产生“hello”,则该小写字符串可能根本不会添加到集合中)只能添加到集合中一次)。
This seems like a fairly clean solution for lists. It should allow for the particular List implementation being used to provide an implementation that is optimal for both the traversal of the list--in linear time--and the replacing of the string--in constant time.
This is the best that I can come up with for sets. As others have said, the operation cannot be performed in-place in the set for a number of reasons. The lower-case string may need to be placed in a different location in the set than the string it is replacing. Moreover, the lower-case string may not be added to the set at all if it is identical to another lower-case string that has already been added (e.g., "HELLO" and "Hello" will both yield "hello", which will only be added to the set once).
您可以使用 Google Collections 执行此操作:
You can do this with Google Collections:
如果您愿意更改输入列表,这里还有另一种实现它的方法。
strings.replaceAll(String::toLowerCase)
If you are fine with changing the input list here is one more way to achieve it.
strings.replaceAll(String::toLowerCase)
好吧,由于两个事实,没有真正优雅的解决方案:
渐近地讲,您无法获得比当前方法更好的运行时间。您必须使用 toLowerCase() 创建一个新字符串,并且您需要自己迭代列表并生成每个新的小写字符串,并将其替换为现有字符串。
Well, there is no real elegant solution due to two facts:
String
s in Java are immutablemap(f, list)
function as you have in functional languages.Asymptotically speaking, you can't get a better run time than your current method. You will have to create a new string using
toLowerCase()
and you will need to iterate by yourself over the list and generate each new lower-case string, replacing it with the existing one.这可能更快:
This is probably faster:
尝试 Commons Collections 中的 CollectionUtils#transform 以获得就地解决方案,或 番石榴中的 Collections2#transform如果您需要实时取景。
Try CollectionUtils#transform in Commons Collections for an in-place solution, or Collections2#transform in Guava if you need a live view.
我不认为如果将字符串更改为集合,就可以就地进行操作(无需创建另一个集合)。这是因为您只能使用迭代器或 foreach 循环来迭代 Set,并且不能在这样做时插入新对象(它会引发异常)
I don't believe it is possible to do the manipulation in place (without creating another Collection) if you change strings to be a Set. This is because you can only iterate over the Set using an iterator or a for each loop, and cannot insert new objects whilst doing so (it throws an exception)
参考已接受的(Matthew T. Staebler)解决方案中的 ListIterator 方法。使用 ListIterator 比这里的方法有什么更好的地方?
Referring to the ListIterator method in the accepted (Matthew T. Staebler's) solution. How is using the ListIterator better than the method here?
我一直在寻找类似的东西,但被困住了,因为我的 ArrayList 对象没有声明为 GENERIC 并且它可以作为原始 List 从某处输入对象。我刚刚得到一个 ArrayList 对象“_products”。因此,我所做的如下所述,它对我来说非常有效 ::
也就是说,我首先使用可用的 _products 并制作了一个 GENERIC 列表对象(正如我得到的那样)仅字符串相同),然后我在列表元素上应用了 toLowerCase() 方法,该方法之前由于非通用 ArrayList 对象而不起作用。
我们这里使用的方法toLowerCase()属于String类。
不属于 ArrayList 或 Object 类。
如有错误请指正。 JAVA新手求指导。 :)
I was looking for similar stuff, but was stuck because my ArrayList object was not declared as GENERIC and it was available as raw List type object from somewhere. I was just getting an ArrayList object "_products". So, what I did is mentioned below and it worked for me perfectly ::
That is, I first took my available _products and made a GENERIC list object (As I were getting only strings in same) then I applied the toLowerCase() method on list elements which was not working previously because of non-generic ArrayList object.
And the method toLowerCase() we are using here is of String class.
not of ArrayList or Object class.
Please correct if m wrong. Newbie in JAVA seeks guidance. :)
使用JAVA 8并行流变得更快
Using JAVA 8 parallel stream it becomes faster
使用Java8流来表示集合中每个元素的小写。
输出:
Have used Java8 streams for lowercase of every element in a Collection.
Output:
对于大写
val originalList = arrayListOf("apple", "banana", "cherry")
val upperCaseList = originalList.map { it.toUpperCase() }
对于小写
>val originalList = arrayListOf("苹果", "香蕉", "樱桃")
val lowerCaseList = originalList.map { it.toLowerCase() }
For UppperCase
val originalList = arrayListOf("apple", "banana", "cherry")
val upperCaseList = originalList.map { it.toUpperCase() }
For LowerCase
val originalList = arrayListOf("Apple", "Banana", "Cherry")
val lowerCaseList = originalList.map { it.toLowerCase() }