Skip to content Skip to sidebar Skip to footer

Are There Any More Elegant Ways Of Handling Lists In Java ? (python Vs Java)

I do like the way I can treat lists in Python. It does any recursion solution to look easy and clean. For instance the typical problem of getting all the permutations of elements i

Solution 1:

No.

But this is why Martin Odersky created Scala. He's even said that one of his goals for Scala is that it be the Python of the Java world. Scala compiles to Java bytecode and easily interops with Java compiled classes.

If that's not an option, you could take a look at the Commons Collection Library.

Solution 2:

You can use the clone() function on Lists to get a shallow copy of them. That way you won’t have to instantiate a new Object yourself but can just use the copy.

ArrayList<Integer> remaining = remaining.clone().remove(i);

Other than that, no, java does not have such operators for Lists.

Solution 3:

Apache Commons solves a lot of these kinds of issues. Have a look at ArrayUtils to do slicing. Java doesn't have a lot of syntactic sugar like scripting languages do for various reasons.

Solution 4:

Different languages require different styles. Trying to accomplish mylist[:i] + mylist[i+1:] in java is like using a hammer with a screw. Yes, you can do it, but it's not very tidy. I believe the equivalent might be something like ArrayList temp = new ArrayList(list); temp.remove(index);

I believe the following accomplishes the same task, but does so in a slightly different fashion, but doesn't suffer readability issues. Instead of creating a new list, it modifies the list, passes it on, and returns the list to its previous state when the recursive call returns.

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

publicclassPermutation {

   publicstatic void main(String[] args) {

      List<List<Integer>> result = permutations(
                                       Arrays.asList( 
                                          new Integer[] {1,2,3}));

      for (List<Integer> permutation : result) {
         System.out.println(permutation);  
      }
   }


   publicstatic<T>List<List<T>> permutations(List<T> input) {
      List<List<T>> out = new ArrayList<List<T>>();
      permutationsSlave(input, new ArrayList<T>(), out);
      return out;
   }

   publicstatic<T> void permutationsSlave(List<T> input, 
            ArrayList<T> permutation, List<List<T>> result) {

      if (input.size() == chosen.size()) {
         result.add(new ArrayList<T>(permutation));
         return;
      }

      for (T obj : input) {
         if (!permutation.contains(obj)) {
            permutation.add(obj);
            permutationsSlave(input, permutation, result);
            permutation.remove(permutation.size()-1);
         }
      } 

   }
}

The python way may look easy and cleaner, but ability to look clean often hides the fact that the solution is quite inefficient (for each level of recursion it creates 5 new lists).

But then my own solution isn't terribly efficient either -- instead of creating multiple new objects it performs redundant comparisons (though some of this could be alleviated by use of accumulators).

Solution 5:

Hi 1 you can use stack, which will be more handy.

2 the for loop can be written like this : for(Number n:numbers)

Post a Comment for "Are There Any More Elegant Ways Of Handling Lists In Java ? (python Vs Java)"