arrays - Java Collections List : Converting from List '<Column <String1, String2>>' to 'List <String1>' -
i have function returns list this:-
list <column <string1, string2>>
next want pass list 2nd function, 2nd function needs list contains 1st part (string1) of column(s) of above list.
so want pass list 2nd function:-
list <string1>
my use case: both functions library use access database(cassandra) web application. 1st function gives me list of columns has 2 parts name(string1) , value(string2). 1st function gives me list of columns(each of has 2 strings) need use list of column names supply 2nd function that'll query db columns.
since need job atleast 2-3 times before asking data db single page, need superfast , reasonably efficient method so.
it depends on mean "efficient" (memory, execution time, else?) , second function doing.
if care speed , second function going looking @ items in list repeatedly, should copy strings new list<string>
:
list<string> strings = new arraylist<string>(input.size()); (column<string, string> column : input) { strings.add(column.name()); } return strings;
if, on other hand, second function going @ small subset of items or if care more memory speed want lazy view converts items accessed. lists.transform
google guava can you:
return lists.transform(input, new function<column<string, string>, string>() { public string apply(column<string, string> column) { return column.name(); } };
note may want create function separate static class unless you're ok holding onto reference enclosing instance. used anonymous class here brevity/clarity.
Comments
Post a Comment