Comparing Strings

Discussion in 'Plugin Development' started by Wbjpen, Sep 19, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Wbjpen

    I have 3 String Lists, they all have 4 strings in them. I wanna compare all the lists to each other and find all the strings that are shared, and return them whether only 2 of the lists share them or all 3, or if there's multiple strings they share.

    Here's the list
    Code:java
    1. String[] ing1 = { inv.getItem(0).getItemMeta().getLore().get(0), inv.getItem(0).getItemMeta().getLore().get(1), inv.getItem(0).getItemMeta().getLore().get(2), inv.getItem(0).getItemMeta().getLore().get(3) };
    2. String[] ing2 = { inv.getItem(1).getItemMeta().getLore().get(0), inv.getItem(1).getItemMeta().getLore().get(1), inv.getItem(1).getItemMeta().getLore().get(2), inv.getItem(1).getItemMeta().getLore().get(3) };
    3. String[] ing3 = { inv.getItem(2).getItemMeta().getLore().get(0), inv.getItem(2).getItemMeta().getLore().get(1), inv.getItem(2).getItemMeta().getLore().get(2), inv.getItem(2).getItemMeta().getLore().get(3) };
     
  2. Offline

    Scizzr

    Here's one way to do it using a Map. Tailor it to your needs.

    Code
    Code:
    /* initialize */
    String[] sa1 = { "a", "b", "c" };
    String[] sa2 = { "a", "c", "d", "e", "f" };
    String[] sa3 = { "c", "g", "h", "i" };
     
    Map<String, Integer> counts = new HashMap<String, Integer>();
     
    /* add */
    for (String s : sa1) { counts.put(s, 1+(counts.containsKey(s) ? counts.get(s) : 0)); }
    for (String s : sa2) { counts.put(s, 1+(counts.containsKey(s) ? counts.get(s) : 0)); }
    for (String s : sa3) { counts.put(s, 1+(counts.containsKey(s) ? counts.get(s) : 0)); }
     
    /* show matches */
    for (Entry<String, Integer> entry : counts.entrySet()) {
        if (entry.getValue() > 1) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
    
    Output
    Code:
    c : 3
    a : 2
    
     
  3. Offline

    Wbjpen

    Thank you, this looks like its exactly what I need. I'll mess with it and see if I can make use out of it.
     
Thread Status:
Not open for further replies.

Share This Page