この質問を投稿する前に、私はここに投稿された似たような質問を見つけました。しかし、答えは文字列に基づいていました。しかし、私はここで別の状況にあります。Stringを削除しようとはしていませんが、AwardYearSourceという別のオブジェクトを削除しようとしています。このクラスには、yearというint属性があります。そのため、年に基づいて重複を削除したいと思います。つまり、2010年に複数回言及されている場合は、そのAwardYearSourceオブジェクトを削除したいと思います。どうやってやるの?
7 に答える
フィールドに基づいて要素を削除する最も簡単な方法は次のとおりです(順序を維持する):
Map<Integer, AwardYearSource> map = new LinkedHashMap<>();
for (AwardYearSource ays : list) {
map.put(ays.getYear(), ays);
}
list.clear();
list.addAll(map.values());
かなり簡単です。マップのバージョンについて何かが私を悩ませていますが(私はそれらが機能することを疑っていませんが、どういうわけか、それはやり過ぎのようです-このバージョンは必ずしもその点で優れているわけではありませんが)。答えは機能的で、スレッドセーフです(不変である
と仮定します)。AwardYearSource
public static List<AwardYearSource> removeDuplicateYears(
final Collection<AwardYearSource> awards) {
final ArrayList<AwardYearSource> input = new ArrayList<AwardYearSource>(awards);
// If there's only one element (or none), guaranteed unique.
if (input.size() <= 1) {
return input;
}
final HashSet<Integer> years = new HashSet<Integer>(input.size(), 1);
final Iterator<AwardYearSource> iter = input.iterator();
while(iter.hasNext()) {
final AwardYearSource award = iter.next();
final Integer year = award.getYear();
if (years.contains(year)) {
iter.remove();
} else {
years.add(year);
}
}
return input;
}
hashCode()
もう1つの方法は、equals(Object obj)
オブジェクトをオーバーライドすることです。同等性を判断するために使用するフィールドが1つしかないため、これは非常に簡単です。何かのようなもの:
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof AwardYearSource)) {
return false;
}
return (this.year == ((AwardYearSource)obj).year);
}
public int hashCode() {
return this.year;
}
次に、すべてのオブジェクトをに貼り付けて、Set
重複を削除できます。
Set<AwardYearSource> set = new Set<AwardYearSource>();
set.add(new AwardYearSource(2011));
set.add(new AwardYearSource(2012));
set.add(new AwardYearSource(2011));
for (AwardYearSource aws : set) {
System.out.println(aws.year);
}
マップを使用して、年をキーとしてオブジェクトを保存できます。
Map<Integer, AwardYearSource> map = new HashMap<Integer, AwardYearSource>();
map.put(someAwardYearSource1.getYear(), someAwardYearSource1);
map.put(someAwardYearSource2.getYear(), someAwardYearSource2);
etc.
最後に、マップには年ごとの一意の値が含まれます。これは、valuesメソッドで呼び出すことができます。
Collection<AwardYearSource> noDups = map.values();
キータイプとしてintを使用し、値タイプとしてクラスを使用してHashMapオブジェクトを作成します。次に、リストを繰り返し処理し、次を使用して各要素をマップに挿入します。
mymap.put(source.year, source);
次に、元のリストからすべての要素を削除し、マップを反復処理して、各要素をリストに挿入します。
AwardYearSourceクラスがequalsメソッドとhashcodeメソッドをオーバーライドする場合(Eclipseは両方を生成できます)、それらをSetに追加できます。セットには重複は含まれません。
public class AwardYearSource
{
private final int year;
public AwardYearSource(int year)
{
this.year = year;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AwardYearSource other = (AwardYearSource) obj;
if (year != other.year)
return false;
return true;
}
@Override
public String toString()
{
return String.valueOf(year);
}
public static void main(String[] args)
{
Set<AwardYearSource> set = new HashSet<AwardYearSource>();
set.add(new AwardYearSource(2000));
set.add(new AwardYearSource(2000));
set.add(new AwardYearSource(2000));
set.add(new AwardYearSource(2000));
System.out.println(set);
}
}
出力は[2000]です。セット内の1つのアイテムのみ。
Set<Integer> set = new HashSet<>();
list.removeIf(i -> set.contains(i.getYear()) ? true : !set.add(i.getYear()));
これは、特定のプロパティ(またはプロパティの組み合わせ)、この場合は年に基づいて複製が決定される場合に役立ちます。お役に立てれば。