[英]Remove duplicates from an arraylist with strings
我有一個看起來像這樣的arraylist:
public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();
我成對存放2人一組。 例如:
[Person1, Person2] [Person3, Person4]
我現在使用的算法仍然會重復,我已經嘗試了哈希映射,並使用for循環對其進行遍歷,但是它們只是將原始列表還給我。
這是代碼:
package com.company;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class createGroups
{
public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();
public static void main(String[] args){
//Define names
String[] names = {"Person1", "Person2", "Person3", "Person4"};
try
{
//Create combinations. In a try catch because of the saveFile method.
combination(names, 0, 2);
//Print all the pairs in the Arraylist x
printPairs();
} catch (IOException e)
{
e.printStackTrace();
}
}
static void combination(String[] data, int offset, int group_size) throws IOException
{
if(offset >= data.length)
{
//Create new Arraylist called foo
ArrayList<String[]> foo = new ArrayList<>();
//Create a pair of 2 (data.length = 4 / group_size = 2)
for(int i = 0; i < data.length / group_size; i++)
{
//Add the pair to foo.
foo.add(Arrays.copyOfRange(data, 2 * i, 2 * (i + 1)));
}
//Add foo to x
x.add(foo);
//saveFile(foo);
}
for(int i = offset; i < data.length; i++){
for(int j = i + 1; j < data.length; j++){
swap(data, offset, i);
swap(data, offset + 1, j);
combination(data, offset + group_size, group_size);
swap(data, offset + 1, j);
swap(data, offset, i);
}
}
}
public static void printPairs(){
//Print all pairs
for(ArrayList<String[]> q : x){
for(String[] s : q){
System.out.println(Arrays.toString(s));
}
System.out.println("\n");
}
}
private static void swap(String[] data, int a, int b){
//swap the data around.
String t = data[a];
data[a] = data[b];
data[b] = t;
}
}
現在的輸出是這樣的: 輸出
每組4個名字都是成對的“列表”(實際上不是列表,但這就是我所說的)
這是所需的輸出: 所需的輸出
但是然后您可以看到第一個和最后一個對列表基本相同,如何在我的combination
方法中進行更改
問題:
如何更改我的
combination
方法,以免創建重復的組。
以及在打印創建的列表時如何縮小列表(所需的輸出)。
如果我不夠清楚,或者我沒有很好地說明自己想要的東西,請告訴我。 我會盡力使它更清晰。
創建與此類似的對象。 它需要4個字符串(2對)。 將字符串放入數組並對該數組進行排序。 這意味着您輸入的字符串的任何組合都將轉換為一種排序的組合,但是對象內部會記住哪個人是person1,person2,...。
private class TwoPairs {
private final String person1;
private final String person2;
private final String person3;
private final String person4;
private final String[] persons;
TwoPairs(String person1, String person2, String person3, String person4) {
this.person1 = person1;
this.person2 = person2;
this.person3 = person3;
this.person4 = person4;
persons = new String[4];
persons[0] = person1;
persons[1] = person2;
persons[2] = person3;
persons[3] = person4;
// if we sort array of persons it will convert
// any input combination into single (sorted) combination
Arrays.sort(persons); // sort on 4 objects should be fast
// hashCode and equals will be comparing this sorted array
// and ignore the actual order of inputs
}
// compute hashcode from sorted array
@Override
public int hashCode() {
return Arrays.hashCode(persons);
}
// objects with equal persons arrays are considered equal
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TwoPairs other = (TwoPairs) obj;
if (!Arrays.equals(persons, other.persons)) return false;
return true;
}
// add methods which you might need
// getters for individual persons
// String getPerson1() { return person1; }
// or perhaps pairs of persons
// String[] getPair1() { return new String[] {person1, person2}; }
// add sensible toString method if you need it
}
您的ArrayList x將會像這樣改變
ArrayList<TwoPairs> x = new ArrayList<TwoPairs>();
在將新的TwoPairs對象添加到x中之前,請檢查此列表是否已包含此對象。
if (!x.contains(twoPairsObject)) {
x.add(twoPairsObject);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.