HomeInterview QuestionsWhat is the difference between map and flatMap?

What is the difference between map and flatMap?

🟢 Easy Conceptual Fresher level
1Times asked
Aug 2026Last seen
Aug 2026First seen

💡 Model Answer

In Spark, map applies a function to each element of an RDD/DataFrame and returns a new element for each input element, preserving the one-to-one relationship. flatMap also applies a function, but that function can return zero, one, or many elements; the results are then flattened into a single RDD/DataFrame. For example, map on ["a", "b"] with a function that returns the length would give [1,1], whereas flatMap with a function that splits each string into characters would give ["a","b"].

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500