Java – how do I get the first five values from the linkedhashset?
•
Java
I have a linkedhashset that contains multiple values
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
I want to iterate over this set of values and display the first five values for the number of items stored in the collection I use the for loop to iterate over the value and display the data, as shown below:
for (String sent : lhs) { text.append(sent); }
This will output all the values stored in the linkedhashset What changes should I make to my code to get only the first five values from the collection
Solution
int i = 0;
int i = 0; for (String sentences : lhs) { if (i > 4) break; text.append(sentences); i++; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码