Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 979129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:10:18+00:00 2026-05-16T04:10:18+00:00

I need to very efficiently compare two maps in Clojure/Java, and return the difference

  • 0

I need to very efficiently compare two maps in Clojure/Java, and return the difference as determined by Java’s .equals(..), with nil/null equivalent to “not present”.

i.e. I am looking for the most efficient way to a write a function like:

(map-difference
  {:a 1, :b nil, :c 2, :d 3}
  {:a 1, :b "Hidden", :c 3, :e 5})

=> {:b nil, :c 2, :d 3, :e nil}

I’d prefer an immutable Clojure map as output, but a Java map would also be fine if the performance improvement would be significant.

For what it’s worth, my basic test case / expectation of behaviour is that the following will be equal (up to the equivalence of null = “Not present”) for any two maps a and b:

a 
(merge b (difference a b))

What would be the best way to implement this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:10:19+00:00Added an answer on May 16, 2026 at 4:10 am

    I’m not sure what the absolutely most efficient way to do this is, but here’s a couple of things which may be useful:

    1. The basic expectation of behaviour from the question text is impossible: if a and b are maps such that b contains at least one key not present in a, (merge b <sth>) cannot be equal to a.

    2. If you end up going with an interop solution but then need to go back to a PersistentHashMap at some point, there’s always

      (clojure.lang.PersistentHashMap/create
       (doto (java.util.HashMap.)
         (.put :foo 1)
         (.put :bar 2)))
      ; => {:foo 1 :bar 2}
      
    3. If you need to pass the keyset of a Clojure map to a Java method, you can use

      (.keySet {:foo 1 :bar 2})
      ; => #< [:foo, :bar]>
      
    4. If all keys involved are guaranteed to be Comparable, this could be exploited for efficient computation of difference on maps with many keys (sort & merge scan). For unconstrained keys this is of course a no-go and for small maps it could actually hurt performance.

    5. It’s good to have a version written in Clojure, if only to set a baseline performance expectation. Here is one: (updated)

      (defn map-difference [m1 m2]
              (loop [m (transient {})
                     ks (concat (keys m1) (keys m2))]
                (if-let [k (first ks)]
                  (let [e1 (find m1 k)
                        e2 (find m2 k)]
                    (cond (and e1 e2 (not= (e1 1) (e2 1))) (recur (assoc! m k (e1 1)) (next ks))
                          (not e1) (recur (assoc! m k (e2 1)) (next ks))
                          (not e2) (recur (assoc! m k (e1 1)) (next ks))
                          :else    (recur m (next ks))))
                  (persistent! m))))
      

      I think that just doing (concat (keys m1) (keys m2)) and possibly duplicating some work is likely more efficient most of the time than checking a given key is in “the other map” too at every step.

    To wrap up the answer, here’s a very simple-minded set-based version with the nice property that it says what it does — if I misunderstood the spec, it should be readily apparent here. 🙂

    (defn map-difference [m1 m2]
      (let [ks1 (set (keys m1))
            ks2 (set (keys m2))
            ks1-ks2 (set/difference ks1 ks2)
            ks2-ks1 (set/difference ks2 ks1)
            ks1*ks2 (set/intersection ks1 ks2)]
        (merge (select-keys m1 ks1-ks2)
               (select-keys m2 ks2-ks1)
               (select-keys m1
                            (remove (fn [k] (= (m1 k) (m2 k)))
                                    ks1*ks2)))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to add parentheses around the lambda expression: h… May 16, 2026 at 5:15 pm
  • Editorial Team
    Editorial Team added an answer Use the Cast<T> extension method return results.Cast<ISearchable>(); May 16, 2026 at 5:14 pm
  • Editorial Team
    Editorial Team added an answer Depending on how complicated the gestures can be, you may… May 16, 2026 at 5:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Although I know this isn't very efficient, I need to get this working fast
I am finding often the need to very often wait until the next second
As part of a data regression and quality assurance exercise, I need to look
Given n enumerables of the same type that return distinct elements in ascending order,
I want to determine whether two different child nodes within an XML document are
In my program, I need to run a external command in a Ubuntu environment
I'm a long-time user of the DevExpress XPO library. It has many great features,
Erlang's Characteristics From Erlang Programming (2009): Erlang concurrency is fast and scalable. Its processes
How efficient is it to use sockets when doing IPC as compared to named
In some library code, I have a List that can contain 50,000 items or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.