我的ArrayList
里面有Object
个。对象具有String name
和String style
以及int strength
。
我想在我的ArrayList
中按名称排序,然后将其列为标准输出。
如何使用名为NameComparator
的方法?
答案 0 :(得分:1)
您需要实施Comparator
:
public class NameComparator implements Comparator<MyClass> {
@Override
public int compare (MyClass c1, MyClass c2) {
return c1.getName().compareTo(c2.getName());
}
}
然后,您可以将其与Collections.sort(List)
结合使用:
List<MyClass> list = <initialize and fill with data>;
Collections.sort(list, new NameComparator());
答案 1 :(得分:0)
您可以使用NameComparator比较器来使用Collections.sort(T [],Comparator c)。 了解详情here
答案 2 :(得分:0)
您可以使用java Comparator / Comparable对列表进行排序
示例员工类
示例: - &gt;
class MySalaryComp implements Comparator<Empl>{
@Override
public int compare(Empl e1, Empl e2) {
if(e1.getSalary() < e2.getSalary()){
return 1;
} else {
return -1;
}
}
}
和员工类
class Empl{
private String name;
private int salary;
public Empl(String n, int s){
this.name = n;
this.salary = s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString(){
return "Name: "+this.name+"-- Salary: "+this.salary;
}
}
现在您可以将此比较器用作
Collections.sort(list,new MySalaryComp());
答案 3 :(得分:0)
您自己编写Comparator
课程,然后使用List
Collections.sort(list, new MyComparator());
进行排序
答案 4 :(得分:0)
您需要写一个Comparator<Blah>
,其中Blah
是您ArrayList
中的内容的类名(即,它是ArrayList<Blah>
)。
它看起来像这样:
public class NameComparator implements Comparator<Blah> {
@Override
public int compare(Blah x, Blah y) {
return x.getName().compareTo(y.getName());
}
}
请注意,这假定name
属性永远不会null
,在您的情况下,这可能是也可能不是。完成此操作后,您可以使用Collections.sort()
对ArrayList
进行排序。
然后,要打印出来,你需要迭代元素:
for (Blah b: myArrayListBlah)
System.out.println(b.getName());
或类似的,取决于您希望它如何出来。
答案 5 :(得分:0)
在模型类中,您必须更改类的签名并实现上述方法:
class YourClass implements Comparator<YourClass>, Comparable<YourClass>{
// Overriding the compareTo method
public int compareTo(Dog d){
return (this.name).compareTo(d.name);
}
// Overriding the compare method to sort the age
public int compare(YourClass d, YourClass d1){
return d.strength - d1.strength;
}
并且在您想要进行排序时,您必须致电:
// Sorts the array list using comparator
Collections.sort(list, new YourClass());
System.out.println(" ");
for(YourClass a: list)//printing the sorted list of strengths
System.out.print(a.getStrength() +" : "+ a.getStrength() + ", ");
答案 6 :(得分:0)
<强> Test.java 强>
package com.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
public static void main(String[] args) {
MyFile myFile1 = new MyFile("square", "bold", 1);
MyFile myFile2 = new MyFile("rect", "bold", 2);
MyFile myFile3 = new MyFile("circle", "italic", 3);
MyFile myFile4 = new MyFile("pyramid", "bold", 4);
List<MyFile> myFiles = new ArrayList<MyFile>();
myFiles.add(myFile1);
myFiles.add(myFile2);
myFiles.add(myFile3);
myFiles.add(myFile4);
Collections.sort(myFiles, new Comparator<MyFile>() {
@Override
public int compare(MyFile o1, MyFile o2) {
if (o1.getName() == o2.getName()) {
return 0;
}
return o1.getName().compareTo(o2.getName());
}
});
for (int i = 0 ; i < myFiles.size() ; i++) {
System.out.println(myFiles.get(i).getName());
}
}
}
MyFile .java
package com.*;
public class MyFile {
private String name;
private String style;
private int strength;
public MyFile(String name, String style, int strength) {
this.name = name;
this.style = style;
this.strength = strength;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
}
<强>输出强>
circle
pyramid
rect
square