0

So I'm trying to solve this problem here:

I have a list of pairs:

    List<Tuple<Byte, Byte>> pairList

    pairList = ( [0,1], [0,2], [0,3], [1,2],[1,3],[2,3])

I have a custom class:

Class CustomPairList {
    byte first;
    byte second;
    int result;

    public byte getFirst();
    public byte getSecond();
    public int getResult();
}

I have another list of the above class, with this data:

List<CustomPairList> customPairlist;

customPairList = {[0,1,1000], [0,2,1000],[0,3,1000]......[0,10,1000],
                  [1,2,2000],[1,3,2000],[1,4,2000].......[1,10,2000],
                  [2,3,3000],[2,4,3000]..................[3,10,3000],
                  ''' 
                  ...
                  [14,1,4000].............................[14,10,4000]}

My goal from the above two lists would be to compare the two lists and extract the result from the customPairList (second list) for a given pair.

For example:

For the pair in the pairlist (0,1) should match the pair in the customPairList (0,1) and the "result" is 1000

Another example:

pair (1,2) in pairlist has a match in customPairList (1,2) and render its corresponding "result" value 2000

How do I achieve this?

skomisa
  • 16,436
  • 7
  • 61
  • 102
  • 1
    What have you tried thus far? – Kevin Anderson Feb 17 '19 at 05:40
  • Have tried to create two maps for the two lists. map 1 will have keys 0->1,0->2,0->3 ...same with Map 2 with keys 0->(1,1000),(2,1000),(3,1000) etc... and tried comparing the key sets to get the third value. But NO luck yet – RishiGemInI Feb 17 '19 at 05:44
  • You could try a `Map, Integer>` (e.g., {[0,1]->1000, [2,4]->3000, ...}), but you'd need to implement either or both `hashCode()` and `equals()` for `Pair`. Or a `Map>` (e.g, {0->{1->1000,2->1000,...}, ..., 2->{4->3000},...}) – Kevin Anderson Feb 17 '19 at 06:27
  • Thanks this helped :) – RishiGemInI Feb 17 '19 at 21:39

2 Answers2

0

You need .equals and .hashCode methods to be implemented in the Tuple class.

I've removed generics and change bytes to ints for simplicity:

class Tuple {
    Integer _1;
    Integer _2;

    Tuple(Integer _1, Integer _2) {
        this._1 = _1;
        this._2 = _2;
    }

    //...

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Tuple tuple = (Tuple) o;
        return (_1 == tuple._1) && (_2 == tuple._2);
    }

    @Override
    public int hashCode() {
        return Objects.hash(_1, _2);
    }
}

class CustomPairList {
    int first;
    int second;
    int result;

    CustomPairList(int first, int second, int result) {
        this.first = first;
        this.second = second;
        this.result = result;
    }

    public int getResult() {
        return result;
    }

    //...
}

Next, you could change CustomPairList to simple Map:

public class Application {

static List<Tuple> pairList = new ArrayList<>();

static {
    pairList.add(new Tuple(0, 1));
    pairList.add(new Tuple(0, 2));
    pairList.add(new Tuple(0, 3));
    pairList.add(new Tuple(1, 2));
    pairList.add(new Tuple(1, 3));
    pairList.add(new Tuple(2, 3));
}

public static void main(String[] args) {
    Map<Tuple, Integer> customPairs = new HashMap<>();
    customPairs.put(new Tuple(0, 1), 1000);
    customPairs.put(new Tuple(0, 2), 1000);
    customPairs.put(new Tuple(2, 1), 3000);
    customPairs.put(new Tuple(4, 1), 4000);

    // To get one result
    System.out.println(customPairs.get(pairList.get(0)));
    System.out.println(customPairs.get(pairList.get(1)));
    System.out.println(customPairs.get(pairList.get(2)));

    // To get all results
    int[] results = customPairs
            .entrySet()
            .stream()
            .filter(entry -> pairList.contains(entry.getKey()))
            .mapToInt(Map.Entry::getValue)
            .toArray();

    for(int i: results) {
        System.out.println(i);
    }
}
}

}

If you need to compare full list, you could try functional approach with your current implementation:

public class Application {

    static List<Tuple> pairList = new ArrayList<>();

    static {
        pairList.add(new Tuple(0, 1));
        pairList.add(new Tuple(0, 2));
        pairList.add(new Tuple(0, 3));
        pairList.add(new Tuple(1, 2));
        pairList.add(new Tuple(1, 3));
        pairList.add(new Tuple(2, 3));
    }

    static boolean filterByMask(CustomPairList customPairList) {
        return pairList.contains(new Tuple(customPairList.first, customPairList.second));
    }

    public static void main(String[] args) {
        List<CustomPairList> customPairLists = new ArrayList<>();
        customPairLists.add(new CustomPairList(0, 1, 1000));
        customPairLists.add(new CustomPairList(0, 2, 1000));
        customPairLists.add(new CustomPairList(3, 1, 3000));
        customPairLists.add(new CustomPairList(4, 1, 4000));

        int[] results = customPairLists
                .stream()
                .filter(Application::filterByMask)
                .mapToInt(CustomPairList::getResult)
                .toArray();

        for(int i: results) {
            System.out.println(i);
        }
    }
}

For more Java Stream API information see Processing Data with Java SE 8 Streams, Part 1, for example.

ilinykhma
  • 980
  • 1
  • 8
  • 14
0

Filter the customPairList by checking if each customPair in the list has a match in the pairList, get the result from filtered customPair and print it

customPairList.stream()
              .filter(customPair -> {
                         return pairList.stream()
                                        .filter(tuple -> { return tuple.getFirst().equals(customPair.getFirst()) && tuple.getSecond().equals(customPair.getSecond()); })
                                        .findFirst() != null;
                })
              .mapToInt(customPair -> { return customPair.getResult(); })
              .forEach(customPair -> { System.out.println(customPair.getResult()); });