4

I have a list of lists in java.

static ArrayList<List> permutationS = new ArrayList<List>();

The inner list is an ArrayList of integers.

List<Integer> innerList = new ArrayList<Integer>();

Now I want to select an innerList, If it contains a particular integer. How can I achieve this?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
sheldon cooper
  • 455
  • 2
  • 8
  • 22
  • Do you want every list that contains at least one instance of that integer? – k.krol.27 Jul 11 '17 at 14:37
  • @k.krol.27 there will be only one list containing a particular integer. Here is an example:[[0, 6, 13, 14], [1, 10, 11, 18], [2, 22, 4, 20], [3, -1, 21, 12], [5, 9, 17, 16], [7, 15, 19, 8]] – sheldon cooper Jul 11 '17 at 14:38

7 Answers7

1

Do it like this:

List<List<Integer>> permutationS = new ArrayList<>();

// list initialization here

int i = 5; // the Integer you are searching for

for(List<Integer> innerList : permutationS) {
   if(innerList.contains(i)) {
     // found, do something with innerList
   }
}
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
1

One solution would be to remove all of the List<Integer>s that do not contain a particular integer (10, for example):

permutationS.removeIf(list -> !list.contains(10));

Another solution would be to use streams and filtering to accomplish it:

permutationS.stream()
            .filter(list -> list.contains(10))
            .collect(Collectors.toList());

If you're only looking for a single List<Integer>, then you could use the following:

List<Integer> newList = permutationS.parallelStream()
                                    .filter(list -> list.contains(10))
                                    .findAny()
                                    .orElseGet(ArrayList::new);
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • `parallelStream()` offers no advantages other than the possibility of faster execution when more cores are available. `parallelStream()` _doesn't always guarantee faster results_. In fact, A parallel stream has a much higher overhead compared to a sequential one. There are only certain cases in which you'd want to use `parallelStream()`. – Ousmane D. Jul 11 '17 at 22:19
0

As of I do not know which Java version you are using I suggest looping throught it like

List<List<Integer>> Permutation_S = new ArrayList<List<Integer>>(); 
for(List<Integer> listInList: Permutation_S){
    for(Integer integerValue: listInList){
        if(integerValue == 2 /*your desired value*/){
            //DO SOMETHING
        }
    }
}

Of course you can use streams, if you are on Java 8 or higher

Pwnstar
  • 2,333
  • 2
  • 29
  • 52
0

A solution with stream:

int x = 3; // the number to be looked

List<Integer> selectedInnerList = permutationS.stream()
                                              .filter(s -> s.contains(x))
                                              .findFirst()
                                              .get();

Note that findFirst() returns an Optional object, so get() will throw an exception if there is no such element

Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46
0

How about:

public List getList(Object value) {

    for (List inner : Permutation_S) {
        if (inner.contains(value))
            return inner;
    }
}
Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
0

Maybe with a structure like this ?

for (List<Integer> lists : permutationS) {
    for(Integer integers : lists){
        //Your tests 
    }
}
msanford
  • 11,803
  • 11
  • 66
  • 93
Alex
  • 33
  • 12
0

This will allow you to get the list which contains the value you want, and if no-one of the inner lists contains it, it will return a new empty list :

int val = 10;
List<Integer> innerList = permutationS.stream()    //iterate over inner lists
        .filter(list -> list.contains(val))        //keep one which contains the value
        .findAny()                                 //keep only one if exists
        .orElse(new ArrayList<>());                // if no-one return new List

Example :

permutationS.add(Arrays.asList(0, 6, 13, 14));
permutationS.add(Arrays.asList(1, 10, 11, 18, 6, 78, 79, 9));
permutationS.add(Arrays.asList(2, 22, 4, 20));

List<Integer> innerList = permutationS.stream().filter(list -> list.contains(10))
                                               .findAny().orElse(new ArrayList<>());
innerList.size(); // = 8

List<Integer> innerList2 = permutationS.stream().filter(list -> list.contains(34))
                                                .findAny().orElse(new ArrayList<>());
innnerList2.size(); // = 0
azro
  • 53,056
  • 7
  • 34
  • 70