6

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);
yoozer8
  • 7,361
  • 7
  • 58
  • 93
user1303750
  • 63
  • 1
  • 1
  • 3

4 Answers4

12

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:

Community
  • 1
  • 1
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
4
for (String s: words) {
    System.out.println(s);
}
dty
  • 18,795
  • 6
  • 56
  • 82
2

The java 8 way, using forEach :

words.forEach(word-> System.out.println(word));
Bajal
  • 5,487
  • 3
  • 20
  • 25
0

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

        }
Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77