Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Collections List : Converting from List '<Column <String1,String2>>' to 'List <String1>'

Reply
Thread Tools

Java Collections List : Converting from List '<Column <String1,String2>>' to 'List <String1>'

 
 
asil klin
Guest
Posts: n/a
 
      02-19-2011


I have a function that returns a list like this:-

List <Column <String1, String2>>

Next I want to pass this list to a 2nd function, but 2nd function just needs a list which contains only 1st part (string1) of the Column(s) of the above list.

So I want pass just this list to 2nd function:-

List <String1>

What would be the best way to do this ??

[my use case: Both the functions are from a library that I use to access database(Cassandra) for a web application. 1st function gives me a list of all columns which has two parts name(String1) and value(String2). So 1st function gives me a list of all columns(each of which has two strings) then I just need to use the list of column names to supply it to 2nd function that'll query the DB for those columns.]
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-19-2011
On 02/19/2011 07:04 AM, asil klin wrote:
>
>
> I have a function that returns a list like this:-


DO NOT MULTI-POST!

--
Lew
Honi soit qui mal y pense.
 
Reply With Quote
 
 
 
 
Daniele Futtorovic
Guest
Posts: n/a
 
      02-19-2011
On 19/02/2011 14:19, Peter Duniho allegedly wrote:

> But you can accomplish much the same sort of thing, albeit with not
> quite as concise a call site (but at least the entire collection doesn't
> have to be duplicated). For example:
>
>
> public interface Select<T, S>
> {
> S select(T t);
> }
>
>
> public class Iterables
> {
> public static <T, S> Iterable<S> Select(Iterable<T> source, Select<T, S>
> select)
> {
> return new SelectIterable<T, S>(source, select);
> }
>
> private static class SelectIterable<T, S> implements Iterable<S>
> {
> private final Iterable<T> source;
> private final Select<T, S> select;
>
> public SelectIterable(Iterable<T> source, Select<T, S> select)
> {
> this.source = source;
> this.select = select;
> }
>
> @Override
> public java.util.Iterator<S> iterator()
> {
> return new Iterator(source.iterator());
> }
>
> private class Iterator implements java.util.Iterator<S>
> {
> private final java.util.Iterator<T> source;
>
> public Iterator(java.util.Iterator<T> source)
> {
> this.source = source;
> }
>
> @Override
> public boolean hasNext()
> {
> return source.hasNext();
> }
>
> @Override
> public S next()
> {
> return SelectIterable.this.select.select(source.next());
> }
>
> @Override
> public void remove()
> {
> throw new UnsupportedOperationException();
> }
> }
> }
> }
>
>
> public class Column
> {
> private final String string1;
> private final String string2;
>
> public Column(String string1, String string2)
> {
> this.string1 = string1;
> this.string2 = string2;
> }
>
> public String getString1() { return string1; }
> public String getString2() { return string2; }
> }
>
>
> import java.util.ArrayList;
> import java.util.List;
>
> public class Main
> {
>
> /**
> * @param args
> */
> public static void main(String[] args)
> {
> List<Column> columns = new ArrayList<Column>();
>
> columns.add(new Column("1", "A"));
> columns.add(new Column("2", "B"));
> columns.add(new Column("3", "C"));
>
> method2(Iterables.Select(columns, new Select<Column, String>()
> {
> public String select(Column column)
> {
> return column.getString1();
> }
> }));
> }
>
> private static void method2(Iterable<String> names)
> {
> for (String name : names)
> {
> System.out.println(name);
> }
> }
>
> }


This is what I'd suggest, too. Although I'd call it 'map' rather than
'select'. And I would recommend going the whole way and making it a
(read-only) collection (by extending AbstractCollection) or a
(read-only) List (by extending AbstractList), rather than using only
Iterable. Iterable is OK, but lacks useful features (notably, size())
for the more than basic uses. And since you're writing some code
already, might as well put in a little extra effort and get the whole bang.

I've written a little framework exactly like this some time ago. It is
insanely useful -- which shouldn't come as a surprise for anyone who's
ever heard of functional programming.

Note that the main advantage is that this approach is that it reflects
modifications of the underlying object transparently, and thus blends in
very nicely with an object-oriented structure.

--
DF.
 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      02-20-2011
On 19/02/2011 18:23, Peter Duniho allegedly wrote:
> On 2/20/11 12:50 AM, Daniele Futtorovic wrote:
>> This is what I'd suggest, too. Although I'd call it 'map' rather
>> than 'select'.

>
> The word "select" simply came from my exposure to .NET. Actually, I
> find the word "project" (as a verb) to be more appropriate, though I
> agree "map" is fine too.
>
> I don't think the precise name is going to matter too much.


Sure.

>> And I would recommend going the whole way and making it a
>> (read-only) collection (by extending AbstractCollection) or a
>> (read-only) List (by extending AbstractList), rather than using
>> only Iterable.

>
> It will depend on how one intends to use it. Again, because I am
> following the .NET model, Iterable is the first choice only due to
> that.
>
> However, it's worth pointing out that Iterable allows for sequences
> larger than 2GB items, while extending AbstractList or
> AbstractCollection, because those classes use an int to describe
> their length (and in the case of AbstractList, to enumerate as
> well).


Good point; I hadn't thought of that. Still, as the underlying structure
is already one that is constrained by the 2^31 -1 limit (being, in the
OP's case, a java.util.List), and considering how the overwhelming
majority of use-cases would fall hither of that limit, I think it would
be safe to code for these uses. Might of course make the Iterable
available, too.


> Also, using Iterable means that the code is usable for a broader
> range of inputs. I.e. is more re-usable. Extending AbstractList or
> AbstractCollection will limit the uses to wrapping lists or
> collections.


Now I'm not sure you got what I meant. I meant that the content-mapping
code you showed for Iterable structures could be extended so that the
"view" objects would be Collections, when mapping the contents of any
class implementing Collection, or Lists, when mapping the contents of
any class implementing List; and that it's very easy to do by making the
"view" Object extend Abstract(Collection|List), because you only have a
very few methods to implement (provided you make them read-only -- if
they're writable it becomes slightly more tricky).

I agree that Iterable is an interface that serves broader purposes and
can be implemented by a wider range of objects than Collection et al.,
but nevertheless a great share of the code (my code, at least) consists
of working with Collections. It's fine if it's broad, but not if it
being broad lessens its usefulness.


>> Iterable is OK, but lacks useful features (notably, size()) for the
>> more than basic uses.

>
> ..NET addresses that lack by including the extension method Count()
> (in my code, that would show up as a static method in the Iterables
> class), which checks for common interfaces that support a direct
> retrieval of the collection length, and if those are not available,
> then enumerates the entire collection as a fall-back.


Is that the equivalent of this "defender methods" stuff I've been
hearing about?


>> Note that the main advantage is that this approach is that it
>> reflects modifications of the underlying object transparently, and
>> thus blends in very nicely with an object-oriented structure.

>
> I'm not sure I'd call that the _main_ advantage. In fact, it can be a
> bit of a pitfall in .NET, as chained enumerators are all re-run each
> time you enumerate the last link of the chain.
>
> Not only is it a potential performance issue, variable capturing can
> result in some non-intuitive (and often unintended) results, because
> even the parameters of the enumerations may be changed after the
> fact, having effect on subsequent enumerations of the new enumerable
> object.
>
> But it definitely can be a very handy aspect of the approach, if one
> is careful to mind the implications of the implementation.


Sorry but you totally lost me there.

I wouldn't claim for a second that these things are going to be very
efficient. To me, the main use is as a specific "view" of a given
structure, short-term objects which I'm going briefly to manipulate and
then discard. In those cases, there is next to no performance impact,
because what the view is doing, I would have to do if I didn't have it
-- it just hides it and provides a handy syntax, making the application
logic clearer.

--
DF.
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      02-20-2011
On 19-02-2011 20:00, Peter Duniho wrote:
> On 2/20/11 8:05 AM, Daniele Futtorovic wrote:
>> Is that the equivalent of this "defender methods" stuff I've been
>> hearing about?

>
> I have no idea. What "defender methods" have you been hearing about?
> I've never even heard the term.


http://cr.openjdk.java.net/~darcy/DefenderMethods.pdf

Arne
 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      02-20-2011
On 11-02-19 09:05 PM, Arne Vajhøj wrote:
> On 19-02-2011 20:00, Peter Duniho wrote:
>> On 2/20/11 8:05 AM, Daniele Futtorovic wrote:
>>> Is that the equivalent of this "defender methods" stuff I've been
>>> hearing about?

>>
>> I have no idea. What "defender methods" have you been hearing about?
>> I've never even heard the term.

>
> http://cr.openjdk.java.net/~darcy/DefenderMethods.pdf
>
> Arne


I respect Goetz, but it's a lousy name. IMO.

AHS

--
We must recognize the chief characteristic of the modern era - a
permanent state of what I call violent peace.
-- James D. Watkins
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      02-20-2011
On 19-02-2011 20:40, Arved Sandstrom wrote:
> On 11-02-19 09:05 PM, Arne Vajhøj wrote:
>> On 19-02-2011 20:00, Peter Duniho wrote:
>>> On 2/20/11 8:05 AM, Daniele Futtorovic wrote:
>>>> Is that the equivalent of this "defender methods" stuff I've been
>>>> hearing about?
>>>
>>> I have no idea. What "defender methods" have you been hearing about?
>>> I've never even heard the term.

>>
>> http://cr.openjdk.java.net/~darcy/DefenderMethods.pdf

>
> I respect Goetz, but it's a lousy name. IMO.


He explains the name in the text.

I don't have a problem with it.

But I think that the headline should be "virtual extension methods"
because that is what it is all about. The "defender methods" is
just a part of it.

Arne

 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      02-20-2011
On 11-02-19 09:49 PM, Arne Vajhøj wrote:
> On 19-02-2011 20:40, Arved Sandstrom wrote:
>> On 11-02-19 09:05 PM, Arne Vajhøj wrote:
>>> On 19-02-2011 20:00, Peter Duniho wrote:
>>>> On 2/20/11 8:05 AM, Daniele Futtorovic wrote:
>>>>> Is that the equivalent of this "defender methods" stuff I've been
>>>>> hearing about?
>>>>
>>>> I have no idea. What "defender methods" have you been hearing about?
>>>> I've never even heard the term.
>>>
>>> http://cr.openjdk.java.net/~darcy/DefenderMethods.pdf

>>
>> I respect Goetz, but it's a lousy name. IMO.

>
> He explains the name in the text.
>
> I don't have a problem with it.
>
> But I think that the headline should be "virtual extension methods"
> because that is what it is all about. The "defender methods" is
> just a part of it.
>
> Arne
>

I know he explained it; I read the paper. But it was an unnecessary
and cutesy name. What's wrong with virtual extension method? If Goetz'
pet name takes off then it'll be just more useless jargon to confuse
novices...and without having read the specific explanation for it, the
name isn't even meaningful.

Like I said, I respect the guy a great deal. But I'll take him up on his
implied out, when he says "you could call these “public defender”
methods...". I could, but I won't.

AHS

--
We must recognize the chief characteristic of the modern era - a
permanent state of what I call violent peace.
-- James D. Watkins
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-20-2011
On Sat, 19 Feb 2011 04:04:53 -0800 (PST), asil klin
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>What would be the best way to do this ??


you pretty well have to create a new list and copy the items over one
at a time.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Refactor early. If you procrastinate, you will have
even more code to adjust based on the faulty design.
..

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-20-2011
On 02/20/2011 06:04 AM, Wanja Gayk wrote:
> In article<8f13c843-eced-41f8-b20b-901979548436
> @glegroupsg2000goo.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
>>
>> I have a function that returns a list like this:-
>>
>> List<Column<String1, String2>>
>>
>> Next I want to pass this list to a 2nd function, but 2nd function just needs a list which contains only 1st part (string1) of the Column(s) of the above list.
>>
>> So I want pass just this list to 2nd function:-
>>
>> List<String1>
>>
>> What would be the best way to do this ??

>
>> [my use case: Both the functions are from a library that I use to
>> access database(Cassandra) for a web application. 1st function gives
>> me a list of all columns which has two parts name(String1) and value
>> (String2). So 1st function gives me a list of all columns(each of
>> which has two strings) then I just need to use the list of column
>> names to supply it to 2nd function that'll query the DB for those
>> columns.]

>
> Naive Solution:
>
> List<String> names= new ArrayList<String>();


This does not fulfill the OP's condition that the list's base type be 'String1'.

A singularly poorly-named type, to be sure, but the one they asked for.

--
Lew
Honi soit qui mal y pense.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collections.sort() in Java 5 alex_us01 Java 7 10-16-2005 03:34 PM
where to find other java api collections like following web address?@@. mikeotp Java 2 12-20-2004 12:20 PM
Trees in the Java Collections framework Joona I Palaste Java 5 06-09-2004 10:42 AM
Sorting collections based on nested collections Doug Poland Java 9 09-27-2003 10:46 PM
InnerProperty Persistance for Collections containing other Collections mutex ASP .Net Building Controls 0 07-27-2003 02:45 PM



Advertisments