对字符串的迭代器进行排序 [英] Sort an iterator of strings

查看:157
本文介绍了对字符串的迭代器进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串迭代器。

I have an iterator of strings.

为了排序我需要从中创建一个列表并使用 Collections.sort(list)对其进行排序)

For sorting i need to create a list from it and sort it using Collections.sort(list).

有没有简单的方法来对迭代器进行排序。

Is there any simple way to sort an iterator.

推荐答案

Iterator不是一个容器,它是一个用于遍历容器元素的实用程序。
因此,如果您只能访问Iterator,则无法更改此迭代器的创建者定义的迭代顺序。

An Iterator is NOT a container, it is a utility for traversing over the elements of a container. So if you only have access to the Iterator there is no way to change the order of iteration which is defined by the creator of this iterator.

如果您无法更改原始容器,您必须在新集合中收集迭代器提供的元素并在其中对它们进行排序。

If you can't change the original container, you'll have to gather the elements delivered by the iterator within a new Collection and sort them therein.

(理解的一个好方法)迭代器的可能性是查看JDK类的源代码或实现自己的迭代器)

(A good approach to understand what is possible with iterators is to have a look at the Source-code of the JDK classes or to implement an own iterator)

这篇关于对字符串的迭代器进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆