9

I want to create a string list in Clojure equivalent to the following Java code:

List<String> cities = Arrays.asList(new String[] {"Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris","Stockholm" });

: but I am unsure about the exact syntax

3
  • 3
    As a side note at least in newer Java versions you can just use Arrays.asList("Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris", "Stockholm"). Commented Feb 23, 2011 at 8:43
  • possible duplicate of Converting Clojure data structures to Java collections Commented Aug 11, 2012 at 9:23
  • for all those interested in Clojure. In Clojure, the list does not provide direct access, for direct access you need Vector (in Clojure). Commented Mar 2, 2019 at 17:24

5 Answers 5

17

Strictly speaking, examples provided above will produce vectors. List can be produced in the following way:

(list "Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris","Stockholm")

or shorter:

'("Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris","Stockholm")

In some cases this difference could be important.

Sign up to request clarification or add additional context in comments.

1 Comment

the tick in front will also prevent anything in the list from being evaluated. its good to be aware of the difference between <code>(list "hello" (str "hello " "world") "world") ==> "hello hello world world"</code> and '("hello" (str "hello " "world") "world") produces its seld with the (str "hello" "world") intact
8
(def cities ["Berlin" "Brussels" "Helsinki" "Madrid" "Oslo" "Paris" "Stockholm"])

http://en.wikibooks.org/wiki/Clojure_Programming/Tutorials_and_Tips#Clojure_for_Java_Programmers

Comments

7

in general you can use the name of a collection you want to create as the function to create it (easy to remember eh?):

(vector     "Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris","Stockholm")
(hash-map   "Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris")
(sorted-map "Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris")
(array-map  "Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris")
(hash-set   "Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris" "Stockholm")
(sorted-set "Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris" "Stockholm")

or you can use into if you dont know in advance what type you will need to create.

(defn make-trip [trip cities]
    (into trip cities))

and pass it something like (make-trip #{"home"} ["Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris"])

Comments

4

None of these are actually equivalent to:

List<String> cities = Arrays.asList(new String[] {"Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris","Stockholm" });

This is much closer to an answer to what was actually asked:

user=> (java.util.ArrayList. [1 2 3])
#<ArrayList [1, 2, 3]>
user=> (.get (java.util.ArrayList. [1 2 3]) 0)
1

Comments

2

How about:

(def cities ["Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris","Stockholm"])

The commas are optional.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.