How do you link each element of the collection to the other?

I have two Collection objects, I want to bind each object of these two in a readable way (HashMap, Object created on purpose, you choose).

I was thinking of two loops, one nested within the other, but perhaps this is a well known problem and has a generally accepted solution ...

What if the number of Collection objects increases above two?

EDIT after Joseph Daigle's comment: The items in the collection are all the same type, these are hotel rooms that can be booked under certain conditions.

Collection<Room> roomsFromA = getRoomsFromA();
Collection<Room> roomsFromB = getRoomsFromB();
for(Room roomA : roomsFromA){
    for(Room roomB : roomsFromB){
        //add roomA and roomB to something, this is not important for what I need
        //the important part is how you handle the part before
        //especially if Collection objects number grows beyond two
    }
}

      

EDIT 2: I'll try to explain better, sorry for the confusing question. Next example: A user requests a double and one room. The hotel has 3 double and 4 single rooms.

I need to associate each "double room" with each "private room", this is because each room has its own peculiarity, they say internet, a nicer view, and so on. So I need to give the user all the combinations to allow him to choose.

This is a simple case where there are only two Collection of Room objects involved, how do you deal with the problem of being told that the hotel and the user can offer / request more room types?

+1


a source to share


8 answers


What you are trying to do here is to get all possible permutations in the choice of X from the set Y. This is a well-known problem in discrete mathematics, and I think it is simply called combinatorial mathematics.



To solve your problem, you need to create a super collection containing all your room types. If it is an array or a list, you can use this example to calculate all the possible ways of selecting X from a set of Y. The example will give you indices from a list / array.

+1


a source


Are the collections going accurately?



HashMap map = new HashMap();
for (int i=0; i<c1.Size(); i++) {
   map.put(c1[i], c2[i]);
}

      

0


a source


Well, since I don't know, you will need to search for both of them having only one, HashMap will not work.

I would create a class that receives a pair. Sorting:

private static class Pair<K, T> {
    private K one;
    private T two;

    public Pair(K one, T two) {
        this.one = one;
        this.two = two;
    }

    /**
     * @return the one
     */
    public K getOne() {
        return one;
    }

    /**
     * @return the two
     */
    public T getTwo() {
        return two;
    }
} 

      

And create a list with them.

0


a source


Your example assumes that the return value from "roomsFromB" is a subset of the return value from "roomsFromA", so it would be more natural to model it this way:

class Room {
   public Collection<Room> getRoomsFromB { ... 
}

      

which will then allow you to:

// Collection rooms

for (Room a: rooms)
{ 
   for(Room b a.getRoomsFromB){ ...

      

This suggests that they are, of course, modeled hierarchically. If it is not, it is inappropriate, but then the question you are asking, it seems to me, is really how to model the relationship between them, and you haven't made it explicit yet.

0


a source


I suppose that:

  • Each item in collection

    1 will match one item in collection

    2
  • Collections are the same size
  • Collections can be ordered and the order matches each item in both collections

  • Order both collections (in the same order) of the property that identifies each object.
  • Iterate through both collections with one loop, build a relationship object and add it to the new collection.

See if this helps you:

public static class Room {
    private int number;
    private String name;

    public Room(int number, String name) {
        super();
        this.number = number;
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }
}

public static class RoomRelation {
    private Room a;
    private Room b;

    public RoomRelation(Room a, Room b) {
        super();
        this.a = a;
        this.b = b;
    }

    public Room getA() {
        return a;
    }

    public Room getB() {
        return b;
    }

    @Override
    public String toString() {
        return a.getName() + "(" + a.getNumber() + ") " + b.getName() + "(" + b.getNumber() + ")";
    }
}

public static void main(String[] args) {

    List<Room> roomsFromA = new ArrayList<Room>();
    List<Room> roomsFromB = new ArrayList<Room>();

    roomsFromA.add(new Room(1,"Room A"));
    roomsFromA.add(new Room(2,"Room A"));

    roomsFromB.add(new Room(1,"Room B"));
    roomsFromB.add(new Room(2,"Room B"));

    Comparator<Room> c = new Comparator<Room>() {
        @Override
        public int compare(Room o1, Room o2) {
            return o1.getNumber() - o2.getNumber();
        } };

    Collections.sort(roomsFromA, c);
    Collections.sort(roomsFromB, c);

    List<RoomRelation> relations = new ArrayList<RoomRelation>();

    for (int i = 0; i < roomsFromA.size(); i++) {
        relations.add(new RoomRelation(roomsFromA.get(i), roomsFromB.get(i)));
    }

    for (RoomRelation roomRelation : relations) {
        System.out.println(roomRelation);
    }
}

      

0


a source


You can reconsider if you need this exact logic. You are introducing an O (n ^ 2) operation that can get out of hand quickly. (Technically O (mn), but I'm assuming m and n are in roughly the same order.)

Is there another solution to your problem? Perhaps you could create a "set" that includes all A and all B, and then every object in A and B could point to that set instead?

0


a source


Your question is not entirely clear. As I understand it, you want to list all combinations of rooms, minus duplicates. Here we have the code to create a 2nd array of all room combinations. Add another nested loop for more room views.

Collection<Room> roomsFromA = getRoomsFromA();
Collection<Room> roomsFromB = getRoomsFromB();

Room[][] combinations = new Room[roomsFromA .size()][roomsFromB .size()];

int a = 0;
int b = 0;

for(Room roomA : roomsFromA){

   for(Room roomB : roomsFromB){
      combinations [a][b] = [roomA][roomB]; //Build up array
      b++; 
   }
   a++;
}

return combinations;

      

0


a source


This is a common problem. He called it a Cartesian product . If you have two collections, for example in your case, I would have two nested loops without hesitation. If not, see this question .

-1


a source







All Articles