I have ArrayList it contains so many arrays, each array contain first name, last name. now I want to sort the list based on the last name.
Example:
ArrayList<String[]> list=new ArrayList<String[]>();
String[] name1={"koti" ,"reddy"};
String[] name2={"hanu" ,"sanjay"};
String[] name3={"ajay" ,"zedeja"};
String[] name4={"basha" ,"kadhar"};
list.add(name1);
list.add(name2);
list.add(name3);
list.add(name4);
I want the sorting order like:
basha kadhar
koti reddy
hanu sanjay
ajay zedeja
Could you please help on this ASAP, Thanks in Advance
Write a custom Comparator and supply that to the appropriate sort overload along with the data.
However, I would recommend a separate Person/Name type, instead of String arrays, as it will make data easier to keep track of and it could implement Comparable (which would eliminate/replace the need of a Comparator).
Now, when writing an applicable compare/compareTo, the code should look similar to:
int cmpLastName = a_lastName.compareTo(b_lastName);
if (cmpLastName == 0) {
// same lastname, order now based on first name
return a_firstName.compareTo(b_firstName);
} else {
// different lastname, so have enough ordering
return cmpLastName;
}
try this
Collections.sort(list, new Comparator<String[]>() {
#Override
public int compare(String[] o1, String[] o2) {
int c = o1[0].compareTo(o2[0]);
if (c != 0) {
return c;
}
return o1[1].compareTo(o2[1]);
}
});
This is how I would perform that sort operation.
public static void main(String[] args) {
ArrayList<String[]> list = new ArrayList<String[]>();
String[] name1 = { "koti", "reddy" };
String[] name2 = { "hanu", "sanjay" };
String[] name3 = { "ajay", "zedeja" };
String[] name4 = { "basha", "kadhar" };
list.add(name1);
list.add(name2);
list.add(name3);
list.add(name4);
System.out.println("Before sorting");
for (String[] r : list) {
System.out.println(Arrays.toString(r));
}
Collections.sort(list, new Comparator<String[]>() {
public int compare(String[] left, String[] right) {
if (left == null) { // null?
if (right == null) { // null == null!
return 0;
}
return -1; // null < not(null)
} else if (right == null) {
return 1; // not(null) > null.
}
// If the last names aren't the same, return the result
// of comparing the last names.
if (left[1].compareTo(right[1]) != 0) {
return left[1].compareTo(right[1]);
}
// Return the result of comparing the first names.
return left[0].compareTo(right[0]);
}
});
System.out.println("After sorting");
for (String[] r : list) {
System.out.println(Arrays.toString(r));
}
}
try this code to achieve your output.
public static void main(String []args){
ArrayList<String[]> list=new ArrayList<String[]>();
String[] name1={"koti" ,"reddy"};
String[] name2={"hanu" ,"sanjay"};
String[] name3={"ajay" ,"zedeja"};
String[] name4={"basha" ,"kadhar"};
list.add(name1);
list.add(name2);
list.add(name3);
list.add(name4);
Collections.sort(list, new Comparator<String[]>() {
#Override
public int compare(String[] s1, String[] s2) {
int i = s1[0].compareTo(s2[0]);
if (i != 0) {
return i;
}
return s1[1].compareTo(s2[1]);
}
});
System.out.println("after sorting"+"\n");
for (String[] s : list) {
for(int i=0;i<s.length;i++){
System.out.print(s[i]+"\t");
}
System.out.print("\n");
}
}
Related
I need to sort strings containing numbers
Eg :
input : {"1","12","12","2","ABC","a"}
Expected output: 1 2 12 a ABC
My output :1 2 12 12 a ABC
I am not able to remove duplicate. can anyone help me with this ?
Below is the code I used for sorting
public static String[] Sort(String[] list) {
Comparator<String> comp = new Comparator<String>() {
public int compare(String str1, String str2) {
try {
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
return Integer.compare(num1, num2);
}
catch (NumberFormatException e) {
return str1.compareTo(str2);
}
}
};
Arrays.sort(list, comp);
return list;
}
Thanks in Advance
The easiest way of approaching this problem is using Streams.
This would let us tackle this problem using a single line of code.
String[] sorted = Arrays.stream(list).distinct().sorted((s1,s2)->compare(s1,s2)).toArray(String[]::new);
edit: added use of the given comparator for full support of both number strings and other strings.
Also, the comparator should compare the strings after toLowerCase() have been applied. Resulting in
catch (NumberFormatException e) {
return str1.toLowerCase().compareTo(str2.toLowerCase());
}
Set can be used to remove dublicates
public static Set<String> sort(String[] list) {
Comparator<String> comp = new Comparator<String>() {
public int compare(String str1, String str2) {
try {
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
return Integer.compare(num1, num2);
}
catch (NumberFormatException e) {
return str1.compareTo(str2);
}
}
};
Set<String> set = new TreeSet<String>(comp);
set.addAll(Arrays.asList(list));
System.out.println(set);
return set;
}
You want to sort as well remove duplicate. it means its two process that is difficult for comparator to do both, you need to get distinct string in array, so sort and then get only distinct, there are lot of ways, but i did which is easy to understand:
public static String[] Sort(String[] list) {
Comparator<String> comp = new Comparator<String>() {
public int compare(String str1, String str2) {
try {
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
return Integer.compare(num1, num2);
}
catch (NumberFormatException e) {
return str1.compareTo(str2);
}
}
};
Arrays.sort(list, comp);
ArrayList<String> new_list = new ArrayList<String>;
if(list.length()>0){
new_list.add(list[0]);
}
for(String s : list){
//comparing with the last added string in list, if not same then add
if(!new_list.get(new_list.size()-1).equals(s)){
new_list.add(s);
}
}
return new_list.toArray(new String[new_list.size()]);
}
Try this. I included a more complicated array of the same elements to verify.
String[] vals = {"1","ABC","12","a", "1", "2", "12","2","ABC", "a", "ABC","a"};
Comparator<String> comp = ((a,b)-> {
if (a.matches("\\d+") && b.matches("\\d+")) {
return Integer.compare(Integer.valueOf(a), Integer.valueOf(b));
} else {
return a.toLowerCase().compareTo(b.toLowerCase());
}
});
String[] result = Arrays.stream(vals).distinct().sorted(comp).toArray(String[]::new);
System.out.println(Arrays.toString(result));
Prints
[1, 2, 12, a, ABC]
You've asked 2 differents question !
To answear to the first:
Comparator<String> comparator = new Comparator<String>() {
public int compare(String str1, String str2) {
return isInteger(str1) && isInteger(str2)
? Integer.compare(num1, num2)
: isInteger(str1)
? -1
: isInteger(str2)
? 1
: str1.compareTo(str2);
}
};
private boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
}
catch (NumberFormatException e) {
return false;
}
I am trying to rearrange an ArrayList based on the name of the items to be on specific index.
My list currently is this:
"SL"
"TA"
"VP"
"SP"
"PR"
and i want to rearrange them to:
"SL"
"SP"
"TA"
"PR"
"VP"
but based on the name and not in the index.
I have tried this:
for (int i=0; i< list.size(); i++){
if (list.get(i).getCategoryName().equals("SL")){
orderedDummyJSONModelList.add(list.get(i));
}
}
for (int i=0; i< list.size(); i++){
if (list.get(i).getCategoryName().equals("SP")){
orderedDummyJSONModelList.add(list.get(i));
}
}
for (int i=0; i< list.size(); i++){
if (list.get(i).getCategoryName().equals("TA")){
orderedDummyJSONModelList.add(list.get(i));
}
}
for (int i=0; i< list.size(); i++){
if (list.get(i).getCategoryName().equals("PR")){
orderedDummyJSONModelList.add(list.get(i));
}
}
for (int i=0; i< list.size(); i++){
if (list.get(i).getCategoryName().equals("VP")){
orderedDummyJSONModelList.add(list.get(i));
}
}
and it works fine, but i want to know if there is a more efficient way to do in 1 for loop or maybe a function. I do not wish to do it like this:
orderedDummyJSONModelList.add(list.get(0));
orderedDummyJSONModelList.add(list.get(3));
orderedDummyJSONModelList.add(list.get(1));
orderedDummyJSONModelList.add(list.get(4));
orderedDummyJSONModelList.add(list.get(2));
Which also works. Any ideas?
You can use Collection.Sort method as Collection.Sort(list) since list is a List<String> you will be fine. But if you want to implement a new comparator:
Collections.sort(list, new NameComparator());
class NameComparator implements Comparator<String> { //You can use classes
#Override
public int compare(String a, String b) { //You can use classes
return a.compareTo(b);
}
}
EDIT:
You can define a class comparator for your needs:
class ClassComparator implements Comparator<YourClass> { //You can use classes
#Override
public int compare(YourClass a, YourClass b) { //You can use classes
return a.name.compareTo(b.name);
}
}
The key thing here is: you need to get clear on your requirements.
In other words: of course one can shuffle around objects stored within a list. But: probably you want to do that programmatically.
In other words: the correct approach is to use the built-in Collection sorting mechanisms, but with providing a custom Comparator.
Meaning: you better find an algorithm that defines how to come from
"SL"
"TA"
"VP"
"SP"
"PR"
to
"SL"
"SP"
"TA"
"PR"
"VP"
That algorithm should go into your comparator implementation!
The point is: you have some List<X> in the first place. And X objects provide some sort of method to retrieve those strings you are showing here. Thus you have to create a Comparator<X> that works on X values; and uses some mean to get to those string values; and based on that you decide if X1 is <, = or > than some X2 object!
here´s an answer just specific for your problem working just for the given output. If the List contains anything else this might break your ordering, as there is no rule given on how to order it and the PR just randomly appears in the end.
public static void main(String[] args) {
List<String> justSomeNoRuleOrderingWithARandomPRInside = new ArrayList<String>();
justSomeNoRuleOrderingWithARandomPRInside.add("SL");
justSomeNoRuleOrderingWithARandomPRInside.add("TA");
justSomeNoRuleOrderingWithARandomPRInside.add("VP");
justSomeNoRuleOrderingWithARandomPRInside.add("SP");
justSomeNoRuleOrderingWithARandomPRInside.add("PR");
java.util.Collections.sort(justSomeNoRuleOrderingWithARandomPRInside, new NameComparator());
for(String s : justSomeNoRuleOrderingWithARandomPRInside) {
System.out.println(s);
}
}
static class NameComparator implements Comparator<String> { //You can use classes
#Override
public int compare(String a, String b) { //You can use classes
// Lets just add a T in front to make the VP appear at the end
// after TA, because why not
if (a.equals("PR")) {
a = "T"+a;
} else if(b.equals("PR")) {
b = "T"+b;
}
return a.compareTo(b);
}
}
O/P
SL
SP
TA
PR
VP
But honestly, this solution is crap, and without any clear rule on how to order these this will be doomed to fail as soon as you change anything as #GhostCat tried to explain.
How about this
// define the order
List<String> ORDER = Arrays.asList("SL", "SP", "TA", "PR", "VP");
List<MyObject> list = ...
list.sort((a, b) -> {
// lamba syntax for a Comparator<MyObject>
return Integer.compare(ORDER.indexOf(a.getString()), ORDER.indexOf(b.getString());
});
Note that this will put any strings that aren't defined in the ORDER list at the start of the sorted list. This may or may not be acceptable - it may be worth checking that only valid strings (i.e. members of ORDER) appear as the result of MyObject.getString().
Use a hashmap to store the weight of all strings (Higher the value of the hashmap means the later this string should come in the final list).
Using a Hashmap, so you can expand it later for other strings as well. It'll be easier to enhance in future.
Finally, Use a custom Comparator to do it.
Required Setup:
List<String> listOfStrings = Arrays.asList("SL", "TA", "VP", "SP", "PR");
HashMap<String, Integer> sortOrder = new HashMap<>();
sortOrder.put("SL", 0);
sortOrder.put("TA", 1);
sortOrder.put("VP", 2);
sortOrder.put("SP", 3);
sortOrder.put("PR", 4);
Streams:
List<String> sortedList = listOfStrings.stream().sorted((a, b) -> {
return Integer.compare(sortOrder.get(a), sortOrder.get(b));
}).collect(Collectors.toList());
System.out.println(sortedList);
Non-Stream:
Collections.sort(listOfStrings, (a, b) -> {
return Integer.compare(sortOrder.get(a), sortOrder.get(b));
});
OR
listOfStrings.sort((a, b) -> {
return Integer.compare(sortOrder.get(a), sortOrder.get(b));
});
System.out.println(listOfStrings);
Output:
[SL, TA, VP, SP, PR]
You can build an index map using a LinkedHashMap. This will be used to lookup the order which to sort using the category names of your items.
ItemSorting
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ItemSorting {
public static void main(String[] args) {
List<Item> list = new ArrayList<Item>();
IndexMap indexMap = new IndexMap("SL", "SP", "TA", "PR", "VP");
ItemComparator itemComparator = new ItemComparator(indexMap);
list.add(new Item("SL"));
list.add(new Item("TA"));
list.add(new Item("VP"));
list.add(new Item("SP"));
list.add(new Item("PR"));
Collections.sort(list, itemComparator);
for (Item item : list) {
System.out.println(item);
}
}
}
ItemComparator
import java.util.Comparator;
public class ItemComparator implements Comparator<Item> {
private IndexMap indexMap;
public IndexMap getIndexMap() {
return indexMap;
}
public void setIndexMap(IndexMap indexMap) {
this.indexMap = indexMap;
}
public ItemComparator(IndexMap indexMap) {
this.indexMap = indexMap;
}
#Override
public int compare(Item itemA, Item itemB) {
if (itemB == null) return -1;
if (itemA == null) return 1;
if (itemA.equals(itemB)) return 0;
Integer valA = indexMap.get(itemA.getCategoryName());
Integer valB = indexMap.get(itemB.getCategoryName());
if (valB == null) return -1;
if (valA == null) return 1;
return valA.compareTo(valB);
}
}
IndexMap
import java.util.LinkedHashMap;
public class IndexMap extends LinkedHashMap<String, Integer> {
private static final long serialVersionUID = 7891095847767899453L;
public IndexMap(String... indicies) {
super();
if (indicies != null) {
for (int i = 0; i < indicies.length; i++) {
this.put(indicies[i], new Integer(i));
}
}
}
}
Item
public class Item {
private String categoryName;
public Item(String categoryName) {
super();
this.categoryName = categoryName;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((categoryName == null) ? 0 : categoryName.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Item other = (Item) obj;
if (categoryName == null) {
if (other.categoryName != null) return false;
} else if (!categoryName.equals(other.categoryName)) return false;
return true;
}
#Override
public String toString() {
return String.format("Item { \"categoryName\" : \"%s\" }", categoryName);
}
}
Result
Item { "categoryName" : "SL" }
Item { "categoryName" : "SP" }
Item { "categoryName" : "TA" }
Item { "categoryName" : "PR" }
Item { "categoryName" : "VP" }
You coud define a helper method like this one:
public static int get(String name) {
switch (name) {
case "SL":
return 1;
case "SP":
return 2;
case "TA":
return 3;
case "PR":
return 4;
case "VP":
return 5;
default:
return 6;
}
}
and write in your main method something like:
ArrayList<String> al = new ArrayList<>();
al.add("SL");
al.add("TA");
al.add("VP");
al.add("SP");
al.add("PR");
Collections.sort(al, (o1, o2) -> return get(o1) - get(o2); );
al.forEach((s) -> System.out.println(s));
You can create a Map that maintains the position. When you iterate through the unordered list just get the position of that string value and insert into new array(not arraylist), then later if required you can convert that array to ArrayList.
Example code:
Map<String,Integer> map = new HashMap<>(); //you can may be loop through and make this map
map.put("SL", 0);
map.put("SP", 1);
map.put("TA",2);
map.put("PR",3);
map.put("VP",3);
List<String> list1 // your unordered list with values in random order
String[] newArr = new String[list1.size()];
for(String strName: list1){
int position = map.get(strName);
arr[position] = strName;
}
//newArr has ordered result.
i have created an string[] array of row values of a csv file and stored it in array list. i need to group it based on arr[0] and delete any duplicate array values in that group.
there can be n number columns each time. I have taken 3 columns for example
List<String[]> rowList = new ArrayList<String[]>();
BufferedReader reader = null;
reader = new BufferedReader(new FileReader("C:\\test.csv"));
String[] currLineSplitted;
while (reader.ready()) {
currLineSplitted = reader.readLine().split(",");
rowList.add(currLineSplitted);
}
Set<String[]> s = new TreeSet<String[]>(new Comparator<String[]>() {
#Override
public int compare(String[] o1, String[] o2) {
int cmp = 0;
if((o1[0]).compareTo(o2[0])==1){
for(int i=1;i<currLineSplitted.length;i++){
cmp = (o1[i]).compareTo(o2[i]);
}
} else {
cmp=0;
}
return cmp;
}
});
s.addAll(rowList);
List<Object> res = Arrays.asList(s.toArray());
for(Object obj:res){
String[] arr = (String[])obj;
System.out.println(arr[0]+","+arr[1]+","+arr[2]);
}
input file:
{"1","a","gh"}
{"1","a","rs"}
{"1","b","cd"}
{"2","a","xy"}
{"2","b","xy"}
{"3","a","pq"}
output:
1,a,gh
2,b,xy
Required output:
1,a,gh
1,a,rs //should be deleted as in group 1 a is repeated
1,b,cd
2,a,xy
2,b,xy //should be deleted as in group 2 xy is repeated
3,a,pq
You were almost right. I modified your compare function a little. So replace your compare function with this
#Override
public int compare(String[] o1, String[] o2) {
int cmp = 0;
if(o1[0].equals(o2[0])){//grouping 1st column
for(int i=1;i<o1.length;i++){
cmp = (o1[i]).compareTo(o2[i]);
if(cmp==0)
return cmp;// if two column matched return immediately
}
} else {
return o1[0].compareTo(o2[0]);
}
return cmp;
}
Remember there is no guarantee that String.compare will return 1. It compares two strings lexicographically and returns zero if two strings matches.
So in your code following line is creating a logical error.
o1[0]).compareTo(o2[0])==1
Read more about string comparator here
create a class for example ArrayClass
public class ArrayClass{
private String firstItem,secondItem,thirdItem;
public ArrayClass(String[] param){
firstItem = param[0];
secondItem = param[1];
thirdItem = param[2];
}
//getters and setters
}
then override the equals and hashCode method
#Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (this == obj) return true;
if (obj == null || (this.getClass() != obj.getClass())) {
return false;
}
ArrayClass aC = (ArrayClass) obj;
return (this.firstItem.equals(aC.getFirstItem())
&& this.secondItem.equals(aC.getSecondItem()))
|| (this.firstItem.equals(aC.getFirstItem())
&& this.thirdItem.equals(aC.getThirdItem()));
}
#Override
public int hashCode() {
// TODO Auto-generated method stub
// up to you how you compute your hashcode to be unique
return thirdItem != null ? thirdItem.hashCode() : 0;
}
then in your main class use Set instead of List
Set<ArrayClass> testSet = new HashSet<ArrayClass>();
then modify your while loop
while (reader.ready()) {
ArrayClass aC = new ArrayClass(reader.readLine().split(","));
testSet.add(aC);
}
to show the output
for(ArrayClass aC : testSet){
System.out.println(aC.getFirstItem()+" "+aC.getSecondItem()+" "+aC.getThirdItem());
}
output:
1,a,gh
1,b,cd
2,a,xy
3,a,pq
The problem is the following. There are multiple rows that have non-unique identifiers:
id value
0: {1,2,3}
0: {1,2,2}
1: {1,2,3}
2: {1,2,3}
2: {1,1,3}
I have the function equals that can compare multiple rows between each other. I need to write a code that selects the rows as an input of the function equals. The rows selected must have unique ids, BUT I should check all possible combinations of unique ids. For instance, if there are 5 rows with ids: 0,0,1,2,3, then I should check the following two combinations of ids: 0,1,2,3 and 0,1,2,3, because 0 apears twice. Of course, each of these two combinations will consist of unique rows that have id=0.
My code snippet is the following:
public class Test {
public static void main(String[] args) {
ArrayList<Row> allRows = new ArrayList<Row>();
allRows.add(new Row(0,new int[]{1,2,3}));
allRows.add(new Row(0,new int[]{1,2,2}));
allRows.add(new Row(1,new int[]{1,2,3}));
allRows.add(new Row(2,new int[]{1,2,3}));
allRows.add(new Row(2,new int[]{1,1,3}));
boolean answer = hasEqualUniqueRows(allRows);
}
private boolean hasEqualUniqueRows(ArrayList<Row> allTokens) {
for (int i=0; i<allTokens.size(); i++) {
ArrayList<Integer[]> rows = new ArrayList<Integer[]>();
rows = findUniqueRows(i,allTokens);
boolean answer = equalsExceptForNulls(rows);
if (answer) return true;
}
return false;
}
// Compare rows for similarities
public static <T> boolean equalsExceptForNulls(ArrayList<T[]> ts) {
for (int i=0; i<ts.size(); i++) {
for (int j=0; j<ts.size(); j++) {
if (i != j) {
boolean answer = equals(ts.get(i),ts.get(j));
if (!answer) return false;
}
}
}
return true;
}
public static <T> boolean equals(T[] ts1, T[] ts2) {
if (ts1.length != ts2.length) return false;
for(int i = 0; i < ts1.length; i++) {
T t1 = ts1[i], t2 = ts2[i];
if (t1 != null && t2 != null && !t1.equals(t2))
return false;
}
return true;
}
class Row {
private String key;
private Integer[] values;
public Row(String k,Integer[] v) {
this.key = k;
this.values = v;
}
public String getKey() {
return this.key;
}
public Integer[] getValues() {
return this.values;
}
}
}
Since the number of rows with unique ids is apriori unknown, I don´t know how to solve this problem. Any suggestions? Thanks.
Edit#1
I updated the code. Now it´s more complete. But it lacks the implementation of the function findUniqueRows. This function should select rows from the ArrayList that have unique keys (ids). Could someone help me to develop this function? Thanks.
Assuming the objective is to find every combination without duplicates you can do this with the following. The test to find duplicates is just to confirm it doesn't generate any duplicates in the first place.
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String... args) {
Bag<Integer> b = new Bag<>();
b.countFor(1, 2);
b.countFor(2, 1);
b.countFor(3, 3);
Set<String> set = new LinkedHashSet<>();
for (List<Integer> list : b.combinations()) {
System.out.println(list);
String s = list.toString();
if (!set.add(s))
System.err.println("Duplicate entry " + s);
}
}
}
class Bag<E> {
final Map<E, AtomicInteger> countMap = new LinkedHashMap<>();
void countFor(E e, int n) {
countMap.put(e, new AtomicInteger(n));
}
void decrement(E e) {
AtomicInteger ai = countMap.get(e);
if (ai.decrementAndGet() < 1)
countMap.remove(e);
}
void increment(E e) {
AtomicInteger ai = countMap.get(e);
if (ai == null)
countMap.put(e, new AtomicInteger(1));
else
ai.incrementAndGet();
}
List<List<E>> combinations() {
List<List<E>> ret = new ArrayList<>();
List<E> current = new ArrayList<>();
combinations0(ret, current);
return ret;
}
private void combinations0(List<List<E>> ret, List<E> current) {
if (countMap.isEmpty()) {
ret.add(new ArrayList<E>(current));
return;
}
int position = current.size();
current.add(null);
List<E> es = new ArrayList<>(countMap.keySet());
if (es.get(0) instanceof Comparable)
Collections.sort((List) es);
for (E e : es) {
current.set(position, e);
decrement(e);
combinations0(ret, current);
increment(e);
}
current.remove(position);
}
}
I want this method to work for any given number of arguments, i can do that with code generation(with a lot of ugly code), can it be done with recursion? if so how? I understand recursion, but i dont know how to write this.
private static void allCombinations(List<String>... lists) {
if (lists.length == 3) {
for (String s3 : lists[0]) {
for (String s1 : lists[1]) {
for (String s2 : lists[2]) {
System.out.println(s1 + "-" + s2 + "-" + s3);
}
}
}
}
if (lists.length == 2) {
for (String s3 : lists[0]) {
for (String s1 : lists[1]) {
System.out.println(s1 + "-" + s3);
}
}
}
}
Here is a simple recursive implementation:
private static void allCombinations(List<String>... lists) {
allCombinations(lists, 0, "");
}
private static void allCombinations(List<String>[] lists, int index, String pre) {
for (String s : lists[index]) {
if (index < lists.length - 1) {
allCombinations(lists, index + 1, pre + s + "-");
}else{
System.out.println(pre + s);
}
}
}
Do you particularly need it to be recursive? I'd make it non-recursive but still not special case things:
public static void allCombinations(List<String>... lists) {
int[] indexes = new int[lists.length];
while (incrementIndexes(lists, indexes)) {
StringBuilder builder = new StringBuilder();
for (int i=0; i < indexes.length; i++) {
if (i != 0) {
builder.append("-");
}
builder.append(lists[i].get(indexes[i]));
}
System.out.println(builder);
}
}
private static boolean incrementIndexes(List<String>[] lists, int[] indexes) {
for (int depth = indexes.length-1; depth >= 0; depth--) {
indexes[depth]++;
if (indexes[depth] != lists[depth].size()) {
return true;
}
// Overflowed this index. Reset to 0 and backtrack
indexes[depth] = 0;
}
// Everything is back to 0. Finished!
return false;
}
Here's a generalised recursive version. It complains about unchecked generic array creation in the test code, but the permute code itself is okay:
import java.util.*;
public class Test
{
public interface Action<T> {
void execute(Iterable<T> values);
}
public static void main(String[] args) {
List<String> first = Arrays.asList(new String[]{"1", "2", "3"});
List<String> second = Arrays.asList(new String[]{"a", "b", "c"});
List<String> third = Arrays.asList(new String[]{"x", "y"});
Action<String> action = new Action<String>() {
#Override public void execute(Iterable<String> values) {
StringBuilder builder = new StringBuilder();
for (String value : values) {
if (builder.length() != 0) {
builder.append("-");
}
builder.append(value);
}
System.out.println(builder);
}
};
permute(action, first, second, third);
}
public static <T> void permute(Action<T> action, Iterable<T>... lists) {
Stack<T> current = new Stack<T>();
permute(action, lists, 0, current);
}
public static <T> void permute(Action<T> action, Iterable<T>[] lists,
int index, Stack<T> current) {
for (T element : lists[index]) {
current.push(element);
if (index == lists.length-1) {
action.execute(current);
} else {
permute(action, lists, index+1, current);
}
current.pop();
}
}
}
here's my recursive solution with correct ordering, based on Rasmus' solution. it works only if all lists are of same size.
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> first = Arrays.asList(new String[]{"1", "2", "3"});
List<String> second = Arrays.asList(new String[]{"a", "b", "c"});
List<String> third = Arrays.asList(new String[]{"x", "y", "z"});
allCombinations (first, second, third);
}
private static void allCombinations(List<String>... lists) {
allCombinations(lists, 1, "");
}
private static void allCombinations(List<String>[] lists, int index, String pre) {
int nextHop = hop(index, lists.length-1);
for (String s : lists[index]) {
if (index != 0) {
allCombinations(lists, nextHop, pre + s + "-");
} else System.out.println(pre + s);
}
}
private static int hop(int prevIndex, int maxResult){
if (prevIndex%2 == 0){
return prevIndex-2;
} else {
if (prevIndex == maxResult)
return prevIndex-1;
int nextHop = prevIndex+2;
if (nextHop > maxResult){
return maxResult;
} else return nextHop;
}
}
}
a "correct ordering" solution that allows lists of different sizes will have to start from the last list and work it's way backwards to the first list (lists[0]), appending the element at either beginning or end of the "pre" string and passing it onward. again, the first list will print the result. I'd have coded that, but lunch is ready and girlfriend is beginning to dislike stackoverflow...