user1303750
user1303750

Reputation: 63

How to print out individual Strings from Iterable<String>

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);

Upvotes: 6

Views: 20899

Answers (4)

Bajal
Bajal

Reputation: 5986

The java 8 way, using forEach :

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

Upvotes: 2

Yogesh Prajapati
Yogesh Prajapati

Reputation: 4870

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());

        }

Upvotes: 0

Colin Hebert
Colin Hebert

Reputation: 93167

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

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

Resources:

Related topics:

Upvotes: 12

dty
dty

Reputation: 18998

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

Upvotes: 4

Related Questions