Java CookBook--Using a Buffer--by Tim O'Brien 整理by博主

数据结构Buffer的使用

  下面是一个最简单的Buffer实现UnboundedFifoBuffer的使用实例,UnboundedFifoBuffer是一个先进先出无大小限制的数据结构,如果无限制的添加,可能会耗尽内存。

import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.buffer.UnboundedFifoBuffer;

public class BufferTest {
	public static void main(String[] args) {
		
		/*
		 * A buffer is an object that is defined by the algorithm used for element
		 * removal.Buffer objects can be priority queues, staging areas,message queues,
		 * or buffers for I/O. One of the simplest Buffer implementations is the 
		 * UnboundFifoBuffer, a first-in, first-out data structure with no size limit.
		 */
		//create an Unbounded FIFO
		Buffer buffer =new UnboundedFifoBuffer();
		//add element to the Buffer
		buffer.add("A");
		buffer.add("B");
		buffer.add("D");
		//remove element from the buffer
		String value=(String)buffer.remove();
		buffer.add("E");
		value=(String)buffer.remove();
		System.out.println(buffer.toString());
       
		/*
		 * Conclusion:
		 * 1. This example creates an UnboundedFifoBuffer, adding three elements: 
		 * "A," "B," and "D." When remove() is invoked,the buffer selects 
		 * the item that was placed into the buffer first—the first-in is the 
		 * first-out. The first call to remove( )returns "A," and the second call 
		 * returns "B." so the result is [D, E]
		 * 2. BufferUnderflowException will be thrown if you try to remove() or get() 
		 * an element from an empty Buffer.
		 * 3.This data sturcture is userful if you need a temporary holding are between
		 * stages in a pipeline--a queue to hold objects as they wait to be processed
		 * by the next stage. A unbounded FIFO buffer has no size limit, and, as such, 
		 * it could grow to a limitless size. In fact,if an application continued to fill
		 * an unbounded buffer, it would exhaust availabe memory.
		 */
	}
}
  结果如上述:

[D,E]
  再简单简绍一个先进先出的有容量限制的Buffer, 在超出容量删除或增加时,会抛出异常。如下:

  

import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.BufferOverflowException;
import org.apache.commons.collections.BufferUnderflowException;
import org.apache.commons.collections.buffer.BoundedFifoBuffer;

public class BufferTest {
	public static void main(String[] args) {
		//Create a Bounded FIFO with a max size of two
		Buffer buffer=new BoundedFifoBuffer(2);
		buffer.add( "One" );
		buffer.add( "Two" );
		// Adding a third element to this buffer will cause an exception
		try {
			buffer.add( "Three" );
			} catch(BufferOverflowException bue){
				System.out.println("Buffer is Full!");
			}
		// Remove an object... Buffer now contains one element.
		Object removed = buffer.remove( );
		System.out.println("removed:"+removed);
		// Add another object
		buffer.add( "Three" );
		Object remove1 = buffer.remove( );
		System.out.println("remove1:"+remove1);
		Object remove2 = buffer.remove( );
		System.out.println("remove2:"+remove2);
		// This next remove( ) should cause a BufferUnderflowException
		try {
			Object remove3 = buffer.remove( );
			} catch(BufferUnderflowException bue){
				System.out.println( "Buffer is Empty!" );
			}	
		/*
		 * A buffer in Commons Collections is analogous to a Queue in Java5.The Queue 
		 * interface is accompanied by a number of interfaces that provide almost
		 * identical behavior to the Buffer interface as defined in Commons Collections 3.0. 
		 * Queue contains some new methods, offer( ) and peek( ), which perform the same 
		 * functions as add( ) and remove( ), with one difference: these new methods return 
		 * false if there is any problem adding an object to a Queue.
		 */
	}
}

结果如下:

Buffer is Full!
removed:One
remove1:Two
remove2:Three
Buffer is Empty!

如结论所言,我们也可以用java自带的Queue替代Buffer。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值