java 如何从 Iterable<String> 打印出单个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9947221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print out individual Strings from Iterable<String>
提问by user1303750
I am having trouble printing out individual Strings from Interable object. I have this given function prefixMatch(String someword) that returns Iterable (that keeps a LinkedList of Strings, I think). I have tried turning it into a list but it wont work. Dose anyone know how to get the Strings out of it one by one?
我无法从 Interable 对象打印出单个字符串。我有这个给定的函数 prefixMatch(String someword) 返回 Iterable (我认为它保留了一个字符串的 LinkedList )。我试过把它变成一个列表,但它不起作用。有谁知道如何将字符串一一取出?
tst is a Ternary search tree
tst 是一个三元搜索树
Iterable<String> words = tst.prefixMatch(word);
回答by Colin Hebert
回答by dty
for (String s: words) {
System.out.println(s);
}
回答by Bajal
The java 8 way, using forEach
:
java 8 方式,使用forEach
:
words.forEach(word-> System.out.println(word));
回答by Yogesh Prajapati
I think this will help you, here i am using Iterable.
我认为这会对你有所帮助,我在这里使用 Iterable。
TreeSet treeSet = new TreeSet<String>();
treeSet.add("A");
treeSet.add("B");
Iterable<String> it = treeSet;
Iterator iterator = it.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}