Right way to make my own collection in java -
i'm trying own collection class in java.
i need find data key , able iterate on , element index, decide make encapsulation of hashtable , arraylist.
this code:
public class geocollection <t extends geographic> implements iterable<t>, iterator<t>{ private hashtable<string,t> data_table; private arraylist<t> data; private int cursor = 0; public geocollection(){ data = new arraylist<>(); data_table = new hashtable<>(); } public void add(string key,t data){ this.data.add(data); data_table.put(key,data); } public t get(int index){ if(index >= data.size()) throw new indexoutofboundsexception(); return data.get(index); } public t get(string v){ return data_table.get(v); } public t next() { if( cursor == data.size()) throw new nosuchelementexception(); cursor++; return data.get(cursor-1); } public t first(){ cursor = 0; return data.get(cursor); } public boolean hasnext(){ return cursor < data.size(); } public boolean remove(person p) { return data.remove(p); } //se implemeta el iterator @override public iterator<t> iterator() { cursor = 0; return this; }
}
there's need implement list interface or that? because don't know if encapsulating hashtable , arraylist , implementing basic operations in enough call "collection" class.
i thankful advice or correction of code.
thanks.
i think best way implementing collection
implements java.util.collection<e>
that way have implement every method in collection interface bt making class extending abstractcollection extends abstractcollection
more easy since nessessary things , thing have worry iterator()
, size()
Comments
Post a Comment