1

Currently I am working on List of Lists. I stumbled on this question (I mean, selected answer). List of Lists of Lists

@Mario Fusco is pointing to have better abstraction with classes and objects. Honestly, I don't get it whether the use-case applies to all scenario. For instance, I have a list(of list ):

[[2, 5, 6, 7], [8, 10, 12, 13, 15], [6, 13, 23, 25, 30, 34], [16, 25], [5, 16, 25, 30], [6, 25, 30], [1, 5, 9, 13, 14], [14, 25], [2, 6, 12, 34], [2, 5, 25], [2, 3, 31], [1, 16, 19], [2, 34], [3, 6], [8, 10, 12, 13, 15], [5, 10], [1, 8, 14], [3, 5], [1, 2, 8, 9, 13, 15], [3, 6, 13], [8, 15], [25, 34], [25, 31], [5, 23], [30, 31], [8, 10, 12, 13, 15], [30, 34], [8, 10, 12, 13, 15], [25, 31], [25, 31], [5, 23, 34], [3, 5, 8, 10, 12, 13, 15], [2, 8, 9, 12, 13, 15], [2, 15]]
/* I created it using ArrayList<ArrayList<Integer>> and few previous processing. 

And I need to do simple processing on it like count occurrence of 2,5 etc, find duplicate sets in list(of list) and so on. Will it be intelligent to create a class for inner list and then add those objects to list (each object containing ArrayList of these numbers). I am a bit confused with options available at this moment and need to figure out which way should work better. I would feel overwhelmed if you could explain me some example, use-case.

Edit: I am From Python background and very friendly with playing list and list of lists etc. But working on large datasets was a bit problem there (no offensive intention, there are good things like Cython, but it is just for requirement), hence shifted to Java for part of problem.

Can I change the direction of question and ask which approach will be better for memory management and CPU perspective ? Number of sublists may grow to nearly half a million to million each containing 2-10 numbers.

Community
  • 1
  • 1
akshayb
  • 1,219
  • 2
  • 18
  • 44

5 Answers5

2

And I need to do simple processing on it like count occurrence of 2,5 etc, find duplicate sets in list(of list) and so on.

If you're just doing simple processing of a list of a list of numbers, and these numbers don't really represent anything else, then your setup is fine - no point needlessly adding classes just for the sake of it.

The answer you linked to was referring to the fact that in most real-world cases like this, the embedded list of numbers will generally be representing a certain kind of object in your OO model, rather than just being a list of numbers in and of itself. If this is the case, then it's clearer to those reading the code if a new class is created to more explicitly describe what this "inner list" actually represents.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • thanks. I have done extensive programming in procedural python (for some reasons). These numbers are mathematical expression of few other things, but that doesn't matter in calculations. – akshayb Apr 30 '13 at 10:58
  • it is core mathematical requirement, hence I chose to focus on algo. – akshayb May 04 '13 at 05:58
1

And I need to do simple processing on it like count occurrence of 2,5 etc, find duplicate sets in list(of list) and so on. Will it be intelligent to create a class for inner list and then add those objects to list (each object containing ArrayList of these numbers)

It seems that all you need is to perform simple operation on the data. In that case, you are better off with List of list of list. Since it makes your code simple enough to manipulate, easy to maintain and efficient as well. Since, you would like to have your algorithm efficient and perhaps avoid unnecessary complexity.

@Mario Fusco is pointing to have better abstraction with classes and objects.

Generally, if you are modeling your business objects using list of list of list, it is not recommended since, rightly pointed by @Mario Fusco, it will lead to maintenance scenarios and readability problems. When modeling business objects, you should prefer using the domain jargon.

Ali
  • 1,409
  • 13
  • 23
0

I suggest you go object oriented and create a list of custom object which internally creates a list of integers. This way you can override equals and hashcode and also write any other method you may want.

public class CustomObject { 

    List<Integer> intlist;

    public List<Integer> getIntlist() {
        return intlist;
    }

    public void setIntlist(List<Integer> intlist) {
        this.intlist = intlist;
    }

    @Override
    public int hashCode() {
        //code here
    }

    @Override
    public boolean equals(Object obj) {
        //code here
    }

}

Can write methods here to check counts of integers and equals can compare two objects and find duplicates

Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
0

You will find no convenience/intelligence in the ArrayList, which is why Mario Fusco's point holds. You'll find yourself writing hideous amounts of code, and without a proper class to put that code into, it will just be floating around in your main class. You'll be passing your list-of-lists all over the place to methods that could have been that object's instance methods.

Don't fight Java's love for classes. Java will always win.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • I think, it will make sense to use Class as the number of inner lists grow(currently I am processing nearly 50k sublists - originally from Python). – akshayb Apr 30 '13 at 11:07
0

You can very well use ArrayList> for the purpose you have mentioned. Your use case doesn't have anything that signifies a class.

Now if these numbers were say student ids and the inner list was representing a class unit. Then you could have designed as below.

public class Unit {

  private List<Interger> studentIds;

  // Rest of code as getter setter etc.
}

And say the outer list is representing a school.

public class School {

  private List<Unit> units;

  // Rest of the code as getter setter etc.
}
dkaustubh
  • 444
  • 2
  • 15