I have multiple lists, Get the largest list where the sublists in the list are not intersections - java

for example:
#Test
public void test() {
List<Integer> list1 = Arrays.asList(1, 2);
List<Integer> list2 = Arrays.asList(3, 4);
List<Integer> list3 = Arrays.asList(5, 6, 7);
List<Integer> list4 = Arrays.asList(2, 3);
List<Integer> list5 = Arrays.asList(7);
List<Integer> list6 = Arrays.asList(3);
}
The result is stored with the following collection
List<List<List<Integer>>> result = new ArrayList<>();
i want this result,The sublists in each list in the result are disjoint,they don't need to be in order
[[1, 2],[3, 4],[5, 6, 7]]
[[1, 2],[3, 4],[7]]
[[1, 2],[3],[5, 6, 7]]
[[1, 2],[3],[7]]
[[2, 3],[5, 6, 7]]
[[2, 3],[7]]

You can use like this:
import java.util.ArrayList;
public class MyClass {
public static void main(String args[]) {
ArrayList<ArrayList<Integer>> list=new ArrayList<ArrayList<Integer>>();
list.add(new ArrayList<Integer>());
list.get(0).add(12);
System.out.println(list.get(0).get(0));
}
}
for more information here is link: https://www.geeksforgeeks.org/multidimensional-collections-in-java/

Related

Sorting a Dynamic 2D Array according to a column in JAVA

I have created an ArrayList of arrays which may contain n rows but two fixed columns. For e.g.
ArrayList<int[]> rows = new ArrayList<>();
rows.add(new int[] {3, 100});
rows.add(new int[] {4, 150});
rows.add(new int[] {4, 80});
rows.add(new int[] {2, 90});
rows.add(new int[] {2, 300});
Note that there may be more rows. I want to sort these list of rows based on the second column. How do I go about doing that? If there is any other better method to do this not based on ArrayList, please let me know that as well.
Comparator::comparingInt
Use it as shown below:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
ArrayList<int[]> rows = new ArrayList<>();
rows.add(new int[] { 3, 100 });
rows.add(new int[] { 4, 150 });
rows.add(new int[] { 4, 80 });
rows.add(new int[] { 2, 90 });
rows.add(new int[] { 2, 300 });
// Sort arrays (i.e. rows) on the value at index, 1 (i.e. second column)
rows.sort(Comparator.comparingInt(e -> e[1]));
// Display
rows.forEach(e -> System.out.println(Arrays.toString(e)));
}
}
Output:
[4, 80]
[2, 90]
[3, 100]
[4, 150]
[2, 300]
You can use comparator of collections class also..
public static void main(String[] args) {
ArrayList<int[]> rows = new ArrayList<>();
rows.add(new int[] {3, 100});
rows.add(new int[] {4, 150});
rows.add(new int[] {4, 80});
rows.add(new int[] {2, 90});
rows.add(new int[] {2, 300});
Collections.sort(rows, new Comparator<int[]>() {
#Override
public int compare(int[] o1, int[] o2) {
// TODO Auto-generated method stub
if (o1[1] > o2[1])
return 1;
else
return -1;
}
});
rows.forEach(e -> System.out.println(Arrays.toString(e)));
}

Create lists from arrays from elements at corresponding indices

I am new to java 8. Just wondering how would I do below operation using java 8 streams. Any suggestions
public static void main(String[] args) {
Integer[] arr1 = new Integer[]{1, 2, 3};
Integer[] arr2 = new Integer[]{4, 5, 6};
Integer[] arr3 = new Integer[]{7, 8, 9};
for(int i=0; i<arr1.length; i++){
System.out.println(listFromIndex(arr1[i], arr2[i], arr3[i]));
}
}
private static List<Integer> listFromIndex(Integer e, Integer e1, Integer e2) {
List<Integer> list = new ArrayList<>();
list.add(e) ;
list.add(e1) ;
list.add(e2) ;
return list;
}
Output :
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Assuming the arrays are of the same length, you can do it as:
IntStream.range(0, arr1.length)
.mapToObj(i -> listFromIndex(arr1[i], arr2[i], arr3[i]))
.forEach(System.out::println);
If all of the arrays have the same length you can use this:
List<Integer[]> arrays = Arrays.asList(arr1, arr2, arr3);
IntStream.range(0, arr1.length)
.mapToObj(i -> arrays.stream().map(a -> a[i]).collect(Collectors.toList()))
.forEach(System.out::println);
This creates a list containing all arrays. After that it creates a stream iterating over all arrays and collects the new arrays.
This will print the following result:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
If you have arrays with different lengths you can use something like this:
Integer[] arr1 = new Integer[]{1, 2, 3};
Integer[] arr2 = new Integer[]{4, 5};
Integer[] arr3 = new Integer[]{7};
List<Integer[]> arrays = Arrays.asList(arr1, arr2, arr3);
IntStream.range(0, arrays.stream().mapToInt(a -> a.length).max().orElseThrow())
.mapToObj(i -> arrays.stream().map(a -> i < a.length ? a[i] : null).collect(Collectors.toList()))
.forEach(System.out::println);
This uses the length of the largest array and checks i before collecting the resulting arrays.
The result will be this:
[1, 4, 7]
[2, 5, null]
[3, null, null]

List of List but by output is not coming right

I have been trying to insert list of integer to another list but it seems i am making some mistake my output is not coming correct could anyone please help?
You can also comment http://goo.gl/xdLP9H
public class ListInsideList {
public static void main(String[] args) {
List<List<Integer>> newList = new ArrayList<List<Integer>>();
List<Integer> tempList = new ArrayList<Integer>();
List<Integer> originalList = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++) {
originalList.add(i);//adding values to list
}
for(Integer value : originalList){
tempList.add(value);
if(tempList.size()==5)
{
newList.add(tempList);
System.out.println("iteration:"+newList);
tempList.clear();//clearing list
}
}
System.out.println("final:"+newList);
}
}
Output:
iteration:[[6, 7, 8, 9, 10], [6, 7, 8, 9, 10]]
final :[[], []]
required is:iteration:[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
final:[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
Your final answer requires there be 2 separate lists.
[1, 2, 3, 4, 5] // List One
[6, 7, 8, 9, 10] // List Two
So where do you create these two separate lists? You don't, you're just re-using the same tempList on each iteration. Adding tempList to another List doesn't divorce it from the variable name of "tempList" which is a reference to the same object which you then clear().
Instead of clear(), create a new one each time:
Instead of tempList.clear(); //clearing list
Use tempList = new ArrayList<>(); // new List
You can replace newList.add(tempList); with:
1.
note: for this to work, your tempList must be a ArrayList (for List isn't serializable);
newList.add(SerializationUtils.clone(tempList));
templist.clear();
2.
List<Integer> list = new ArrayList<>(newList.size());
list.addAll(newList);
newList.add(list);
newList.clear();
Your Lists are empty in the final print statement. There is nothing for it to print since both the lists are empty as shown,
System.out.println("final:"+newList.get(0).isEmpty());
and
System.out.println("final:"+newList.get(1).isEmpty());
so instead try,
ArrayList<ArrayList<Integer>> newList = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> tempList = new ArrayList<Integer>();
ArrayList<Integer> originalList = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> finalList = new ArrayList<ArrayList<Integer>>();
for (int i = 1; i <= 10; i++) {
originalList.add(i);//adding values to list
}
for(Integer value : originalList){
tempList.add(value);
if(tempList.size()==5)
{
newList.add(tempList);
System.out.println("iteration:"+newList);
finalList = newList;
}
}
System.out.println("final:"+finalList);
you can do something like this for minimal workload:
change
newList.add(tempList)
to
newList.add(newArrayList<Integer>(tempList));
in order to copy tempList and not using it's reference
for(Integer value : originalList){
tempList.add(value);
if(tempList.size()==5)
{
newList.add(new ArrayList<Integer>(tempList));
System.out.println("iteration:"+newList);
tempList.clear();//clearing list
}
}
System.out.println("final:"+newList);

A List, anyone can answer

public static void main(String[] args) {
List<List<Integer>> list = new ArrayList<List<Integer>>(); // final list
List<Integer> l = new ArrayList<Integer>(); // l is list
List<Integer> m = new ArrayList<Integer>(); // m is list
List<Integer> temp = new ArrayList<Integer>();
l.add(1);
l.add(2);
l.add(3); // list l
m.add(4);
m.add(5);
m.add(6); // list m
temp.addAll(l); // add l to temp
list.add(temp);
System.out.println("temp: "+temp);
System.out.println("list: "+list);
temp.addAll(m); // add m to temp1
list.add(temp);
System.out.println("temp: "+temp);
System.out.println("list: "+list);
}
the result is
temp: [1, 2, 3]
list: [[1, 2, 3]]
temp: [1, 2, 3, 4, 5, 6]
list: [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]
I think it should be:
temp: [1, 2, 3]
list: [[1, 2, 3]]
temp: [1, 2, 3, 4, 5, 6]
list: [[1, 2, 3], [1, 2, 3, 4, 5, 6]]
why the last list is [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]?
I rename your temp1 to temp in order to get compiled correctly.
This is because when you first execute "list.add(temp);"
list gets a reference to temp. So when the content of temp is changed, the content of list also gets changed.
public static void main(String[] args) {
List<List<Integer>> list = new ArrayList<List<Integer>>(); // final list
List<Integer> l = new ArrayList<Integer>(); // l is list
List<Integer> m = new ArrayList<Integer>(); // m is list
List<Integer> temp = new ArrayList<Integer>();
l.add(1);
l.add(2);
l.add(3); // list l
m.add(4);
m.add(5);
m.add(6); // list m
temp.addAll(l); // add l to temp1
list.add(temp); // list now references to temp. So when the content of temp is changed, the content of list also gets changed.
System.out.println("temp: "+temp);
System.out.println("list: "+list);
temp.addAll(m); // add m to temp. The content of temp is changed, so does the content of list
list.add(temp);
System.out.println("temp: "+temp);
System.out.println("list: "+list);
}
The list list ends up with two references to the same list (temp). You can achieve the desired behavior by creating a second temporary list, adding the contents of temp to it, then adding 4, 5, and 6 to it, then adding that temporary list to list.
I assume there is no temp1 variable in the code, it is the same as temp.
You are surprised by the fact that after adding "temp" to the "list" in the first time, the content of this first element was changed when you change temp. What you are missing is that "list" is an list of references, so its first element is a reference to "temp", not a copy of its content. Hence, whenever "temp" changes, this will be reported in the printout, even if the content of ""list" is not changed.
You can examine this behavior by adding something, say "100" to temp "just before printing, without changing "list". You will see that 100 will appear in the printout.

Initializing an array inside of adding it to an Array List

Hi so pretty much what I have been trying to do is create a method that is passed an Array List of Integers and returns an ArrayList of int arrays. I want each array inside of the returned Array List to contain on of the values of passed Array List. here is what I have so far
public static ArrayList<int[]> createPossible(ArrayList<Integer> al)
{
ArrayList<int[]> returned = new ArrayList<int[]>();
for(int i = 0; i < al.size(); i++)
{
returned.add(new int [1]{al.get(i)});
}
return returned;
}
I think that you can see the basic point of what I'm getting at here. Just cant figure out how to properly initialize each new array inside of where I'm adding it to the returned ArrayList
Just use
new int[] {al.get(i)}
The length of the array is useless, since you pass a given number of values inside the curly braces.
This will work similar to what you have described but it uses List<Integer[]> rather than a List<int[]>. If you must have List<int[]> then it could be augmented to suit your needs.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StackOverflow {
public static List<Integer[]> createPossible(List<Integer> al) {
List<Integer[]> returned = new ArrayList<Integer[]>();
for (int i = 0; i < al.size(); i++) {
returned.add(al.toArray(new Integer[0]));
}
return returned;
}
public static void main(String[] args) {
List<Integer> al = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5 });
List<Integer[]> result = createPossible(al);
System.out.println(Arrays.deepToString(result.toArray()));
}
}
The output of the code above:
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]

Categories