Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the String class in Java implement Iterable?

Many Java framework classes implement Iterable, however String does not. It makes sense to iterate over characters in a String, just as one can iterate over items in a regular array.

Is there a reason why String does not implement Iterable?

like image 618
user333335 Avatar asked May 05 '10 10:05

user333335


People also ask

Why is String not iterable?

A String is an immutable sequence of bytes. Strings are iterable; iteration over a string yields each of its 1-byte substrings in order. But String doesn't implement Iterable 's Iterate method.

Why are strings iterable?

The list numbers and string names are iterables because we are able to loop over them (using a for-loop in this case).

Does Java list implement Iterable?

The Collection interface extends Iterable , so all subtypes of Collection also implement the Iterable interface. For instance, both the Java List and Set interfaces extend the Collection interface, and thereby also the Iterable interface.

What does it mean for a class to implement the Iterable interface?

In general, an object Implementing Iterable allows it to be iterated. An iterable interface allows an object to be the target of enhanced for loop(for-each loop).


1 Answers

There really isn't a good answer. An iterator in Java specifically applies to a collection of discrete items (objects). You would think that a String, which implements CharSequence, should be a "collection" of discrete characters. Instead, it is treated as a single entity that happens to consist of characters.

In Java, it seems that iterators are only really applied to collections and not to a string. There is no reason why it is this way (near as I can tell - you would probably have to talk to Gosling or the API writers); it appears to be convention or a design decision. Indeed, there is nothing preventing CharSequence from implementing Iterable.

That said, you can iterate over the characters in a string like so:

for (int i = 0; i < str.length(); i++) {   System.out.println(str.charAt(i)); } 

Or:

for(char c : str.toCharArray()) {   System.out.println(c); } 

Or:

"Java 8".chars().forEach(System.out::println); 

Also note that you cannot modify a character of a String in place because Strings are immutable. The mutable companion to a String is StringBuilder (or the older StringBuffer).

EDIT

To clarify based on the comments on this answer. I'm trying to explain a possible rationale as to why there is no Iterator on a String. I'm not trying to say that it's not possible; indeed I think it would make sense for CharSequence to implement Iterable.

String provides CharSequence, which, if only conceptually, is different from a String. A String is usually thought of as a single entity, whereas CharSequence is exactly that: a sequence of characters. It would make sense to have an iterator on a sequence of characters (i.e., on CharSequence), but not simply on a String itself.

As Foxfire has rightly pointed out in the comments, String implements the CharSequence interface, so type-wise, a String is a CharSequence. Semantically, it seems to me that they are two separate things - I'm probably being pedantic here, but when I think of a String I usually think of it as a single entity that happens to consist of characters. Consider the difference between the sequence of digits 1, 2, 3, 4 and the number 1234. Now consider the difference between the string abcd and the sequence of characters a, b, c, d. I'm trying to point out this difference.

In my opinion, asking why String doesn't have an iterator is like asking why Integer doesn't have an iterator so that you can iterate over the individual digits.

like image 116
Vivin Paliath Avatar answered Oct 01 '22 10:10

Vivin Paliath