我有如下对象列表,
Emp e1 = new Emp(10,"Anitha",1000,"AE");
Emp e2 = new Emp(20,"Chaitanya",2000,"SE");
Emp e3 = new Emp(30,"Chaitanya",3000,"SE");
Emp e4 = new Emp(40,"Deepthi",2100,"AE");
Emp e5 = new Emp(50,"Deepthi",2200,"CE");
Emp e6 = new Emp(60,"Deepthi",2300,"BE");
Emp e7 = new Emp(70,"Anitha",2300,"BE");
Emp e8 = new Emp(80,"Anitha",2400,"ME");
Emp e9 = new Emp(90,"Sita",2200,"CE");
Emp e10 = new Emp(100,"Hari",2200,"CE");
Emp e11 = new Emp(110,"Krishna",2200,"CE");
我想过滤唯一名称的值,也过滤相同的名称
1.on unique name : output should be
(50,"Deepthi",2200,"CE")
(100,"Hari",2200,"CE")
(110,"Krishna",2200,"CE")
并共享相同的名称:
喜欢输出
(10,"Anitha",1000,"AE")
(70,"Anitha",2300,"BE")
(80,"Anitha",2400,"ME")
(20,"Chaitanya",2000,"SE");
(30,"Chaitanya",3000,"SE");
(40,"Deepthi",2100,"AE");
(50,"Deepthi",2200,"CE");
(60,"Deepthi",2300,"BE");
使用集合.... 有人可以帮助我吗?
提前致谢。 尼西亚
最佳答案
如果您使用的是 java 8,请跳到最后!
我可能会创建一个 map来执行此操作,但看起来您是 Java 的新手,所以我将描述更基本的方法。
你应该首先像这样创建一个列表(arraylist):
// create an arraylist (list based on an array)
List<Emp> emps = new ArrayList<Emp>();
然后您可以将对象添加到列表中:
emps.add(new Emp(10,"Anitha",1000,"AE"));
emps.add(new Emp(20,"Chaitanya",2000,"SE"));
.
.
现在您可以开始过滤了!
因此,假设您在 Emp
类中有一个 getName()
方法,您可以编写如下函数:
// this function takes a list and a name, and filters the list by the name
public static List<Emp> filterEmpsByName(List<Emp> emps, String name){
// a place to store the result
List<Emp> result = new ArrayList<Emp>();
// iterate over the list we got
for (Emp emp: emps){
// save only the elements we want
if (emp.getName().equals(name)){
result.add(emp);
}
}
return result;
}
现在,过滤将是调用该函数的简单问题:
// print to standard output the result of our function on the "main" list `emp` with name "Anitha"
for (Emp emp : filterEmpsByName(emps, "Anitha")){
System.out.println(emp.toString()); // make sure toString() is overridden in Emp class
}
第二部分有点棘手:
// this function takes a list and a name, and filters the list by the name
public static List<Emp> getDistinctlyNamedEmps(List<Emp> emps, String name) {
// this time we use a map which is A LOT faster for this kind of operation
Map<String, Emp> result = new HashMap<String, Emp>();
// iterate over the list we got
for (Emp emp : emps) {
// save only the elements we want
if (result.get(emp.getName()) == null ) {
result.put(emp.getName(), emp);
}
}
// convert map to list - not mandatory if you can use the map as is...
return new ArrayList<Emp>(result.values());
}
注意你也可以写一个comparator使用名称/任何其他属性比较对象,但这超出了本评论的范围:-)
将整个事情放在一起:
主类:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// create an [arraylist][4] (list based on an array)
List<Emp> emps = new ArrayList<Emp>();
emps.add(new Emp(10, "Anitha", 1000, "AE"));
emps.add(new Emp(20, "Chaitanya", 2000, "SE"));
// print to standard output the result of our function on the "main"
// list `emp` with name "Anitha"
System.out.println("filterEmpsByName(emps, \"Anitha\") output:");
for (Emp emp : filterEmpsByName(emps, "Anitha")) {
System.out.println(emp.toString()); // make sure toString() is
// overridden in Emp class
}
// print to standard output the result of our second function on the "main"
// list `emp`
System.out.println("getDistinctlyNamedEmps(emps) output:");
for (Emp emp : getDistinctlyNamedEmps(emps)) {
System.out.println(emp.toString()); // make sure toString() is
// overridden in Emp class
}
}
// this function takes a list and a name, and filters the list by the name
public static List<Emp> filterEmpsByName(List<Emp> emps, String name) {
// a place to store the result
List<Emp> result = new ArrayList<Emp>();
// iterate over the list we got
for (Emp emp : emps) {
// save only the elements we want
if (emp.getName().equals(name)) {
result.add(emp);
}
}
return result;
}
// this function takes a list and a name, and filters the list by the name
public static List<Emp> getDistinctlyNamedEmps(List<Emp> emps) {
// this time we use a map which is A LOT faster for this kind of
// operation
Map<String, Emp> result = new HashMap<String, Emp>();
// iterate over the list we got
for (Emp emp : emps) {
// save only the elements we want
if (result.get(emp.getName()) == null) {
result.put(emp.getName(), emp);
}
}
// convert map to list - not necessary
return new ArrayList<Emp>(result.values());
}
}
和部分 Emp
类:
public class Emp {
private String name;
public Emp(int stubi, String name, int j, String stubs) {
this.name = name;
}
public String getName() {
return this.name;
}
public String toString() {
return "[" + this.name + "]";
}
}
Java 8:
Java 8 有 lambda expressions (匿名函数),这是许多其他语言中用于过滤和其他操作的简洁工具。
您可以阅读更多关于使用它们的信息 here .
关于集合中的 Java 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21560566/