访问树集中的元素

发布于 2024-11-05 06:36:35 字数 281 浏览 1 评论 0原文

我在这里尝试一些非常基本的java,但遇到了一些令人头疼的问题。本质上,我需要将文件中的某些元素读入某种类型的数组或列表中,对它们进行排序,消除重复项,然后返回前三个元素。 TreeSet 似乎非常适合,因为它可以进行排序并删除重复项。我的问题是我对如何仅返回前三个元素感到困惑。迭代器似乎贯穿整个集合。使用手动迭代器创建一个 while 循环来包含一个保存迭代器循环的 while 循环似乎令人困惑并且不太可能成功。这里的答案是我需要迭代树集并将每个元素放入数组列表中,以便我可以访问前三个元素吗?我的意思是,这似乎可行,但看起来非常复杂。尖端?

I am attempting some very basic java here and have reached a bit of a head scratcher. Essentially,I need to read some element from a file into some type of array or list, sort them, eliminate duplicates, and then return the first three elements. TreeSet seemed like the perfect fit in so much as it does the sort and kills the duplicates. My issue is that I am confounded as to how to return only the first three elements. The iterator seems to run all the way through the set. Creating a while loop with a manual iterator to contain a while loop that holds the iterator loops seems confusing and unlikely to be successful. Is the answer here that I need to iterate through the treeset and place each element into an arraylist so that I can then access the first three elements? I mean, it seems that this would work but it seems highly convoluted. Tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肤浅与狂妄 2024-11-12 06:36:35

使用 Guava 你可以这样做

return Lists.newArrayList(Iterables.limit(treeSet, 3));

Using Guava you could just do

return Lists.newArrayList(Iterables.limit(treeSet, 3));
望喜 2024-11-12 06:36:35

嗯。显而易见的事情有什么问题吗?

ArrayList<MyType> buffer = new ArrayList<MyType>(3); 

for( MyType elt: myTreeSet ) {

    buffer.add(elt);
    if( buffer.size() == 3 ) break;
}

或者

ArrayList<MyType> buffer = new ArrayList<MyType>(3);
Iterator<MyType> iter = myTreeSet.iterator();

while( iter.hasNext() && buffer.size() < 3 ) buffer.add(iter.next());

如果您更喜欢“脱糖”版本?

Hm. What's wrong with the obvious?

ArrayList<MyType> buffer = new ArrayList<MyType>(3); 

for( MyType elt: myTreeSet ) {

    buffer.add(elt);
    if( buffer.size() == 3 ) break;
}

Or

ArrayList<MyType> buffer = new ArrayList<MyType>(3);
Iterator<MyType> iter = myTreeSet.iterator();

while( iter.hasNext() && buffer.size() < 3 ) buffer.add(iter.next());

if you prefer the "desugared" version?

记忆之渊 2024-11-12 06:36:35

字符串示例:

TreeSet<String> treeSet = new TreeSet<String>(); 

// you populate treeSet with data

String[] stringArray = new String[NUMBER_OF_NEEDED_RECORDS];
for(int i =0; i < NUMBER_OF_NEEDED_RECORDS; i++) {
    stringArray[i] = treeSet.pollFirst();
}

Example with Strings:

TreeSet<String> treeSet = new TreeSet<String>(); 

// you populate treeSet with data

String[] stringArray = new String[NUMBER_OF_NEEDED_RECORDS];
for(int i =0; i < NUMBER_OF_NEEDED_RECORDS; i++) {
    stringArray[i] = treeSet.pollFirst();
}
木格 2024-11-12 06:36:35

我会使用(希望您使用 Java 1.6):

Arrays.copyOf(myTreeSet.toArray(), Math.min(3, myTreeset.size()));

编辑:使用我添加的 Math.min() 大小进行防弹

I would use (expecting you use Java 1.6):

Arrays.copyOf(myTreeSet.toArray(), Math.min(3, myTreeset.size()));

Edit: to be bulletproof with the size I added Math.min()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文