2

I have an List implemented ArrayList having large number of indices. I would like to parition it into separate ArrayList. I have done as

      List<List<Integer>> list = new ArrayList<List<Integer>>(10000000);
      List<List<Integer>> sublist1 = list.subList(0,x)
      List<List<Integer>> sublist2 = list.subList(x,y)

and so on.I don't know if it is the correct way of partitioning. Could you please suggest me an efficient way of partitioning?

[EDIT]

I have an arrayList of something like this:

    [[1,2,3],[4,5,6],[8,9,10],[11,12,13],[44,88,1000], ......,[54,23,53]]

This list is very long.I would like to get subList such as of some small size from the above list.Each of the list will contain non-overlapping inside list:

sublist1:[[1,2,3][4,5,6] ...[.,.,.]]  sublist2:[[,,,][] .... [,,,,]]  sublistn:[[,,,][,,,]....[54,23,53]]

[EDIT] Please do not get confuse that [] is null list.I wanted to show the number of lists inside a list.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
thetna
  • 6,903
  • 26
  • 79
  • 113
  • Why do you have a List-of-Lists for storing some indexes? – aioobe Jul 18 '12 at 09:19
  • actually, inside list carries a complete information. That's why i am keeping it list inside list. – thetna Jul 18 '12 at 09:21
  • 2
    Your way of partition is correct. In your [former question](http://stackoverflow.com/q/11536768/1103872) you gave more details as to why you are doing that so I fail to see the point if this question. – Marko Topolnik Jul 18 '12 at 09:21
  • Shouldn't `sublist2` in your example be `[[][4,5,6] ... []]`? – aioobe Jul 18 '12 at 09:36
  • that may come in **sublist1**, it is because, it just second entry. I want some first **n** entries in some list and next **n** in some sublist and so on. – thetna Jul 18 '12 at 09:38
  • 1
    So flatten the list and use the first answer I gave? – aioobe Jul 18 '12 at 09:42

3 Answers3

1

I think you've confused the types slightly here.

Perhaps this is what you're after:

public static List<List<Integer>> getSubLists(List<Integer> ints, int k) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    for (int i = 0; i < ints.size(); i += k)
        result.add(ints.subList(i, Math.min(i+k, ints.size())));
    return result;
}

Example usage:

List<Integer> ints = Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90, 100);

List<List<Integer>> sublists = getSubLists(ints, 3);

// Prints [[10, 20, 30], [40, 50, 60], [70, 80, 90], [100]]
System.out.println(sublists);
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • thanks for your reply. But, this is not exactly what i am looking . I will try to make you clear by editing question – thetna Jul 18 '12 at 09:27
1

Here's a suggestion that follows your example:

private static List<List<Integer>> getSubLists(List<List<Integer>> ints, int i) {
    List<List<Integer>> sublist =
            new ArrayList<List<Integer>>(Collections.nCopies(ints.size(),
                                         Collections.<Integer>emptyList()));
    sublist.set(i, ints.get(i));
    return sublist;
}

Example usage:

List<List<Integer>> ints = Arrays.asList(Arrays.asList(1,2,3),
                                         Arrays.asList(4,5,6),
                                         Arrays.asList(54,23,53));

List<List<Integer>> subList0 = getSubLists(ints, 0);
List<List<Integer>> subList1 = getSubLists(ints, 1);
List<List<Integer>> subList2 = getSubLists(ints, 2);

System.out.println("subList0: " + subList0);
System.out.println("subList1: " + subList1);
System.out.println("subList2: " + subList2);

Output:

subList0: [[1, 2, 3], [], []]
subList1: [[], [4, 5, 6], []]
subList2: [[], [], [54, 23, 53]]
aioobe
  • 413,195
  • 112
  • 811
  • 826
1

I would suggest you have a look to the java collection API in details to answer this question, as it mainly depends on the type of access and the type of data you are accessing in the collection.

Without any further details, there are at least two good suggestions:

  1. The optimal solution would be to move to a TreeSet, as these are naturally fast data structures which can be split simply by cutting a part of the Tree. Everything is already part of the Java API and you won't have much work to do. This requires your use case not to allow for repeated values, and to have orderable data inside your collection, but will always provide you logn operations

  2. If you are stucked with non-orderable data and you admit repeated value, you will have to work with Lists. The ArrayList has an efficient implementation with access O(n) because it is backed by an Array. Making it very big poses however some problems due to the complexity of allocating a long space of adjacent memory. There is a break-even between using ArrayList or LinkedList, which you should have to find out according to your requirements, including the need for random access, insertion/deletion after the lists have been created and so on

Edmondo
  • 19,559
  • 13
  • 62
  • 115