java - Differnce between addfirst and offerFirst methods in ArrayDeque -
have tried out sample program understand difference between addfirst , offerfirst methods in arraydeque of java 6. seem same, suggestions?
public void interfacedequetest() { try{ arraydeque<string> ad = new arraydeque<string>(); ad.addfirst("a1"); ad.offerfirst("o1"); ad.addfirst("a2"); ad.offerfirst("02"); ad.addfirst("a3"); system.out.println("in block"); (string number : ad){ system.out.println("number = " + number); } }
the difference happens when addition fails, due queue capacity restriction:
.addfirst()throws (unchecked) exception,.offerfirst()returnsfalse.
this documented in deque, arraydeque implements.
of note arraydeque has no capacity restrictions, .addfirst() never throw exception (and .offerfirst() return true); unlike, instance, linkedblockingqueue built initial capacity.
Comments
Post a Comment