Java join collections using functor - java

2 collections are given with the same number of elements, say List<String>. What are elegant ways in JAVA to apply a functor on each 2 elements of collections with corresponding indexes?
Say, one example could be:
List<String> = { "APPLE", "PEAR" };
List<String> = { "BANANA", "ORANGE" };
A predicate that joins string together will result in the following List<String>:
List<String> = { "APPLEBANANA", "PEARORANGE" };

Akin to the functors found in Apache Commons Collections, I have created binary equivalents in the past.
For your situation, a binary transformer type object, which takes to two input objects and returns a single object, could be used. Here is some sample code that's conveys my approach:
// tranformer
interface BinaryTransformer<X, Y, Z> {
Z transform(X a, Y b);
}
// implementation for your problem
class ConcatTransformer implements BinaryTransformer<String, String, String> {
public String transform(String a, String b) {
return a + b;
}
}
// general use transformer
class BinaryListUtils {
public static <X, Y, Z> List<Z> collect(List<X> aList, List<Y> bList, BinaryTransformer<X, Y, Z> t) {
List<Z> ret = new ArrayList<Z>(aList.size());
Iterator<X> aIter = aList.iterator();
Iterator<Y> bIter = bList.iterator();
while(aIter.hasNext()) {
ret.add(t.transform(aIter.next(), bIter.next()));
}
}
}
HTH

A quick driver of this showed it to work. Not responsible for all test cases. :-)
List<String> combineListsHorizontally(List<String> a, List<String> b) {
assert a.size() == b.size(); // just avoids some checks
List<String> result = new ArrayList<String>(a.size());
Iterator<String> itera = a.iterator();
Iterator<String> iterb = b.iterator();
for(int i = 0; i < a.size(); i++) {
String combined = itera.next() + iterb.next();
result.add(combined);
}
return result;
}
If you need something generic, you would need to know they ahve a way that they can be joined
List<E> combineListsHorizontally(List<E> a, List<E> b) {
assert a.size() == b.size(); // just avoids some checks
List<E> result = new ArrayList<E>(a.size());
Iterator<E> itera = a.iterator();
Iterator<E> iterb = b.iterator();
for(int i = 0; i < a.size(); i++) {
E combined = new MagicCombiner<E>(a,b).get(); // define this line yourself
result.add(combined);
}
return result;
}
///////////////// EDIT - here's a working example based off #Brents (superior) example. Props to him for illustrating this pattern better than I did.
import java.util.*;
/**
* Compile: "javac BinaryListUtils"
* Usage: "java BinaryListUtils"
C:\Documents and Settings\user\My Documents>javac BinaryListUtils.java
C:\Documents and Settings\user\My Documents>java BinaryListUtils
APPLEBANANA
PEARORANGE
C:\Documents and Settings\user\My Documents>
*/
// general use transformer
class BinaryListUtils {
// tranformer
static interface BinaryTransformer<X, Y, Z> {
Z transform(X a, Y b);
}
// implementation for your problem
static class ConcatTransformer implements BinaryTransformer<String, String, String> {
public String transform(String a, String b) {
return a + b;
}
}
public static <X, Y, Z> List<Z> collect(List<X> aList, List<Y> bList, BinaryTransformer<X, Y, Z> t) {
List<Z> ret = new ArrayList<Z>(aList.size());
Iterator<X> aIter = aList.iterator();
Iterator<Y> bIter = bList.iterator();
while(aIter.hasNext()) {
ret.add(t.transform(aIter.next(), bIter.next()));
}
return ret;
}
public static void main(String[] args) {
List<String> aList = new ArrayList<String>();
List<String> bList = new ArrayList<String>();
aList.add("APPLE");
aList.add("PEAR");
bList.add("BANANA");
bList.add("ORANGE");
ConcatTransformer ct = new ConcatTransformer();
List<String> cList = BinaryListUtils.collect(aList,bList,ct);
for(String s : cList) System.out.println(s);
}
}

What you're asking for isn't a predicate. It's doing a transformation on the lists zipped together. The generic way to do this is to write an iterable zipper that will zip the two lists into an iterable of a Pair, and then apply the transformation to the pairs.
I initially thought you were asking for the intersection of two collections, which is supplied in Guava collections as Sets.intersection(Set, Set).

I think your best bet here will be to do it iteratively. I can't think of any core Java API that can do it.
public List<String> predicate(List<String> list1, List<String> list2) {
List<String> list = new ArrayList<String>();
for(int i = 0; i < list1.size(); i++) {
list.add(new StringBuilder(list1.get(i)).append(list2.get(i)).toString());
}
return list;
}
Haven't compiled / run it. Good luck.

Related

Sort ArrayList items by name

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.

Create random values of generic type in Java

I have the following:
public class RandomList {
private List<Integer> list;
public List<Integer> getList() {
return list;
}
public RandomList (int n) {
list = new ArrayList<Integer>();
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
for (int i=0; i < n; i++)
{
Integer r = rand.nextInt();
list.add(r);
}
}
}
which gives me a list filled with random Integer values. I would like to generalize this, to also get a list of random Character values or perhaps lists of other types' random values.
So what I want is a generic type version, class RandomList<T>. I can replace everywhere "Integer" by "T", but am stuck at the line Integer r = rand.nextInt(); which would read different for different types.
I am thinking of doing the following:
pass in the class of the generic type to RandomList
using instanceof check the passed in class against the desired types (Integer, Character...) and depending on the check return the proper random value
Does this make sense? Is there another/better way to achieve what I want?
First method (inferior)
In Java you can't check for the generic type, at least not without reflection. You're on the money with the generic type, so you'd do something like this:
public class RandomList<T> {
private List<T> list;
private Class<T> clazz;
public List<T> getList() {
return list;
}
public RandomList (Class<T> clazz, int n) {
this.clazz = clazz;
list = new ArrayList<T>();
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
if (clazz.isAssignableFrom(Integer.class)) {
for (int i = 0; i < n; i++) {
Integer r = rand.nextInt();
list.add(r);
}
}
else {
throw new IllegalArgumentException("Unsupported class: " + clazz.getName());
}
}
}
Second method (superior)
Alternatively, you could generalise this even further and add a Function to produce the randomised results. Note that this requires Java 8. If you're not on Java 8, you could just define an interface and construct that anonymously.
public class RandomList<T> {
private List<T> list;
public List<T> getList() {
return list;
}
public RandomList (Function<Random, T> creator, int n) {
list = new ArrayList<T>();
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
for (int i = 0; i < n; i++) {
list.add(creator.apply(rand));
}
}
}
Construct a new instance using:
RandomList<Integer> list = new RandomList<>(rand -> rand.nextInt(), 10);
Third method (cleaner)
Edit: This occurred to me later, but you seem to be using Java 8, so you could just use streams:
List<Integer> list = Stream.generate(() -> rand.nextInt()).limit(10).collect(Collectors.toList())

Making a main method to test linked lists

I need help to make a main method to test this program I've made for an assignment
Write a method to merge two linked lists of integers that are sorted into descending order. The result should be a third linked list that is the sorted combination of the original lists. Do not destroy the original lists.
import java.util.Iterator;
import java.util.LinkedList;
public class Exercise6
{
public static LinkedList<Integer> merge(LinkedList<Integer> a,LinkedList<Integer> b)
{
//Initialize variables
LinkedList<Integer> result = new LinkedList<Integer>();
Iterator<Integer> aI = a.iterator();
Iterator<Integer> bI = b.iterator();
int aTemp = 0;
int bTemp = 0;
//Get the first values from both lists using the next method
aTemp = aI.next();
bTemp = bI.next();
while(aI.hasNext() && bI.hasNext())
{
//Comparing the two elements
if(aTemp > bTemp)
{
result.add(bTemp);
bTemp = bI.next();
}
}
if(!aI.hasNext())
{
result.add(aTemp);
}
while(aI.hasNext())
{
result.add(aTemp);
aTemp = aI.next();
}
while(bI.hasNext())
{
result.add(bTemp);
bTemp = bI.next();
}
if(!aI.hasNext())
{
result.add(aTemp);
}
else
{
result.add(bTemp);
}
return result;
}
}
I think this is all you are asking for:
LinkedList<Integer> list1 = new LinkedList<Integer>();
list1.add(9);
list1.add(7);
list1.add(3);
LinkedList<Integer> list2 = new LinkedList<Integer>();
list1.add(8);
list1.add(5);
list1.add(1);
Exercise6 test = new Exercise6();
test.merge(list1,list2)
First, you are missing an else for your if in while(aI.hasNext() && bI.hasNext()). Next, I would strongly recommend you program to the List interface (instead of the concrete LinkedList type). Also, I would make the merge method generic on some comparable type T. Something like,
public static <T extends Comparable<? super T>> List<T> merge(List<T> a, List<T> b) {
// Initialize variables
List<T> result = new LinkedList<>();
Iterator<T> aI = a.iterator();
Iterator<T> bI = b.iterator();
// Get the first values from both lists using the next method
T aTemp = aI.hasNext() ? aI.next() : null;
T bTemp = bI.hasNext() ? bI.next() : null;
while (aI.hasNext() && bI.hasNext()) {
// Comparing the two elements
if (aTemp.compareTo(bTemp) < 0) {
result.add(bTemp); // <-- add the right-hand side
bTemp = bI.next();
} else {
result.add(aTemp); // <-- add the left-hand side
aTemp = aI.next();
}
}
// Add the final two values from the loop.
if (aTemp.compareTo(bTemp) < 0) {
result.add(bTemp);
result.add(aTemp);
} else {
result.add(aTemp);
result.add(bTemp);
}
while (aI.hasNext()) { // Add any remaining values from a
result.add(aI.next());
}
while (bI.hasNext()) { // Add any remaining values from b
result.add(bI.next());
}
return result;
}
Then you can test merge like
public static void main(String[] args) {
System.out.println(merge(Arrays.asList(6, 4, 2), Arrays.asList(5, 3, 1)));
System.out.println(merge(Arrays.asList("bat", "ant"),
Arrays.asList("dog", "cat")));
}
I get
[6, 5, 4, 3, 2, 1]
[dog, cat, bat, ant]
So do you write your code without ever testing it? I would suggest you get familiar with writing driver classes since as your code gets bigger you will need to test it along the way.
Create another class in the same package of your Exercise class: you can call it ExerciseDriver. Import classes as needed.
Declare Initialize and populate two linked lists. Declare a third linked lost to store the result.
Call the static method of your Exercise6 class
Print the result to verify
You could do something like:
import java.util.LinkedList;
public Class ExcerciseDriver{
public static void main (String[] args){
LinkedList<Integer> list1 = new LinkedList<>();
LinkedList<Integer> list2 = new LinkedList<>();
LinkedList<Integer> resultList;
list1.add(77);
list1.add(7);
list1.add(6);
list2.add(100);
list2.add(43);
list2.add(8);
resultList = Excercise6.merge(list1, list2);
System.out.println(resultList);
}
}
Now all you have to do is running the main method and verify the correctness of your algorithm

The best way to find out the biggest length

I have fours variables that call different methods:
public String[] longestSide(){
ArrayList<T> western = getWestern();
ArrayList<T> eastern = getEastern();
ArrayList<T> northern = getNorthern();
ArrayList<T> southern = getSouthern();
return //theLongestOne??
}
Instead of writing a bunch of if-else statements, what is the shortcut to find out which of the arraylists has the longest length and return it? Thanks!
return Collections.max(Arrays.asList(western, eastern, northern, southern),
(a, b) -> Integer.compare(a.length, b.length));
If not on Java 8 yet, the same code can be written as
return Collections.max(Arrays.asList(western, eastern, northern, southern),
new Comparator<String[]> {
#Override
public int compare(String[] a, String[] b) {
return Integer.compare(a.length, b.length));
}
});
If what you have is in fact 4 List<String>, and not 4 arrays as in the original question, then it's even simpler:
return Collections.max(Arrays.asList(western, eastern, northern, southern),
Comparator.comparing(List::size));
which is equivalent to
return Collections.max(Arrays.asList(western, eastern, northern, southern),
new Comparator<List<String>> {
#Override
public int compare(List<String> a, List<String> b) {
return Integer.compare(a.size(), b.size());
}
});
Define a method as follows:
public Sting[] longestOf(String[] a, String[] b){
if(a.length>b.length) {
return a;
}
return b;
}
Now, you can do the following in your longestSide() method:
return longestOf(longestOf(western, eastern), longestOf(northern, southern));
String maxLength = Math.max(Math.max(western.length, eastern.length), Math.max(southern.length, northern.length));
String[] longest =
western.length == maxLength ? western :
eastern.length == maxLength ? eastern :
southern.length == maxLength ? southern :
northern;
return Collections.max(Arrays.asList(getWestern(), getEastern(), getNorthern(), getSouthern()), new Comparator<String[]>() {
#Override
public int compare(String[] first, String[] second) {
return first.length - second.length;
}
});
You can do it in another way.There can be some compilation problem, but the general idea is clear:
private List<T> longest;
// Decorator method, which will return array list, but will also calculate required longest direction.
public <T> List<T> calculateAndGet(List<T> list) {
if (Objects.isNull(longest) || longest.size() < list.size()) {
longest = list.size();
}
return list;
}
public String[] longestSide(){
List<T> western = calculateAndGet(getWestern());
List<T> eastern = calculateAndGet(getEastern());
List<T> northern = calculateAndGet(getNorthern());
List<T> southern = calculateAndGet(getSouthern());
return longest.toArray(new String[longest.size()]);
}
I also changed ArrayList to List. Is there any specific logic not to use "Coding to interfaces" principle?
Implementing as suggested by Shai in a comment:
public String[] longestSide(){
String[] western = getWestern();
String[] eastern = getEastern();
String[] northern = getNorthern();
String[] southern = getSouthern();
String[] longest = null;
for (String[] a : new String[][] {western,eastern,northern,southern})
if (longest == null || a.length > longest.length)
longest = a;
return longest;
}
Or using a helper method for a List (or other Collection) as indicated by updated question:
#SafeVarargs
public static <L extends Collection<?>> L longestOf(L ... lists) {
L longest = lists[0];
for (int i = 1; i < lists.length; i++)
if (lists[i].size() > longest.size())
longest = lists[i];
return longest;
}
Use like this:
return longestOf(western, eastern, northern, southern);

Cross-product calculator in Java

I am working my way through Norvig's book on AIP. There is an exercise in it on writing a cross-product function -
(defun cross-product (fn list-1 list-2)
(mappend #'(lambda (y)
(mapcar #'(lambda (x)
(funcall fn y x))
list-2))
list-1))
(defun mappend (fn the-list)
(if (null the-list)
nil
(append (funcall fn (first the-list))
(mappend fn (rest the-list)))))
I am trying to write an implementation in Java -
interface Function<T1, T2, T3> {
public T3 function(T1 t1, T2 t2);
}
public class CrossProduct<T1, T2> {
private List<T1> list1;
private List<T2> list2;
public CrossProduct(List<T1> t1, List<T2> t2) {
this.list1 = t1;
this.list2 = t2;
}
public <T3> List<T3> calculate(Function<T1, T2, T3> fn) {
List product = new ArrayList();
for (int i = 0; i < list1.size(); i++)
for (int j = 0; j < list2.size(); j++)
product.add(fn.function(list1.get(i), list2.get(j)));
return product;
}
}
Usage -
#Test
public void testWithStrings() {
List<String> list1 = new ArrayList<String>();
list1.add("6");
list1.add("8");
List<String> list2 = new ArrayList<String>();
list2.add("2");
list2.add("3");
List<String> product = new CrossProduct<String, String>(list1, list2)
.<String> calculate(new Function<String, String, String>() {
public String function(String x, String y) {
return (String) x + (String) y;
}
});
Assert.assertEquals("62", product.get(0));
Assert.assertEquals("63", product.get(1));
Assert.assertEquals("82", product.get(2));
Assert.assertEquals("83", product.get(3));
}
Is there a better way of doing this?
It seems a little arbitrary to define your CrossProduct class that way: why are the list args member variables, whereas the fn is a method parameter? In fact, why is CrossProduct a class at all? A cross product is a list, but it's not a subtype of list, since a given list could both
be expressed as a cross product in many different ways, and
not have been constructed using the crossproduct function.
It's not natural to think of "cross product" as a type, IMO.
I would probably do something like
public class ListFunctions {
public static <T1, T2, T3> List<T3> crossProduct(List<T1> list1, List<T2> list2, Function<T1, T2, T3> fn) {
List<T3> product = new ArrayList<T3>();
for (int i = 0; i < list1.size(); i++)
for (int j = 0; j < list2.size(); j++)
product.add(fn.function(list1.get(i), list2.get(j)));
return product;
}
}
If you did want to define a class CrossProduct for some reason (e.g. to implement lazy evaluation as salman suggested), I would say it's more OO to have all three args as member variables, and have the class implement List, e.g.
public class CrossProduct<T1, T2, T3> implements List<T3> {
public CrossProduct(T1 list1, T2 list2, Function<T1, T2, T3> fn) {
// remember args...
}
// etc...
}
I don't know exactly which parameters you would like to improve. However, I would say I don't like N*M list size since it can be too big. If I knew that the result list can be immutable, then I would implement my own List which only calculates product(l1(i), l2(j)) when result.get(i*M+j-1) is called. So I have not keep a long list (perhaps just a small cache if needed).

Categories

Resources