集合中的 Java 过滤

标签 java collections

我有如下对象列表,

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/

相关文章:

c++ - 存储 double 和整数混合集合的最有效方法

java - 仅获取 ArrayList 中符合条件的第一个元素

java - 我需要一个简单的命令行程序来使用 XSL 样式表转换 XML

java - 无法访问 Spring Boot 和 Jersey 应用程序中的某些 Controller

Java Swing GUI 在鼠标悬停时改变颜色

java - 从 Java 调用 Informatica PC wf 并读取响应以恢复 Java 程序

hibernate - HQL中如何检查一个集合的所有元素是否存在于另一个集合中

java - 我需要哪种 Java 对象类型(集合/列表/集合/其他)?

c# - 如何访问继承Collection<>的类中的集合?

java - Google ProtoBuf 序列化/反序列化