java - Iterator doesn't have a method to getNextElement directly without moving cursor -
why there no method element directly without iterating through it?
i have looked answer , found copy-pasted answer this:
it can implemented on top of current iterator interface since use rare, doesn't make sense include in interface has implement.
what's reason behind this?
collection interface extends "iterable".so implementations have use iterator. element in map directly,we can use map.get(key). element in list can use index. list implements randaomaccess interface easy element directly using key. in set cant element directly. point trying make implementations different 1 thing common in implementations need go through objects inside collection .this job done iterator.
now java promotes object oriented programming using iterator can use object(iterator instance) , call methods hasnext() first check if element present.it imposes limit of iteration restriction. using next() next element. hasnext() imposes nullcheck also.
in nutshell promotes object oriented concept , flexible abstarction used different implementations.
Comments
Post a Comment