这是我的代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class MyComp implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
}
public class CollectionsAlgo2 {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> al = new ArrayList<>();
// Add elements to the array list.
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("al: "+al);
Collections.sort(al);
System.out.println("al after sorting: "+al);
int pos = Collections.binarySearch(al, "B", new MyComp());
System.out.println("pos: "+pos);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是在 Collections.binarySearch(List<? extends T> …
为什么我们在比较器之前使用 new 关键字,同时将其用作排序的构造函数,因为比较器是一个接口,因此我们无法实例化它?
Collections.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return p1.getAge() - p2.getAge();
}
});
Run Code Online (Sandbox Code Playgroud) 我当前有一个包含 Pair 类型元素的 TreeSet,并且我按其值降序对它们进行排序。
以下是原始 Pair 类文档的链接:https://docs.oracle.com/javase/9/docs/api/javafx/util/Pair.html
这是声明:
TreeSet<Pair<Integer, Integer>> sortedSet = new TreeSet<Pair<Integer, Integer>>((a,b) -> b.getValue() - a.getValue());
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我尝试在集合中插入几对,例如: Pair(4, 51)、Pair(8, 85)、Pair(1, 16)、Pair(2,51)、Pair( 2,51) 未插入。
插入对的代码:
sortedSet.add(new Pair<Integer, Integer>(4, 51));
sortedSet.add(new Pair<Integer, Integer>(8, 85));
sortedSet.add(new Pair<Integer, Integer>(1, 16));
sortedSet.add(new Pair<Integer, Integer>(2, 51));
Run Code Online (Sandbox Code Playgroud)
我设法找出原因是因为已经存在一个具有相同值的对元素 - P(4,51),但是,它们具有不同的键,因此它们是不同的元素,并且我期望 Pair(2,51)插入到 P(4,51) 之前或之后。
我有办法解决这个问题吗?
我创建了一个名为的类SortByFarz,并尝试按升序对其进行排序,但出现以下错误,我不知道为什么:
error: cannot find symbol
Collections.sort(a1, new SortByFarz());
^
symbol: class SortByFarz
location: class Salaat
Run Code Online (Sandbox Code Playgroud)
代码:
error: cannot find symbol
Collections.sort(a1, new SortByFarz());
^
symbol: class SortByFarz
location: class Salaat
Run Code Online (Sandbox Code Playgroud)
请注意,这些类都在同一个Salaat.java文件中。
我有一个Collection包含5个字段的对象:
id;
entityType;
entityId;
brandId;
productId;
Run Code Online (Sandbox Code Playgroud)
要排序的ArrayList的Collection我写了下面Comparaor.
Comparator<Collection> collectionComparator = new Comparator<Collection>() {
@Override
public int compare(Collection collection1, Collection collection2) {
if(collection1.getId().equals(collection2.getId())) {
if(collection1.getEntityType().equals(collection2.getEntityType())) {
if(collection1.getEntityId().equals(collection2.getEntityId())) {
if(collection1.getBrandId().equals(collection2.getBrandId())) {
return collection1.getProductId().compareTo(collection2.getProductId());
} else {
return collection1.getBrandId().compareTo(collection2.getBrandId());
}
} else {
return collection1.getEntityId().compareTo(collection2.getEntityId());
}
} else {
return collection1.getEntityType().compareTo(collection2.getEntityType());
}
}
return collection1.getId().compareTo(collection2.getId());
}
};
Run Code Online (Sandbox Code Playgroud)
这是Comparator在具有多个字段进行比较的对象上实现的正确方法吗?
我对java.util.PriorityQueue和我自己的Comparator的这个小例子感到非常困惑:
在这段代码中,我在队列中得到了错误的顺序。结果是:5,8,7而不是5,7,8
我有什么问题Comparator<Vertex>吗?谢谢您的帮助。
public class Test {
public static void main(String[] args) {
PriorityQueue<Vertex> priorityQueue = new PriorityQueue<Vertex>(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Vertex u = (Vertex) o1;
Vertex v = (Vertex) o2;
return Integer.compare(new Integer(u.distance), new Integer(v.distance));
}
});
Vertex vertex1 = new Vertex(1);
Vertex vertex2 = new Vertex(2);
Vertex vertex3 = new Vertex(3);
Vertex vertex4 = new Vertex(4);
vertex1.distance = 8;
vertex2.distance = 5;
vertex3.distance = 7;
priorityQueue.add(vertex1); …Run Code Online (Sandbox Code Playgroud) 我正在创建一个脚本,其中用户必须提供第一个强制参数,第二个参数是可选的。如果小于 1 个或大于 2 个参数,则应抛出错误。
这是我到目前为止所做的:
if [ $# -eq 0 -o $# -gt 2]
then
echo " *** ! No arguments were supplied. *** !"
echo " Usage example is: sudo myserver pathToYourFolder [URL]"
echo ""
echo " The first argument 'pathToYourFolder' is mandatory.
It is the path to your mysite folder.
Please use like this example: sudo myserver /Users/jhon/Documents/mysite"
echo ""
echo " The second argument 'URL' is optional. It shuld be the desired URL to run with …Run Code Online (Sandbox Code Playgroud) 请你告诉我实现多级比较器的最简单方法:我有一个员工类,我想按薪水比较它们,这是通过实现比较器来完成的:
public class SalaryComparator implements Comparator<Employee> {
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o1.getSalary()-o2.getSalary());
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果薪水相同,我想通过姓氏或 ID 比较它们
我该怎么做 ?
谢谢
我正在寻找一种方法按键按升序std::multimap对条目进行排序,但如果键匹配,则按值按降序排序。
是否可以使用自定义Compare谓词来实现?
我正在尝试对一些数字进行排序。我收到“java.lang.IllegalArgumentException:比较方法违反了其一般契约!” 当我执行以下代码时出现异常。
import org.apache.commons.lang3.StringUtils;
public class ComparatorTest {
public static void main(String[] args) {
List<String> ll = List.of("1.A", "1.A.1", "10.A", "10.A.1", "10.A.2", "10.A.3", "12.A", "12.A.1", "12.A.2",
"12.A.4", "12.A.6", "1A.2", "2.A.1", "2.A.1.b", "2.A.1.b.1", "2.A.1.b.2", "2.A.1.b.3", "20.A.1",
"20.A.1.a", "20.A.1.b", "20.A.1.b.1", "20.A.1.b.2", "3.A.1", "3.A.1.a", "3.A.1.a.1", "3.A.1.a.2",
"3.A.1.a.3", "3.A.1.a.4", "3.A.1.b", "3.A.10", "6.A.1", "9.A.1");
ArrayList<String> l2 = new ArrayList<>(ll);
Collections.sort(l2, (obj1, obj2) -> {
try {
String[] prodClass1 = obj1.split("\\.");
String[] prodClass2 = obj2.split("\\.");
for (int i = 0; (i < prodClass1.length) && (i < …Run Code Online (Sandbox Code Playgroud)