How can I iterate using Guava multimap to print values ​​as pairs

For each key in Guava multiplayer, I need to take the appropriate values ​​and create pairs with them that are independent of order. The uniqueness of pairs does not depend on the order in which pairs are created inside the key. [A, B] = [B, A]. If the pair [B, A] is in a different key, then this should be considered as another pair. I don't know how to iterate over the values ​​so that I can print out pairs that are not order dependent. I'm not sure how to index it. For arrays, I could just use a double loop.

Here is an example of a multimap:

BE0004429: [DB00515, DB00951, DB01582]
BE0000059: [DB00603, DB01285]
BE0001052: [DB00366, DB00472, DB00856, DB01104, DB01149]

I want to take this data and change it in this format. Look carefully, I'm trying to take the values ​​of each key and make pairs.

I want to take the first value in each key and compare it with other values. Take the second value and connect it to the value after. There are no more unique pairs yet.

DB00515, DB00951
DB00515, DB01582
DB01582, DB00951

DB00603, DB01285

DB00366, DB00472
DB00366, DB00856
DB00366, DB01104
DB00366, DB01149
DB00472, DB00856
DB00472, DB01104
DB00472, DB01149
DB00856, DB01104
DB00856, DB01149
DB01104, DB01149
+4
source share
3 answers

There are two parts to this question, now that I understand it, and the fact that you are working with Multimaps is not really important. It is important to try to break down the problems into their component parts; when you make problems, they often become clearer, even seemingly trivial.

  • How to print consecutive pairs from a collection?

    We need to define a function that will take the collection (this will allow us to work with arbitrary Multimaps) and generate your pairs:

    public static <V> List<String> genPairs(Collection<V> col) {
      List<V> ls = ImmutableList.copyOf(col);
      List<String> ret = new ArrayList<>();
    
      for (int i = 0; i < ls.size()-1; i++) {
        for (int j = i+1; j < ls.size(); j++) {
          ret.add(ls.get(i)+", "+ls.get(j));
        }
      }
    
      return ret;
    }
    

    Demo:

    for(String pair : genPairs(ImmutableList.of(1,2,3))) {
      System.out.println(pair);
    }
    
    1, 2
    1, 3
    2, 3
    
  • Multimap?

    , :

    for(Collection<String> col : multimap.asMap().values()) {
      for(String pair : genPairs(col)) {
        System.out.println(pair);
      }
      System.out.println();
    }
    
+5

, , , , - , , Multimap, . . , , , OP.


, Multimap:

Multimap<String, Integer> map = new ImmutableSetMultimap.Builder<String, Integer>()
                                .put("one", 1)
                                .putAll("several", 1, 2, 3)
                                .putAll("many", 1, 2, 3, 4, 5)
                                .build();

, :

  • :

    map.toString()
    
    {one=[1], several=[1, 2, 3], many=[1, 2, 3, 4, 5]}
    
  • = > :

    for(String key : map.keySet()) {
      System.out.println(key+": "+map.get(key));
    }
    
    // OR with the asMap() view
    
    for(Entry<String,Collection<Integer> e : map.asMap().entrySet()) {
      System.out.println(e.getKey()+": "+e.getValue());
    }
    
    one: [1]
    several: [1, 2, 3]
    many: [1, 2, 3, 4, 5]
    
  • = > (, , , ListMultimap):

    for(Entry<String, Integer> e : map.entries()) {
      System.out.println(e.getKey()+": "+e.getValue());
    }
    
    one: 1
    several: 1
    several: 2
    several: 3
    many: 1
    many: 2
    many: 3
    many: 4
    many: 5
    
  • , :

    for(Integer v : map.values()) {
      System.out.println(v);
    }
    
    1
    1
    2
    3
    1
    2
    3
    4
    5
    

, Multimap. , " , " - , , , , 3 , , :

List<String> pairs = new ArrayList<>();
for(Entry<String, Integer> e : map.entries()) {
  pairs.add(e.getKey()+": "+e.getValue());
}
Collections.shuffle(pairs);
for(String s : pairs) {
  System.out.println(s);
}

Multimap .

+4

, ...

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class GuavaSample {

    public static void main(String[] args) {
        Multimap<String, String> multiMap = ArrayListMultimap.create();
        multiMap.put("BE0004429", "DB00515");
        multiMap.put("BE0004429", "DB00951");
        multiMap.put("BE0004429", "DB01582");

        multiMap.put("BE0000059", "DB00603");
        multiMap.put("BE0000059", "DB01285");

        multiMap.put("BE0001052", "DB00366");
        multiMap.put("BE0001052", "DB00472");
        multiMap.put("BE0001052", "DB00856");
        multiMap.put("BE0001052", "DB01104");
        multiMap.put("BE0001052", "DB01149");

        for (String key : multiMap.keySet()) {
            List<String> list = (List<String>) multiMap.get(key);
            for (int i = 0; i < list.size(); i++) {
                for (int j = i + 1; j < list.size(); j++) {
                    System.out.println(list.get(i) + "," + list.get(j));
                }
            }
            System.out.println();
        }
    }

}
0

Source: https://habr.com/ru/post/1539358/


All Articles