0

I've a list of user object. And I want to get list of user name except using loop. Is there any collection or util function in java 7??

10
  • 1
    what is the problem with for loop Commented Nov 18, 2013 at 10:44
  • 1
    @Rounak: there is no way to do that without a loop. Whatever library you find, it will always use a loop. What is "huge"? Commented Nov 18, 2013 at 10:45
  • 1
    @JBNizet There is a way to do it without a loop, by creating a view into the underlying User list. Maybe Guava has something like that? Whether that would solve OP's performance issue highly depends on his access pattern of the name list. Commented Nov 18, 2013 at 10:50
  • 1
    5,000 users is not a huge list. Commented Nov 18, 2013 at 10:56
  • 2
    @Rounak 5000 items in a List is a ridiculously small number. There's no performance issues involved here, trust me. Commented Nov 18, 2013 at 10:56

2 Answers 2

2

No, there's no util function. It's 3 lines of code to do it yourself.

Even if there was a util function, it wouldn't help with performance. It would be implemented as a for loop.

As for performance, have you actually profiled your application and seen that iterating the List is a performance hotspot?

Sign up to request clarification or add additional context in comments.

Comments

0

I think what you mean is:

for (String pos: User) {
    System.out.println(pos.toString());
}

This uses an Iterator, but since 1.5 Java you can use this shorthand to not show the Iterator.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.