Java Extend ArrayList <Dummy> Functions
let's say that I have
class Dummy { String a, b; public Dummy(String a, String b) { this.a = a; this.b = b; } public String toString(){ return a+b; } public String getA() { return a; } public String getB() { return b; } }
I would like to have
List<Dummy> myList = new ArrayList<Dummy>() {{ add(new Dummy("test", "")); add(new Dummy("boo", "o")); }}; System.out.println( myList.toString()); System.out.println( myList.getAs()); //should return ["test", "boo"] System.out.println( myList.getBs());//should return ["", "o"] If you see what I mean
perhaps you need to create an extension of the class ArrayList<Dummy> ?
change
seems just as good
class Dummy { String a, b; //... public static String toAString(List<Dummy> l){ String s=""; for (Dummy d : l){ s+=da; } return s; } } edit2:
I will have only 2 lines in the mannequin, is it better to do this ArrayList<String []> ? in terms of memory
Define first-class functions corresponding to getA and getB . I am using Function from Guava for this.
class Dummy { String a, b; public Dummy(String a, String b) { this.a = a; this.b = b; } public String toString(){ return a+b; } public String getA() { return a; } public String getB() { return b; } public static Function<Dummy, String> getA = new Function<Dummy, String>() { public String apply(Dummy d) { return da; } } public static Function<Dummy, String> getB = new Function<Dummy, String>() { public String apply(Dummy d) { return db; } } } Here is how you can use it: ( Iterables below also from Guava).
List<Dummy> myList = new ArrayList<Dummy>() {{ add(new Dummy("test", "")); add(new Dummy("boo", "o")); }}; System.out.println( myList.toString()); System.out.println( Iterables.transform(myList, Dummy.getA)); // returns ["test", "boo"] System.out.println( Iterables.transform(myList, Dummy.getB)); // returns ["", "o"] In pre-Java 8, you can use one of the functional APIs such as Guava, as follows:
Function<Dummy, String> getAFunc = new Function<Dummy, String> { @Override public String apply(Dummy dummy) { return dummy.getA(); } } Function<Dummy, String> getBFunc = new Function<Dummy, String> { @Override public String apply(Dummy dummy) { return dummy.getB(); } } System.out.println( Lists.transform(myList, getAFunc) ); System.out.println( Lists.transform(myList, getBFunc) ); Agreed that this is pretty ugly and verbose, and some of what the Guava team says when they say that misusing functional idioms makes them cry .
But wait, there is hope and a way to make this code more elegant, shorter and more generalized in any situation like this using the open source Funcito Library ( DISCLAIMER : I am the main author)
Now instead you can simply write
Function<Dummy, AType> getAFunc = functionFor( callsTo(Dummy.class).getA() ); Function<Dummy, BType> getBFunc = functionFor( callsTo(Dummy.class).getB() ); System.out.println( Lists.transform(myList, getAFunc) ); System.out.println( Lists.transform(myList, getBFunc) ); Keys for short use Funcito to compose your functions. It works even if A and B are not strings, as I showed in the example. And now you can use it for any type except Dummy, and for any number of fields with getter methods, using Funcito to quickly create a function that wraps the corresponding method. And Funcito will also work with several alternative functional APIs other than Guava, if you prefer.
Please note that the above uses import from Guava and static import from Funcito.
Use composition instead of inheritance ..............
Consideration of all classes in one package.
Dummy Class:
class Dummy { String a, b; public Dummy(String a, String b) { this.a = a; this.b = b; } public String toString(){ return a+b; } public String getA() { return a; } public String getB() { return b; } } Test Class: EDITED
public class Test{ Test t = new Test(); List<Dummy> myList = new ArrayList<Dummy>(); public static void main(String[] args){ t.myList.add(new Dummy("test", "")); t.myList.add(new Dummy("boo", "o")); for (Dummy d : t.myList) { System.out.println(d.toString()); System.out.println(d.getA()); System.out.println(d.getB()); } } } Expanding an ArrayList array quickly becomes bankrupt, as you will eventually need MoronArrayList, GeniusArrayList, DummyHashMap, DummyQueue, etc.
Guava stuff suggested by @missingfaktor is one option. Another option, if really necessary, is to put utilities in the Dummy class. eg.
public static String toAStrings(Collection<Dummy> collection) { StringBuilder sb = new StringBuilder; boolean firstTime = true; for (Dummy dummy : collection) { if (!firstTime ) sb.append(","); firstTime = false; sb.append(dummy.getA()); } return sb.toString(); }