How to replace two Java Constructors with one generic Constructor -
i have following 2 java methods(simplified). replace these 2 methods 1 method. reason wanting 1 method used local testing, , 1 method using during run on hadoop cluster. spent while debugging code before realising had adjusted 1 method , not other, , know how avoid silly mistake in future.
public myobject(arraylist<string> values){ for(string val: values){ system.out.println(val.tostring()); } } public myobject(iterable<text> values){ for(text val: values){ system.out.println(val.tostring()); } }
text apache hadoop class. here link source code. http://grepcode.com/file/repository.cloudera.com/content/repositories/releases/com.cloudera.hadoop/hadoop-core/0.20.2-737/org/apache/hadoop/io/text.java
since not using specific string
or text
(whatever is) in constructors, take iterable<? extends object>
parameter:
public myobject(iterable<? extends object> values){ for(object val: values){ system.out.println(val.tostring()); } }
Comments
Post a Comment