gpt4 book ai didi

java - 对使用 Java 中的通用类创建的 Arraylist 进行排序

转载 作者:行者123 更新时间:2023-11-29 07:42:00 25 4
gpt4 key购买 nike

我创建了一个名为 Pair 的类,它是 LR 的通用类型,基本上可以让我存储对。

我正在使用 Arraylist 来存储类型 Pair 但我不确定如何根据 对 arraylist 进行排序(并可能搜索所有元素)键/值 并打印 ArrayList。

    ArrayList<Pair> a = new ArrayList<Pair>();

Pair p = new Pair(1,1);
a.add(p);
a.add(new Pair(1,3));

//System.out.println(help please);

下面是Pair

class Pair<L,R> {

L left;
R right;

public Pair(L left, R right) {
this.left = left;
this.right = right;
}

public L getLeft() { return left; }
public R getRight() { return right; }

@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }

@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}




}

最佳答案

这是一个适用于您的代码示例(它使用了一些 Java 8 功能,但如果您受限于较低版本,则可以换出这些功能)。希望这对您有所帮助!

谢谢,邓肯

package com.hiveit;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Pair<L extends Comparable<L>, R extends Comparable<R>> implements Comparable<Pair<L, R>> {

L left;
R right;

public Pair(final L left, final R right) {
this.left = left;
this.right = right;
}

public L getLeft() {
return left;
}

public R getRight() {
return right;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (left == null ? 0 : left.hashCode());
result = prime * result + (right == null ? 0 : right.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Pair<?, ?> other = (Pair<?, ?>) obj;
if (left == null) {
if (other.left != null) {
return false;
}
} else if (!left.equals(other.left)) {
return false;
}
if (right == null) {
if (other.right != null) {
return false;
}
} else if (!right.equals(other.right)) {
return false;
}
return true;
}

@Override
public int compareTo(final Pair<L, R> other) {

final int compareLeft = left.compareTo(other.left);

if (compareLeft != 0) {
return compareLeft;
}

return right.compareTo(other.right);
}

@Override
public String toString() {
return "Pair [left=" + left + ", right=" + right + "]";
}

public static String listToString(final List<?> list) {
return list.stream().map((pair) -> {
return pair.toString();
}).collect(Collectors.joining(", "));
}

public static void main(final String[] args) {

final List<Pair<Integer, Integer>> a = new ArrayList<>();

a.add(new Pair<>(1, 1));
a.add(new Pair<>(2, 1));
a.add(new Pair<>(2, 3));
a.add(new Pair<>(1, 2));
a.add(new Pair<>(1, 3));
a.add(new Pair<>(2, 2));

final List<Pair<Integer, Integer>> sortedByKey = new ArrayList<>(a);
sortedByKey.sort((o1, o2) -> {
return o1.getLeft().compareTo(o2.getLeft());
});

sortedByKey.stream().map((pair) -> {
return pair.toString();
}).collect(Collectors.joining(", "));

final List<Pair<Integer, Integer>> sortedByValue = new ArrayList<>(a);
sortedByValue.sort((o1, o2) -> {
return o1.getRight().compareTo(o2.getRight());
});

final List<Pair<Integer, Integer>> sortedByKeyAndValue = new ArrayList<>(a);
sortedByKeyAndValue.sort((o1, o2) -> {
return o1.compareTo(o2);
});

System.out.println("Original = " + listToString(a));
System.out.println("Sorted by Left = " + listToString(sortedByKey));
System.out.println("Sorted by Right = " + listToString(sortedByValue));
System.out.println("Sorted by Left then Right = " + listToString(sortedByKeyAndValue));

}
}

关于java - 对使用 Java 中的通用类创建的 Arraylist 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29143165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com