Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do you change all elements in a Collection at the same time?

Reply
Thread Tools

How do you change all elements in a Collection at the same time?

 
 
phillip.s.powell@gmail.com
Guest
Posts: n/a
 
      01-12-2007
In my native language, PHP, we have a function called array_walk()
http://www.php.net/manual/en/function.array-walk.php

That will walk through an array and perform change on every element in
the array.

I've studied the Collections within Java so far and this seems like the
right way to do it (Collections.replaceAll(List list, Object oldVal,
Object newVal)):
http://java.sun.com/j2se/1.4.2/docs/...va.lang.Object)

But looking at the API does not tell me how to do a function-based
"array_walk" like we can so easily do in PHP:

<?
$array = array(1, 2, 3, 4, 5);
@array_walk($array, create_function('&$a', 'return ($a + 1);')); //
WILL RETURN (2, 3, 4, 5, 6)
?>

So how do I do this in Java?

Thanx
Phil

 
Reply With Quote
 
 
 
 
phillip.s.powell@gmail.com
Guest
Posts: n/a
 
      01-12-2007

Paul Hamaker wrote:
> As of Java5 :
> for ( SomeClass c : somecoll )
> { //do something with (each) c in somecoll, like :
> c.change(somearg);


But what if you won't know what "change()" will be? That is, I could be
doing any kind of uniform change to the collection; I could be adding
by 1, rearranging strings, converting Objects, literally anything at
all (except removing them)

Here is where I can't find the Java equivalent of "eval()", something
in PHP that's normal to me.

Phil

> }
> --
> http://javalessons.com Paul Hamaker, SEMM
> Teaching ICT since 1987


 
Reply With Quote
 
 
 
 
Tom Hawtin
Guest
Posts: n/a
 
      01-12-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> That will walk through an array and perform change on every element in
> the array.


> $array = array(1, 2, 3, 4, 5);
> @array_walk($array, create_function('&$a', 'return ($a + 1);')); //
> WILL RETURN (2, 3, 4, 5, 6)


For a one off, you could write it explicitly as:

List<Integer> values = Arrays.asList(new Integer[] {
1, 2, 3, 4, 5
});
for (
ListIterator<Integer> iter = values.listIterator();
Integer value = iter.next();
) {
iter.set(value + 1);
}
System.out.println(values);

To abstract the 'array walking' takes a bit more work:

public interface Transform<T> {
T transform(T value);
}
....
public <T> static void transform(
List<T> values, Transform<T> transform
) {
for (
ListIterator<T> iter = values.listIterator();
T value = iter.next();
) {
iter.set(transform.transform(value));
}
}
....
List<Integer> values = Arrays.asList(new Integer[] {
1, 2, 3, 4, 5
});
transform(values, new Transform<Integer>() {
public Integer transform(Integer value) {
return value+1;
}
});
System.out.println(values);

I have used List instead of Collection, because Collection doesn't
provide a ListIterator and it's not an entirely sensible operation to
perform on a set anyway.


As you can see, abstracting loops is not a particularly natural thing to
do in Java. After splitting the method, my example is only one line
shorter and indents three levels instead of one. The inner class can
only reference final variables of the enclosing method. So it isn't done
very often in Java, at the moment.

There are various proposals to make things simpler in Java 7. These will
allow something like:

// BGGA
transform(values) {
Integer value => value+1
}

transform(values, { Integer value => value+1 });

// CICE (possibly without the <Integer>).
transform(values, Transform<Integer>(Integer value) {
return value+1;
});

// My favourite:
transform(values, new()(Integer value) {
return value+1;
});

Tom Hawtin
 
Reply With Quote
 
Rogan Dawes
Guest
Posts: n/a
 
      01-12-2007
(E-Mail Removed) wrote:
> Paul Hamaker wrote:
>> As of Java5 :
>> for ( SomeClass c : somecoll )
>> { //do something with (each) c in somecoll, like :
>> c.change(somearg);

>
> But what if you won't know what "change()" will be? That is, I could be
> doing any kind of uniform change to the collection; I could be adding
> by 1, rearranging strings, converting Objects, literally anything at
> all (except removing them)
>
> Here is where I can't find the Java equivalent of "eval()", something
> in PHP that's normal to me.
>
> Phil
>


Java is not an interpreted language. Hence it does not have "eval",
since the classes and methods may not actually be present on the classpath.

If you REALLY need "eval" functionality, and cannot achieve what you
need using sub-classes and/or interfaces, you may want to take a look at
BeanShell, which is an interpreted version of Java.

Regards,

Rogan
 
Reply With Quote
 
phillip.s.powell@gmail.com
Guest
Posts: n/a
 
      01-12-2007

Rogan Dawes wrote:
> (E-Mail Removed) wrote:
> > Paul Hamaker wrote:
> >> As of Java5 :
> >> for ( SomeClass c : somecoll )
> >> { //do something with (each) c in somecoll, like :
> >> c.change(somearg);

> >
> > But what if you won't know what "change()" will be? That is, I could be
> > doing any kind of uniform change to the collection; I could be adding
> > by 1, rearranging strings, converting Objects, literally anything at
> > all (except removing them)
> >
> > Here is where I can't find the Java equivalent of "eval()", something
> > in PHP that's normal to me.
> >
> > Phil
> >

>
> Java is not an interpreted language. Hence it does not have "eval",
> since the classes and methods may not actually be present on the classpath.
>
> If you REALLY need "eval" functionality, and cannot achieve what you
> need using sub-classes and/or interfaces, you may want to take a look at
> BeanShell, which is an interpreted version of Java.


Thanx I'll look into it. I don't however see how BeanShell will do
what I need to do within classes for a Java application to do what PHP
can do with eval() and array_walk() more easily.

Phil

>
> Regards,
>
> Rogan


 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      01-12-2007
(E-Mail Removed) wrote:
> Paul Hamaker wrote:
>> As of Java5 :
>> for ( SomeClass c : somecoll )
>> { //do something with (each) c in somecoll, like :
>> c.change(somearg);

>
> But what if you won't know what "change()" will be? That is, I could be
> doing any kind of uniform change to the collection; I could be adding
> by 1, rearranging strings, converting Objects, literally anything at
> all (except removing them)


The suggested code is not an implementation of array_walk, but a
replacement for it. Use it at the point in the code where you decide to
do a specific thing to every element of a collection. Replace
c.change(somearg) with whatever operation, or combination of operations,
you want.

>
> Here is where I can't find the Java equivalent of "eval()", something
> in PHP that's normal to me.


If you consider "eval()" to be essential for whatever you are doing, you
should use an interpreted language, such as PHP.

Patricia
 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      01-12-2007
>Paul Hamaker wrote:
>> As of Java5 :
>> for ( SomeClass c : somecoll )
>> { //do something with (each) c in somecoll, like :
>> c.change(somearg);


(E-Mail Removed) <(E-Mail Removed)> wrote:
>But what if you won't know what "change()" will be? That is, I could be
>doing any kind of uniform change to the collection; I could be adding
>by 1, rearranging strings, converting Objects, literally anything at
>all (except removing them)


Define an interface like
public interface SomeClassProcessor { void process(SomeClass c); }
and have the caller (whoever it is that DOES know what to do with each
element) pass you an instance of SomeClassProcessor. Call process(c) on each
element.

>Here is where I can't find the Java equivalent of "eval()", something
>in PHP that's normal to me.


eval() is misspelled. Change the a to an i. It completely breaks any sort of
compile-time analysis, and cannot be optimized in any reasonable way.
--
Mark Rafn (E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-13-2007

(E-Mail Removed) wrote:
> In my native language, PHP, we have a function called array_walk()
> http://www.php.net/manual/en/function.array-walk.php
>
> That will walk through an array and perform change on every element in
> the array.
>
> I've studied the Collections within Java so far and this seems like the
> right way to do it (Collections.replaceAll(List list, Object oldVal,
> Object newVal)):
> http://java.sun.com/j2se/1.4.2/docs/...va.lang.Object)
>
> But looking at the API does not tell me how to do a function-based
> "array_walk" like we can so easily do in PHP:
>
> <?
> $array = array(1, 2, 3, 4, 5);
> @array_walk($array, create_function('&$a', 'return ($a + 1);')); //
> WILL RETURN (2, 3, 4, 5, 6)
> ?>
>
> So how do I do this in Java?
>
> Thanx
> Phil

Java doesn't exactly have closures, so its hard to do stuff like what
your asking.. However, what you really want is:

Replacing "doChange" with whatever is appropriate.

You COULD make a utility class that does this for you:

public interface Transformer {
Object transform(Object o);
}

public class ListWalker {
public static void walk(List myList, Transformer transformer) {
ListIterator iterator = myList.listIterator();
while (iterator.hasNext()) {
iterator.set( transformer.transform(iterator.next()) );
}
}


Now you can call:
ListWalker.walk(myList, new Transformer() {
public Object transform(Object o) {
return new Integer(((Integer)o).intValue() + 1);
}
});

Ick... Did I just write that?
Well, you can't do little "tricks" with Java like you can in PHP, but
don't worry about that too much.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-13-2007
(E-Mail Removed) wrote:
> Paul Hamaker wrote:
>> As of Java5 :
>> for ( SomeClass c : somecoll )
>> { //do something with (each) c in somecoll, like :
>> c.change(somearg);

>
> But what if you won't know what "change()" will be? [...]


Then you don't know whether the "change" will preserve
the collection's invariants. Imagine traversing a HashSet
and applying a transformation that changed the hashCodes(),
or a TreeSet() and changing the outcome of compareTo() ...

Unrestricted change on a structured collection is not
a Good Thing. If you want mapcar, you know where to find it.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      01-13-2007
Daniel Pitts wrote:
> (E-Mail Removed) wrote:
>> In my native language, PHP, we have a function called array_walk()
>> That will walk through an array and perform change on every element in
>> the array.
>> <?
>> $array = array(1, 2, 3, 4, 5);
>> @array_walk($array, create_function('&$a', 'return ($a + 1);'));
>> // WILL RETURN (2, 3, 4, 5, 6)
>> ?>
>>
>> So how do I do this in Java?

>
> public interface Transformer {
> Object transform(Object o);
> }
>
> Now you can call:
> ListWalker.walk(myList, new Transformer() {
> public Object transform(Object o) {
> return new Integer(((Integer)o).intValue() + 1);
> }
> });
>
> Ick... Did I just write that?


It might be less icky using generics?

interface Transformer<T> {
T transform(T o);
}

ListWalker.walk(myList, new Transformer<Integer>() {
public Integer transform(Integer i) { return i+1; }
});
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Sorting the Collection(java.util.Collection) of Elements Pradeep Java 2 01-24-2007 02:33 PM
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM
To allow left click only on some elements of web page NOT all elements vikas.khengare@gmail.com Javascript 1 05-31-2006 09:26 AM



Advertisments