Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
How to return HashMap from Spring Repository

How to return HashMap from Spring Repository

Posted on April 5, 2025 by Toma Velev

A Spring Data Repository typically returns entities or List of Object arrays List<Object[]> – for custom queries, not collections like HashMap<K, V> directly. You can structure a custom query or a method that returns a Map in your Service or in additional method, but it’s not automatic — you have to be explicit about it.

Example: Custom Repository Method Returning a Map

Let’s say you have an entity:

@Entity
public class Product {
    @Id
    private Long id;
    private String name;
    private Double price;
    // getters and setters
}

If you want to return a Map<String, Double> (e.g., product name to price), you can write a custom query like this:

Option 1: Use @Query and Stream it

public interface ProductRepository extends JpaRepository<Product, Long> {

    @Query("SELECT p.name, p.price FROM Product p")
    List<Object[]> findNameAndPricePairs();
    
    default Map<String, Double> getNamePriceMap() {
        return findNameAndPricePairs()
            .stream()
            .collect(Collectors.toMap(
                row -> (String) row[0],
                row -> (Double) row[1]
            ));
    }
}

Option 2: Use a custom DTO and convert manually

public interface ProductRepository extends JpaRepository<Product, Long> {

    @Query("SELECT new com.example.NamePriceDTO(p.name, p.price) FROM Product p")
    List<NamePriceDTO> findAllNamePrice();
}

Then convert that list into a map in service layer:

Map<String, Double> namePriceMap = repository.findAllNamePrice()
    .stream()
    .collect(Collectors.toMap(NamePriceDTO::getName, NamePriceDTO::getPrice));

TL;DR

No, Spring Data Repository doesn’t directly return HashMap<K, V>, but you can:

  • Use @Query to extract pairs,
  • Convert to Map<K, V> manually in the repo default method or in the service layer.
  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)