List<String> list = new ArrayList<>();
list.add("MP");
list.add("Maharastra");
list.add("Karnataka");
My requirement is to get all the possible permutation from above list.
Expected Output as list is
[MP:Maharastra, MP:Karnataka, Maharastra:MP, Maharastra:Karnataka, Karnataka:MP, Karnataka:Maharastra]
Currently I added only three items in list but this list can hold any number of items.
You can use a nested loop to pair each element with every other element in the List except itself.
List<String> result = new ArrayList<>();
for(int i = 0; i < list.size(); i++){
for(int j = 0; j < list.size(); j++){
if(i != j){
result.add(list.get(i) + ":" + list.get(j));
}
}
}
System.out.println(result);
With Java 8 streams, it's much simpler:
List<String> result = list.stream().flatMap(s -> list.stream().filter(t -> (Objects.nonNull(t)
&& !t.equalsIgnoreCase(s))).map(t -> s.concat(":").concat(t))).collect(Collectors.toList());
Related
List<List<Integer>> myList = new ArrayList<>(3);
for(int i=0; i < 3; i++) {
myList.add(new ArrayList());
}
myList.get(0).add(1); // 0,0
myList.get(0).add(4); //0,1
myList.get(1).add(2); // 1,0
myList.get(1).add(5); // 1,1
myList.get(2).add(3);// 2,0
myList.get(2).add(6); //2,1
myList.get(2).add(7); //2,3
for(int i =0; i<myList.get(i).size(); i++){
for(int j=0; j<myList.size(); j++){
System.out.println(myList.get(j).get(i));
}
}
I cant figure out how to iterate through the list on a index based, with different lengths on each list. My code above only works if all lists are the same size.
Ideal output would be:
1
2
3
4
5
6
7
But I cant figure out how to print out 7 since that list is a different length. This might be a very simple solution and ill probably feel dumb after. Thanks guys
To iterate over all elements of List of Lists you need to iterate in the first for-loop over the outer List, and in the second for-loop over the inner loop at that index. There are several possibilities to achieve the iteration over all elements, as you will see in the following examples.
(Your code would also produce a IndexOutOfBoundsException for the last entry).
Iterating through a List of Lists
Option 1 (your code corrected)
for (int i = 0; i < myList.size(); i++) { // i represents index of outer List
for (int j = 0; j < myList.get(i).size(); j++) { //j represents index of the inner list at index i
System.out.println(myList.get(i).get(j));
}
}
Option 2 (using for-each loop)
for (List<Integer> innerList : myList) {
for (Integer currentPosition : innerList) {
System.out.println(currentPosition);
}
}
Option 3 (using streams)
myList.stream()
.flatMap(Collection::stream)
.forEach(System.out::println);
}
Edit due to comment: added traverse method for wanted output
If you want to print out all first entries of the inner lists first, a possibility would be to traverse your List<List<Integer>> with a method like this (method is generic, would also work with other classes):
private static <T> List<List<T>> traverse(List<List<T>> input) {
List<List<T>> result = new ArrayList<>();
for (int i = 0; i < input.size(); i++) {
for (int j = 0; j < input.get(i).size(); j++) {
if(result.size() <= j) {
result.add(new ArrayList<>());
}
result.get(j).add(input.get(i).get(j));
}
}
return result;
}
In your method then just create a new List<List<Integer>> like this and iterate over this new list of lists:
List<List<Integer>> myListTraversed = traverse(myList);
I have the following code in java,
import java.util.ArrayList;
import java.util.Objects;
public class Cars {
private static ArrayList<String> replaceDuplicates(ArrayList<String> aList) {
for (int i = 0; i < aList.size(); i++) {
for (int j = i + 1; j < aList.size(); j++) {
if (Objects.equals(aList.get(i), aList.get(j))) {
aList.remove(i);
aList.add(i, "");
aList.remove(j);
aList.add(j, "");
}
}
}
return aList;
}
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<>();
cars.add("Ford");
cars.add("Ford");
cars.add("Hyundai");
cars.add("Toyota");
cars.add("Toyota");
cars.add("Toyota");
cars.add("Ford");
cars.add("Honda");
cars.add("GMC");
System.out.println(cars);
cars = replaceDuplicates(cars);
System.out.println(cars);
}
}
The output of this code is - [, , Hyundai, , , Toyota, Ford, Honda, GMC]
I want to replace the name of cars that appear more than once in the array list with a " ". For some reason, in my code if a car's name has appeared thrice in the array list, then the third occurrence isn't getting replaced by " ".
My desired output should be like this - [, , Hyundai, , , , , Honda, GMC]
What am I doing wrong here?
Thank you in advance!
First off: you can simplify this code by using List.set instead of inserting and removing elements.
aList.remove(i);
aList.add(i, "");
would simply become
aList.set(i, "");
You're deleting both entries, if they're duplicate. This leads to the behavior of always deleting an even number of entries. For example:
a b a a c a d a
b a c a d a #first pair removed
b c d a #second pair removed
If the number of elements is odd, there will always remain one element in the list.
Obviously you need some way to remember what elements to delete. A simple approach to this would be to use a flag to remember whether a duplicate of an element has been encountered:
for (int i = 0; i < aList.size(); i++) {
//the flag
boolean duplicate = false;
for (int j = i + 1; j < aList.size(); j++) {
if (Objects.equals(aList.get(i), aList.get(j))) {
aList.set(j, ""); //remove all duplicates with an index higher than i
duplicate = true; //remember that element at index i is a duplicate
}
}
//remove last duplicate element
if(duplicate)
aList.set(i, "");
}
If you still want to use your approach, you can create references to your items before modifying list:
for (int i = 0; i < aList.size(); i++) {
String a = aList.get(i);
for (int j = i + 1; j < aList.size(); j++) {
String b = aList.get(j);
if (Objects.equals(a, b)) {
aList.remove(i);
aList.add(i, "");
aList.remove(j);
aList.add(j, "");
}
}
}
am trying to split elements in array list .
For example i have an arrayList like
List <String> elements = new ArrayList<String>();
elements // ["frnec","fdct","logic","mastro","seloger"]
The size of the elements should be dynamic...
List<List<String>> splittedlists = null;
i want to split the elements like ["frnec"] ,["fdct",logic"],["mastro", "seloger"].
splittedlists //[["frnec"] ,["fdct",logic"],["mastro", "seloger"]].
But the size of the new splittedlists should not exceed 4 ,based on that i have to chop the elements
i had got lots of code code to split lists.But i dont know how to set the maximum size of the 'splittedlists'.
but it will split by setting the target size of spitted elements
public static <T extends Object> List<List<T>> split(List<T> list, int targetSize) {
List<List<T>> lists = new ArrayList<List<T>>();
for (int i = 0; i < list.size(); i += targetSize) {
lists.add(list.subList(i, Math.min(i + targetSize, list.size())));
}
return lists;
}
My requirmeent is to split elements in the list by setting the maximum array size(here 4) of splittedlists
If elements are // ["frnec","fdct"] i want to split is as ["frnec"], ["fdct"]
if elements are // [0, 1, 2, 3, 4, 8, 6] i have to split without exceeding the new array size 4 like
[[0,1],[2,3],[4,8],[6]]
Here is my answer.. Guess you can understand what I'm doing..
List<String> list = new ArrayList<>();
for (int i = 1; i < 11; i++) {
list.add(i+"");
}
List<List<String>> lists = new ArrayList<>();
int x = list.size()/4;
int y = list.size()%4;
int j = 0;
for (int i = 0; i < list.size(); i=j) {
j = i+x;
if(y>0){
j++;
y--;
}
lists.add(list.subList(i, j));
}
System.out.println(list);
System.out.println(lists);
If you don't need to maintain the order you can use modulus (%) operator. So for 3 sublists, it would put the 0th element in the 0th sublist, the first element in the first sublist, the second element in the second sublist, the third element in the 0th sublist, the fourth element in the 1st sublist.... etc.
Something like:
for(int i = 0; i < list.size() ; i++){
listOfLists.get(i % 3).add(list.get(i));
}
List< List<Integer> > arr = new ArrayList< List<Integer> >();
// filling the array
for(List<Integer> values in arr) {
System.out.println(values[0] + values[1]);
}
Why doesn't it work? It displayes an error that ";" is expected, and that it cannot find the symbol. Simple for doesn't work either:
for(int i = 0; i < arr[]; i++) {
}
That is not the correct syntax for the for each loop in Java. Also, you cannot use the [index] notation for a List. That syntax is reserved for arrays. Here is the proper way to iterate using for each in Java.
for(List<Integer> values : arr) {
System.out.println(values.get(0) + values.get(1));
}
For the second half of your question, you should be iterating from 0 towards the size() of the List.
for(int i = 0; i < arr.size(); i++) {
}
I need to merge two lists into one, in ascending order, not duplicates, and I think my code is really close, I'm just missing something and I can't figure it out. As of now, my code is not working properly in my merge method. I think it has something to do with my loops, but I just can't work around it. My current method prints the new list, but it is not in perfect increasing order. I would appreciate any assistance in figuring out how to make this method print my merged list with ascending order using the contents of l1 and l2.
**Note: I cannot use any built-in array sorting methods.
Thanks!
import java.util.ArrayList;
import java.util.Random;
public class MergeLists {
public static ArrayList<Integer> merge(ArrayList<Integer> l1, ArrayList<Integer> l2){
ArrayList<Integer> mergedList = new ArrayList();
for (int j = 0; j < l1.size(); j++) {
if (l1.get(j) < l2.get(j)) {
mergedList.add(l1.get(j));
mergedList.add(l2.get(j));
} else {
mergedList.add(l2.get(j));
mergedList.add(l1.get(j));
}
}
for (int i = l2.size() - l1.size(); i < l2.size(); i++) {
mergedList.add(l2.get(i));
}
return mergedList;
}
public static ArrayList<Integer> makeRandomIncreasingList(int length) {
ArrayList<Integer> randomList = new ArrayList();
Random rand = new Random();
int inList = rand.nextInt(9) + 1;
int inList2 = rand.nextInt(9) + 1;
for (int i = 0; i < length; i++) {
randomList.add(inList);
inList = inList + inList2;
}
return randomList;
}
public static void doMergeTest() {
ArrayList<Integer> list1 = makeRandomIncreasingList(10);
ArrayList<Integer> list2 = makeRandomIncreasingList(20);
ArrayList<Integer> mergedList = merge(list1, list2);
System.out.println("List 1:" + list1);
System.out.println("List 2:" + list2);
System.out.println("Merged list:" + mergedList);
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("Performing merge test #" + (i + 1) + ":");
doMergeTest();
}
}
}
Remove duplicates
arrayList1.remove(arrayList2);
Then merge two arrayList:
arrayList1.addAll(arrayList2);
And Lastly sort the last
collections.sort(arrayList1);
Another way is to use SET: Set doesnt allow duplicates
(HashSet is faster depending on the List implementation class)
Set setmerge = new HashSet(list1);
setmerge.addAll(list2);
list1.clear();
list1.addAll(setmerge);
The first part of your merge() method seems ok, if you modify it a little bit. You need to be going through both lists in parallel, something like
int i = 0, j = 0;
for (; i < l1.size() && j < l2.size();)
And compare individual items and increment indices independently, as in
if (l1.get(i) < l2.get(j)) {
...
i++;
} else
...
j++;
}
The way you were doing it you were literally going in parallel, which is not always correct (think of lists [1 2 2] and [1 1 1] => your merge would look like [1 1 1 2 1 2])
Then, after your "parallel" for-loop (the one where you're iterating through both lists), one of your indices is always going to break your loop because it's at the end of its list. For in-order merging, I usually declare i, j outside the loop (you'll need then after your first for-loop, like above) and then do something like (in your notation):
for (int i1 = i; i1 < l1.size(); i1++) {
mergeList.add(l1.get(i1));
}
for (int i2 = j; i2 < l2.size(); i2++) {
mergeList.add(l2.get(i2));
}
After your first for-loop, you get to the end of exactly one of the lists (someone's going to break the loop), so exactly one of the above loops is going to get executed, and that will contain the remaining items, in order.
Edit: your last for-loop of the merge() method is not correct for your purpose.
You have assumed l2 items are always bigger than l1 items, since you are adding remainder of l2 items in the end of the list. You need to compare them with mergedList items and add them accordingly.