thetna
thetna

Reputation: 7143

Paritioning data using subList in java

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.

Upvotes: 2

Views: 489

Answers (3)

Edmondo
Edmondo

Reputation: 20090

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

Upvotes: 1

aioobe
aioobe

Reputation: 421090

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]]

Upvotes: 1

aioobe
aioobe

Reputation: 421090

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);

Upvotes: 1

Related Questions