java - Split a set to sub sets using Lists.partition or Iterable.partition -


i wondering efficient way split set sub sets?

iterable<list<long>> partitions = iterables.partition(numbers, 10); 

or

list<list<long>> partitions = lists.partition(numbers, 10); 

what difference in time complexity?

thanks

let's @ inner implementation

method partition() iterables

529  public static <t> iterable<list<t>> partition(final iterable<t> iterable, final int size) { 530    checknotnull(iterable); 531    checkargument(size > 0); 532    return new fluentiterable<list<t>>() { 533      @override 534      public iterator<list<t>> iterator() { 535        return iterators.partition(iterable.iterator(), size); 536      } 537    }; 538  } 

method partition() lists

681  public static <t> list<list<t>> partition(list<t> list, int size) { 682    checknotnull(list); 683    checkargument(size > 0); 684    return (list instanceof randomaccess) 685        ? new randomaccesspartition<t>(list, size) 686        : new partition<t>(list, size); 687  } 

in above 2 implementations, if analyse code, lists checks list instanceof randomaccess , if it's true, randomaccess interface used evaluate partition.

now if see documentation of randomaccess:

public interface randomaccess
marker interface used list implementations indicate support fast (generally constant time) random access. primary purpose of interface allow generic algorithms alter behavior provide performance when applied either random or sequential access lists.

so, guess latter have better performance.


what if our list isn't instance of randomaccess interface?

the core class used lists perform partition (source)

689  private static class partition<t> extends abstractlist<list<t>> { 690    final list<t> list; 691    final int size; 692 693    partition(list<t> list, int size) { 694      this.list = list; 695      this.size = size; 696    } 697 698    @override 699    public list<t> get(int index) { 700      checkelementindex(index, size()); 701      int start = index * size; 702      int end = math.min(start + size, list.size()); 703      return list.sublist(start, end); 704    } 705 706    @override 707    public int size() { 708      return intmath.divide(list.size(), size, roundingmode.ceiling); 709    } 710 711    @override 712    public boolean isempty() { 713      return list.isempty(); 714    } 715  } 

and core class used iterators perform partition(source)

605  private static <t> unmodifiableiterator<list<t>> partitionimpl( 606      final iterator<t> iterator, final int size, final boolean pad) { 607    checknotnull(iterator); 608    checkargument(size > 0); 609    return new unmodifiableiterator<list<t>>() { 610      @override 611      public boolean hasnext() { 612        return iterator.hasnext(); 613      } 614      @override 615      public list<t> next() { 616        if (!hasnext()) { 617          throw new nosuchelementexception(); 618        } 619        object[] array = new object[size]; 620        int count = 0; 621        (; count < size && iterator.hasnext(); count++) { 622          array[count] = iterator.next(); 623        } 624        (int = count; < size; i++) { 625          array[i] = null; // gwt 626        } 627 628        @suppresswarnings("unchecked") // put ts in 629        list<t> list = collections.unmodifiablelist( 630            (list<t>) arrays.aslist(array)); 631        return (pad || count == size) ? list : list.sublist(0, count); 632      } 633    }; 634  } 

now if analyze last 2 codes, it's not easy figure out better without doing comprehensive analysis. however, can if our list instance of randomaccess interface, lists.partition(lists, pivot); faster.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -