Hi!
 
So it is saying, if not the condition there is a next element then do remove 
the current element.


Respects,

JKid314159
http://existentialists.blogspot.com/
 
 
 

--- On Tue, 8/4/09, Ashok A V <ashok.f...@gmail.com> wrote:


From: Ashok A V <ashok.f...@gmail.com>
Subject: [java programming] Re: Lab1016
To: migat...@gmail.com, happy27...@yahoo.com
Cc: javaprogrammingwithpassion@googlegroups.com
Date: Tuesday, August 4, 2009, 3:08 AM



Hi folks,

Hope this helps :)

Defined with a example .....

package com.help.pack;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class CollectionsExample {

    public static void main(String args[]) {
        List<String> strlist = new ArrayList<String>();
        strlist.add("Ashok");
        strlist.add("JKid");
        strlist.add("Elvis");
        filter(strlist);//filter criteria defined in here
        System.out.println("The final list after filtering");
        System.out.println(strlist);
        
        
    }

    static void filter(Collection<String> c) {
        for (Iterator<String> it = c.iterator(); it.hasNext();) //Use the 
iterator
            if (!cond(it.next()))
                it.remove();
    }

    static boolean cond(String strfrmcoll) {
        boolean condition = true;
        if (strfrmcoll.equals("Ashok")) {  //Remove Ashok from the list
            condition = false;
        }
        return condition;
    }
}

Thanks and Cheers,
Ashok A V

On Tue, Aug 4, 2009 at 12:55 AM, miga<migat...@gmail.com> wrote:
>
>
>
> On Aug 3, 7:50 pm, JKid314159 <happy27...@yahoo.com> wrote:
>> Re: The Java Tutorials
>> Home Page>Collections?Interfaces>The Collection Interface
>>
>> Dear Java Programmer:
>>
>> I am not too sure how this condition works?  Looks simple but I have a block 
>> on it.
>>
>> if (!cond(it.next()))
>>             it.remove();
>>
> Actually the whole code would be:
>
> static void filter(Collection<?> c) {
>    for (Iterator<?> it = c.iterator(); it.hasNext(); )
>        if (!cond(it.next()))
>            it.remove();
> }
>
> (from 
> http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.html)
>
> So, say you want to get a sub-collection of a collection whose
> elements respond to given condition; to do it, you define a filter on
> the collection to isolate the elements which responds to the
> condition, or to say it more accurately you write a method which
> iterates over the collection, and inside this iteration locates the
> last element which violates the condition, if it does so you remove
> it.
>
> All that, because remove operates on the current element and an
> iterator gives a position between two elements, so that it.next gives
> you the current element to remove if it violates the condition.
>
> See http://java.sun.com/javase/6/docs/api/java/util/ListIterator.html
> for a graphic on cursor position.
>> JKid314159http://existentialists.blogspot.com/
>>
>>
>>
> >
>



-- 
Victory belongs to the most persevering.
- Napoleon





      
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to