Easy way to convert Iterable to Collection

前端 未结 19 2311
忘了有多久
忘了有多久 2020-11-28 01:39

In my application I use 3rd party library (Spring Data for MongoDB to be exact).

Methods of this library return Iterable, while the rest of my

19条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 02:37

    I came across a similar situation while trying to fetch a List of Projects, rather than the default Iterable findAll() declared in CrudRepository interface. So, in my ProjectRepository interface (which extends from CrudRepository), I simply declared the findAll() method to return a List instead of Iterable.

    package com.example.projectmanagement.dao;
    
    import com.example.projectmanagement.entities.Project;
    import org.springframework.data.repository.CrudRepository;
    import java.util.List;
    
    public interface ProjectRepository extends CrudRepository {
    
        @Override
        List findAll();
    }
    

    This is the simplest solution, I think, without requiring conversion logic or usage of external libraries.

提交回复
热议问题