Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Creating a filter method in my InfiniteListImpl class

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a filter method in my InfiniteListImpl class

    I have an InfiniteListImpl class which implements the interface InfiniteList, and I am having difficulty creating the filter method. Here is what I have for my InfiniteListImpl class so far:

    import java.util.function.Supplier;
    import java.util.function.BinaryOperator;
    import java.util.function.Consumer;
    import java.util.function.Function;
    import java.util.function.Predicate;
    import java.util.function.BiFunction;
    import java.util.Optional;
     
    public class InfiniteListImpl<T> implements InfiniteList<T>{
        Supplier<Optional<T>> head;
        Supplier<InfiniteListImpl<T>> tail;
     
        public InfiniteListImpl(Supplier<Optional<T>> head, Supplier<InfiniteListImpl<T>> tail){
            this.head = head;
            this.tail = tail;
        }
     
        public static <T> InfiniteListImpl<T> generate(Supplier<? extends T> generator){
            return new InfiniteListImpl<T>(
                () -> Optional.ofNullable(generator.get()),
                () -> InfiniteListImpl.generate(generator));
        }
     
        public static <T> InfiniteListImpl<T> iterate(T seed, Function<? super T, ? extends T> func){
        return new InfiniteListImpl<T>(
            () -> Optional.ofNullable(seed),
            () -> InfiniteListImpl.iterate(func.apply(seed), func));
        }
     
        public InfiniteListImpl<T> get(){
            Optional<T> headElement = this.head.get();
            if (headElement.isPresent()){
                System.out.println(headElement.get());
            }
            return tail.get();
        }
     
        public <R> InfiniteListImpl<R> map(Function<? super T, ? extends R> mapper){
            return new InfiniteListImpl<R>(
                    () -> head.get().map(mapper),
                    () -> tail.get().map(mapper));
        }
     
        public InfiniteListImpl<T> filter(Predicate<? super T> pred){
     
    }

    The filter method is supposed to fulfill these following test cases:

    jshell> InfiniteList<Integer> ifl = InfiniteList.generate(() -> 1).map(x -> x * 2)
    jshell> ifl instanceof InfiniteListImpl
    $.. ==> true
    jshell> ifl = InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0)
    jshell> ifl instanceof InfiniteListImpl
    $.. ==> true
    jshell> InfiniteListImpl<Integer> ifl1 = InfiniteListImpl.iterate(1, x -> x + 1).map(x -> x * 2)
    jshell> InfiniteListImpl<Integer> ifl2 = ifl1.get().get()
    2
    4
    jshell> ifl2 = ifl1.get().get()
    2
    4
    jshell> ifl1 = InfiniteListImpl.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).get().get()
    2
    jshell> ifl2 = ifl1.get().get().get()
    4
    jshell> ifl1 = InfiniteListImpl.iterate(1, x -> x + 1).map(x -> x * 2).map(x -> x - 1).get().get()
    1
    3
    jshell> ifl1 = InfiniteListImpl.iterate(1, x -> x + 1).filter(x -> x % 2 == 0).filter(x -> x < 4).get().get().get().get()
    2

    Any advice on how I should go about creating my filter method?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a filter method in my InfiniteListImpl class

    Can you explain what the code is supposed to do in more detail?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Creating a method
    By jennocide in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2014, 01:27 AM
  2. [SOLVED] creating an iterator without creating a class for it
    By Lenjaku in forum Collections and Generics
    Replies: 6
    Last Post: January 22nd, 2014, 03:17 AM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. Creating a scaleUp main method in a new class
    By Brainz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 08:58 AM

Tags for this Thread