Discuss / Java / Queue

Queue

Topic source
public class Main {

	public static void main(String[] args) {
		Stream<BigInteger> stream = Stream.generate(new FibonacciQueue());
        stream.limit(50).forEach(System.out::println);
	}
}

class FibonacciQueue implements Supplier<BigInteger>{

    static Queue<BigInteger> queue = new LinkedList<>();
    
    static {
        queue.add(new BigInteger("0"));        
        queue.add(new BigInteger("1"));    
    }

    @Override    
    public BigInteger get() {
        BigInteger prev = queue.poll();        
        queue.add(prev.add(queue.peek()));        
        return queue.peek();    
    }
}

  • 1

Reply