Easy way to convert Iterable to Collection

前端 未结 19 2173
忘了有多久
忘了有多久 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<T> findAll() declared in CrudRepository interface. So, in my ProjectRepository interface (which extends from CrudRepository), I simply declared the findAll() method to return a List<Project> instead of Iterable<Project>.

    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<Project, Long> {
    
        @Override
        List<Project> findAll();
    }
    

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

    0 讨论(0)
  • 2020-11-28 02:38

    This is not an answer to your question but I believe it is the solution to your problem. The interface org.springframework.data.repository.CrudRepository does indeed have methods that return java.lang.Iterable but you should not use this interface. Instead use sub interfaces, in your case org.springframework.data.mongodb.repository.MongoRepository. This interface has methods that return objects of type java.util.List.

    0 讨论(0)
  • 2020-11-28 02:41

    In JDK 8+, without using any additional libs:

    Iterator<T> source = ...;
    List<T> target = new ArrayList<>();
    source.forEachRemaining(target::add);
    

    Edit: The above one is for Iterator. If you are dealing with Iterable,

    iterable.forEach(target::add);
    
    0 讨论(0)
  • 2020-11-28 02:41

    You can use Eclipse Collections factories:

    Iterable<String> iterable = Arrays.asList("1", "2", "3");
    
    MutableList<String> list = Lists.mutable.withAll(iterable);
    MutableSet<String> set = Sets.mutable.withAll(iterable);
    MutableSortedSet<String> sortedSet = SortedSets.mutable.withAll(iterable);
    MutableBag<String> bag = Bags.mutable.withAll(iterable);
    MutableSortedBag<String> sortedBag = SortedBags.mutable.withAll(iterable);
    

    You can also convert the Iterable to a LazyIterable and use the converter methods or any of the other available APIs available.

    Iterable<String> iterable = Arrays.asList("1", "2", "3");
    LazyIterable<String> lazy = LazyIterate.adapt(iterable);
    
    MutableList<String> list = lazy.toList();
    MutableSet<String> set = lazy.toSet();
    MutableSortedSet<String> sortedSet = lazy.toSortedSet();
    MutableBag<String> bag = lazy.toBag();
    MutableSortedBag<String> sortedBag = lazy.toSortedBag();
    

    All of the above Mutable types extend java.util.Collection.

    Note: I am a committer for Eclipse Collections.

    0 讨论(0)
  • 2020-11-28 02:43

    You may write your own utility method for this as well:

    public static <E> Collection<E> makeCollection(Iterable<E> iter) {
        Collection<E> list = new ArrayList<E>();
        for (E item : iter) {
            list.add(item);
        }
        return list;
    }
    
    0 讨论(0)
  • 2020-11-28 02:45

    Try StickyList from Cactoos:

    List<String> list = new StickyList<>(iterable);
    
    0 讨论(0)
提交回复
热议问题