You Can still achieve enhanced for loop using iterator by using following
modification.

I apologize if this thread going in different context. We can go with
either List or Set depending on the Requirement.

*Code Snippet:*

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class PluginIterator implements Iterable<PluginIterator.Plugin> {

  static class Plugin {
    String name;
    Plugin(String name) {
      this.name = name;
    }
    public String toString() {
      return "Plugin{" + name + "}";
    }
  }

  Set<Plugin> set = new HashSet<Plugin>();

  public void addPenguin(Plugin p) {
    set.add(p);
  }

  public Iterator<Plugin> getPlugins() {
    return set.iterator();
  }

  public Iterator<Plugin> iterator() {
    return getPlugins();
  }

  public static void main(String args[]) {
PluginIterator pluginIterators = new PluginIterator();
Plugin plugin1 = new Plugin("Plugin-1");
Plugin plugin2 = new Plugin("Plugin-2");
Plugin plugin3 = new Plugin("Plugin-3");
Plugin plugin4 = new Plugin("Plugin-4");
pluginIterators.addPenguin(plugin1);
pluginIterators.addPenguin(plugin2);
pluginIterators.addPenguin(plugin3);
pluginIterators.addPenguin(plugin4);
    for (Plugin p : pluginIterators) {
      System.out.println(p);
    }
  }
}

On Tue, Jan 12, 2016 at 10:24 AM, Gary Gregory <[email protected]>
wrote:

> I dislike this iterator business very much, and you can't use it in a for
> each loop.
>
> Gary
>
> On Mon, Jan 11, 2016 at 8:46 PM, sampath kumar <[email protected]>
> wrote:
>
>> How about this, Unmodifiable Iterator
>>
>> Iterator<String> getPluginPackages();
>>
>> On Tue, Jan 12, 2016 at 9:50 AM, Gary Gregory <[email protected]>
>> wrote:
>>
>>> List<String> getPluginPackages();
>>>
>>> G
>>>
>>> On Mon, Jan 11, 2016 at 8:12 PM, Paul Benedict <[email protected]>
>>> wrote:
>>>
>>>> Thu can only use Set if you have a notion of equality and comparison.
>>>> Do you for this collection?
>>>> On Jan 11, 2016 9:48 PM, "Gary Gregory" <[email protected]> wrote:
>>>>
>>>>> Well, not really, I'd like to express whether the collection allows
>>>>> for duplicates or not, if not, use a set.
>>>>>
>>>>> Gary
>>>>>
>>>>> On Mon, Jan 11, 2016 at 7:08 PM, Matt Sicker <[email protected]> wrote:
>>>>>
>>>>>> Even a Collection works well for this.
>>>>>>
>>>>>>
>>>>>> On Monday, 11 January 2016, Gary Gregory <[email protected]>
>>>>>> wrote:
>>>>>>
>>>>>>> On Mon, Jan 11, 2016 at 5:19 PM, Ralph Goers <
>>>>>>> [email protected]> wrote:
>>>>>>>
>>>>>>>> It is returning a List already.  Does it really matter much?
>>>>>>>>
>>>>>>>
>>>>>>> It is not indeed critical. It's just a finer point of declaring the
>>>>>>> intention of the data model, either duplicates are allowed and 
>>>>>>> expected, or
>>>>>>> not.
>>>>>>>
>>>>>>> Gary
>>>>>>>
>>>>>>>>
>>>>>>>> Ralph
>>>>>>>>
>>>>>>>> On Jan 11, 2016, at 6:04 PM, Gary Gregory <[email protected]>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>> Should APIs like:
>>>>>>>>
>>>>>>>>
>>>>>>>> org.apache.logging.log4j.core.config.Configuration.getPluginPackages()
>>>>>>>>
>>>>>>>> really return a List instead of a Set?
>>>>>>>>
>>>>>>>> Gary
>>>>>>>>
>>>>>>>> --
>>>>>>>> E-Mail: [email protected] | [email protected]
>>>>>>>> Java Persistence with Hibernate, Second Edition
>>>>>>>> <http://www.manning.com/bauer3/>
>>>>>>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>>>>>>> Spring Batch in Action <http://www.manning.com/templier/>
>>>>>>>> Blog: http://garygregory.wordpress.com
>>>>>>>> Home: http://garygregory.com/
>>>>>>>> Tweet! http://twitter.com/GaryGregory
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> E-Mail: [email protected] | [email protected]
>>>>>>> Java Persistence with Hibernate, Second Edition
>>>>>>> <http://www.manning.com/bauer3/>
>>>>>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>>>>>> Spring Batch in Action <http://www.manning.com/templier/>
>>>>>>> Blog: http://garygregory.wordpress.com
>>>>>>> Home: http://garygregory.com/
>>>>>>> Tweet! http://twitter.com/GaryGregory
>>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Matt Sicker <[email protected]>
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> E-Mail: [email protected] | [email protected]
>>>>> Java Persistence with Hibernate, Second Edition
>>>>> <http://www.manning.com/bauer3/>
>>>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>>>> Spring Batch in Action <http://www.manning.com/templier/>
>>>>> Blog: http://garygregory.wordpress.com
>>>>> Home: http://garygregory.com/
>>>>> Tweet! http://twitter.com/GaryGregory
>>>>>
>>>>
>>>
>>>
>>> --
>>> E-Mail: [email protected] | [email protected]
>>> Java Persistence with Hibernate, Second Edition
>>> <http://www.manning.com/bauer3/>
>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>> Spring Batch in Action <http://www.manning.com/templier/>
>>> Blog: http://garygregory.wordpress.com
>>> Home: http://garygregory.com/
>>> Tweet! http://twitter.com/GaryGregory
>>>
>>
>>
>>
>> --
>> Regards,
>> Sampath
>>
>
>
>
> --
> E-Mail: [email protected] | [email protected]
> Java Persistence with Hibernate, Second Edition
> <http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
>



-- 
Regards,
Sampath

Reply via email to