How to print out individual Strings from Iterable<String>

主宰稳场 提交于 2019-12-23 12:42:02

问题


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?

tst is a Ternary search tree

Iterable<String> words = tst.prefixMatch(word);

回答1:


If it's Iterable you can do an extended for on it.

Iterable<String> iterable;
for(String s : iterable){
    //Do whatever you want
}

Resources:

  • Oracle.com - The foreach loop

Related topics:

  • What is the Iterable interface used for?



回答2:


for (String s: words) {
    System.out.println(s);
}



回答3:


The java 8 way, using forEach :

words.forEach(word-> System.out.println(word));



回答4:


I think this will help you, here i am using 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());

        }


来源:https://stackoverflow.com/questions/9947221/how-to-print-out-individual-strings-from-iterablestring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!