如何从Collection中返回N个连续的元素?
我传递了一个对象集合(在我的例子中是一些 Contact 类),并且需要从该集合返回一个页面。 我的代码感觉比它需要的要长得多。我是否缺少一些库,这些库可以比像下面这样一次迭代每个元素更优雅地执行此操作?
protected Collection<Contact> getPageOfContacts(
Collection<Contact> contacts, int pageIndex, int pageSize) {
if (pageIndex < 0 || pageSize <= 0
|| pageSize > contacts.size()) {
return contacts;
}
int firstElement = pageIndex * pageSize;
int lastElement = (pageIndex + 1) * pageSize - 1;
Collection<Contact> pagedContacts = new ArrayList<Contact>();
int index = -1;
for (Contact contact : contacts) {
index++;
if (index < firstElement) {
continue;
}
if (index > lastElement) {
break;
}
pagedContacts.add(contact);
}
return pagedContacts;
}
I am passed a collection of objects (some Contact class in my case) and need to return a page from that collection.
My code feels much longer than it needs to be. Am I missing some libraries that could perform that more elegantly than iterating over each element one at a time like I do below?
protected Collection<Contact> getPageOfContacts(
Collection<Contact> contacts, int pageIndex, int pageSize) {
if (pageIndex < 0 || pageSize <= 0
|| pageSize > contacts.size()) {
return contacts;
}
int firstElement = pageIndex * pageSize;
int lastElement = (pageIndex + 1) * pageSize - 1;
Collection<Contact> pagedContacts = new ArrayList<Contact>();
int index = -1;
for (Contact contact : contacts) {
index++;
if (index < firstElement) {
continue;
}
if (index > lastElement) {
break;
}
pagedContacts.add(contact);
}
return pagedContacts;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 Guava Iterables.partition:
更复杂的版本不会创建所有页面来选择正确的页面,而是在找到正确的页面时停止。
更新:
感谢 ColinD 指出:
是一个更简单的实现。
You could use Guava Iterables.partition:
A more complex version does not create all pages to pick the right one, but stops when the right page is found.
Update:
Thanks to ColinD to point out that:
is a simpler implementation.
如果您可以要求将数据分页为
List
,则可以使用 Guava:这不涉及复制或迭代(它使用原始列表的子列表视图),并透明地处理少于
pageSize
元素的最终页面。对于任意
Iterable
或Collection
,我会这样做:通过提供这两种方法,您将能够处理已知在编译时列出的对象 -尽可能高效地节省时间和任何其他类型的
Iterable
。If you can require the data to be paged to be a
List
, you can get a sublist view of a single page easily using Guava:This involves no copying or iteration (it uses sublist views of the original list) and handles a final page that has fewer than
pageSize
elements transparently.For an arbitrary
Iterable
orCollection
, I'd do this:By providing both these methods, you'd be able to handle objects that are known to be lists at compile-time efficiently and any other type of
Iterable
as efficiently as you can.如果您希望元素有明确的顺序,则应该使用
List
,而不是collection
。List
和Collection
之间的基本区别在于List
的元素有固定的顺序。它还定义了非常方便的方法subList(int start, int end)
,它创建一个子列表,它是原始列表的别名,仅包含您想要的元素,而无需将它们复制到新列表。If you want a defined order to your elements, you should be using a
List
, not acollection
. The basic difference betweenList
andCollection
is thatList
has a fixed order to the elements. It also defines the very convenient methodsubList(int start, int end)
which creates a sub-list which is an alias of the original list containing just the elements you want without the overhead of copying them to a new list.List 接口提供了一个 subList 方法,该方法采用起始索引和结束索引。请参阅 http://download .oracle.com/javase/6/docs/api/java/util/List.html#subList(int,%20int)。返回的子列表由原始列表支持,因此您可能想要执行某些操作喜欢
The List interface provides a subList method, that takes a start index and an end index. See http://download.oracle.com/javase/6/docs/api/java/util/List.html#subList(int,%20int). The returned sublist is backed by the original list, so you probably want to do something like
注意:这将返回子列表 exclusive lastElement
注意 2:由于 Kevin 提到的原因,结果被复制到另一个列表。
Note: this will return the sublist exclusive lastElement
Note 2: The result are copied to another list for the reasons mentioned by Kevin.