对集合进行排序并将内容分配给 java 中的列表
Sort a set and assign the content to a List in java
我有以下 java 测试代码要实现:
import java.util.*;
public class TestRisorseWeb {
public static void main(String[] args) {
RisorsaWeb p = new PaginaWeb("index.html","<html><body>main</body></html>"); // name and content
java.io.File f = new FileWeb("pippo.txt","Questo e' il contenuto\ndi un file di testo.");
assert f instanceof RisorsaWeb;
Set<RisorsaWeb> set = new HashSet<RisorsaWeb>();
set.add(p);
set.add((RisorsaWeb) f);
set.add(new PaginaWeb("altraPagina.html","<html><body>altro</body></html>") );
List<RisorsaWeb> list = Ordina.perCriterioUtente(set,
(RisorsaWeb a, RisorsaWeb b) -> a.getNome().compareTo(b.getNome()) );
list.get(0).getNome().equals("altraPagina.html");
list.get(1).getNome().equals("index.html");
list.get(2).getNome().equals("pippo.txt");
}
}
我不明白这行:
List<RisorsaWeb> list = Ordina.perCriterioUtente(set,
(RisorsaWeb a, RisorsaWeb b) -> a.getNome().compareTo(b.getNome()) );
我想弄清楚 Ordina 是什么?
一个 class 有一个名为 perCriterioUtente 的函数?
或者一个对象?
这些是我实现的 classes 和接口:
PaginaWeb.java
public class PaginaWeb implements RisorsaWeb{
private String nome, contenuto;
public PaginaWeb(String name, String contained) {
this.nome = name;
this.contenuto = contained;
}
}
RisorsaWeb.java
public interface RisorsaWeb {
}
和FileWeb.java:
import java.io.File;
public class FileWeb extends File implements RisorsaWeb {
private String nome, contenuto;
public FileWeb(String name, String contained){
super(name);
this.contenuto = contained;
}
}
这应该是你要找的:
我认为这将创建一个列表并对其进行排序。
public class Ordina{
public static List<RisorsaWeb> perCriterioUtente(Set<RisorsaWeb> unstorted, Comparator<RisorsaWeb> comparator){
List<RisorsaWeb> list = new ArrayList<RisorsaWeb>();
list.addAll(unsorted);
Collections.sort(list,comparator);
return list;
}
}
注意:仅使用 javadoc 编写,因此未测试。
我有以下 java 测试代码要实现:
import java.util.*;
public class TestRisorseWeb {
public static void main(String[] args) {
RisorsaWeb p = new PaginaWeb("index.html","<html><body>main</body></html>"); // name and content
java.io.File f = new FileWeb("pippo.txt","Questo e' il contenuto\ndi un file di testo.");
assert f instanceof RisorsaWeb;
Set<RisorsaWeb> set = new HashSet<RisorsaWeb>();
set.add(p);
set.add((RisorsaWeb) f);
set.add(new PaginaWeb("altraPagina.html","<html><body>altro</body></html>") );
List<RisorsaWeb> list = Ordina.perCriterioUtente(set,
(RisorsaWeb a, RisorsaWeb b) -> a.getNome().compareTo(b.getNome()) );
list.get(0).getNome().equals("altraPagina.html");
list.get(1).getNome().equals("index.html");
list.get(2).getNome().equals("pippo.txt");
}
}
我不明白这行:
List<RisorsaWeb> list = Ordina.perCriterioUtente(set,
(RisorsaWeb a, RisorsaWeb b) -> a.getNome().compareTo(b.getNome()) );
我想弄清楚 Ordina 是什么?
一个 class 有一个名为 perCriterioUtente 的函数?
或者一个对象?
这些是我实现的 classes 和接口: PaginaWeb.java
public class PaginaWeb implements RisorsaWeb{
private String nome, contenuto;
public PaginaWeb(String name, String contained) {
this.nome = name;
this.contenuto = contained;
}
}
RisorsaWeb.java
public interface RisorsaWeb {
}
和FileWeb.java:
import java.io.File;
public class FileWeb extends File implements RisorsaWeb {
private String nome, contenuto;
public FileWeb(String name, String contained){
super(name);
this.contenuto = contained;
}
}
这应该是你要找的:
我认为这将创建一个列表并对其进行排序。
public class Ordina{
public static List<RisorsaWeb> perCriterioUtente(Set<RisorsaWeb> unstorted, Comparator<RisorsaWeb> comparator){
List<RisorsaWeb> list = new ArrayList<RisorsaWeb>();
list.addAll(unsorted);
Collections.sort(list,comparator);
return list;
}
}
注意:仅使用 javadoc 编写,因此未测试。