防止两个相同列表中的两个对象在java中匹配

时间:2016-11-28 23:37:49

标签: java list arraylist

我正在尝试制作一个随机匹配列表中的人的程序,以便找出谁在购买今年的礼物。

我遇到的问题是,某些人因为结婚而无法匹配,例如,Deana和Pete无法匹配,Chris和Lindsay也无法比拟。

这是我现在所拥有的,而不是试图阻止这些比赛。 谢谢,

        String[] family = {"Jen", "Chris", "Lindsay", "Erica", "Deana", "Pete"};
    ArrayList people = new ArrayList<String>();
    for(int i = 0; i<family.length; i++){
        people.add(family[i]);
    }

    int i=0;
    while (i != family.length) {
        int personTwo = (int) Math.floor(Math.random() * people.size());
        String perTst = people.get(personTwo).toString();
        if((family[i] != perTst)){

            System.out.println(family[i] +" buys for " + people.get(personTwo));

            people.remove(personTwo);
            i++;
        }


    }

2 个答案:

答案 0 :(得分:0)

只需创建情侣地图:

# (Required)
# Rule name, must be unique
name: Invlid Count Spike

# (Required)
# Type of alert.
# (Required)
# Index to search, wildcard supported
index: logstash-*

# (Required one of _cur or _ref, spike specific)
# The minimum number of events that will trigger an alert
threshold_cur: 1
#threshold_ref: 5
# The size of the window used to determine average event frequency
# We use two sliding windows each of size timeframe
# To measure the 'reference' rate and the current rate
timeframe:
  minutes: 20

# (Required, spike specific)
# The spike rule matches when the current window contains spike_height times more
# events than the reference window
spike_height: 2

# (Required, spike specific)
# The direction of the spike
# 'up' matches only spikes, 'down' matches only troughs
# 'both' matches both spikes and troughs
spike_type: "both"

# (Required)
# A list of elasticsearch filters used for find events
# These filters are joined with AND and nested in a filtered query
# For more info: http://www.elasticsearch.org/guide/en/elasticsearch/reference/c
urrent/query-dsl.html
script_fields:
  invalid_ratio:
    script:
       doc['invalid_request'].value / doc['total_request'].value
filter:
- range:
    invalid_ratio:
      gt: 0

# (Required)
# The alert is use when a match is found
alert:
- "email"

# (required, email specific)
# a list of email addresses to send alerts to
email:
- "john.doe@email.com"

然后更新您的Map<String, String> couples = new HashMap<>(); couples.put("Deana", "Pete"); couples.put("Chris", "Lindsay"); 以检查他们是否是一对夫妇:

if

答案 1 :(得分:0)

这样做的一种方法是创建人物地图给一组他们无法匹配的人。例如:

Map<String, Set<String>> notAllowed = new HashMap<>();
Set<String> deanaSet = new HashSet<>();
deanaSet.put("Pete");
Set<String> peteSet = new HashSet<>();
peteSet.put("Deana");
...
notAllowed.put("Deana", deanaSet);
notAllowed.put("Pete", peteSet);

然后,您可以为每个用户获取一组不允许的匹配项,并查看您提议添加的人是否属于不允许的用户组。