5

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,194
  • 6
  • 53
  • 88
user1303750
  • 53
  • 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
  • 88,795
  • 14
  • 155
  • 148
4
for (String s: words) {
    System.out.println(s);
}
dty
  • 18,550
  • 6
  • 52
  • 81
2

The java 8 way, using forEach :

words.forEach(word-> System.out.println(word));
Bajal
  • 4,799
  • 2
  • 19
  • 24
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,608
  • 2
  • 31
  • 72