Java Interop for a static method call that returns an ArrayList

138 views
Skip to first unread message

Adrian Cooley

unread,
Feb 10, 2016, 8:43:53 AM2/10/16
to Clojure
Hi,

I have a Java class that has a static method call that returns a Java Array List of users as follows:

public final class DAO {

    public static List<User> getUsers() {
        List<User> userList = new ArrayList<User>();
        User user = new User("Mike");
        userList.add(user);
        return userList;
    }
}

public class User {

    private String name;

    public User(String name) {
        this.name = name;
    } 
}

In a clojure project I'm not fully sure how to retrieve the call to the list of Users and just print out the name of the User I've created here Mike. I have searched online under the keywords "clojure java interop java.util.ArrayList", but I can only find examples where in clojure you create a new arraylist instance and do operations on this newly created list. How does clojure deal with an ArrayList that is being passed into clojure from java? 

Thanks,
Adrian.

Gary Trakhman

unread,
Feb 10, 2016, 8:51:53 AM2/10/16
to Clojure
(map (fn [u] (.getName u)) (DAO/getUsers)) ?

You'll need to write a getter since name is private.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adrian Cooley

unread,
Feb 10, 2016, 11:18:48 AM2/10/16
to Clojure
Thanks a million Gary, this has been extremely helpful. I'm from an object oriented background and I'm having the usual problems of crossing into the functional thinking land, hence the straightforward question. I appreciate the help, it worked exactly as you've outlined below,

Thanks,
Adrian.

Jeremy Heiler

unread,
Feb 10, 2016, 10:02:59 PM2/10/16
to clo...@googlegroups.com
On Wed, Feb 10, 2016 at 8:43 AM, Adrian Cooley <adrian...@gmail.com> wrote:
How does clojure deal with an ArrayList that is being passed into clojure from java? 

The trick is to not think in terms of specific classes, but in terms of the different Java-related things you can do.


Also, since java.util.ArrayList implements java.util.Iterable, the seq function will accept it.

Hope that helps.
Reply all
Reply to author
Forward
0 new messages