查找记录,该记录应为每一行的一个参数至少定义一个值

时间:2019-01-20 17:03:35

标签: java algorithm data-structures collections logic

我在列表->

以下

列表地址;

员工

名称, 年龄 公司 工资

Name                    Age             Company     Salary    
A                       21              comp_A       100        
A                       21              comp_A              
A                       21              comp_A          
B                       22              comp_B              
B                       22              comp_B       200        
C                       23              comp_C       300        
D                       24              comp_D          
E                       25              comp_E         

我希望我的姓名,年龄,公司名称至少与定义的工资相同。 由于该重复记录代表同一位员工,因此他们必须有薪水。

赞赏的逻辑将获取满足此条件但又不满足条件的记录,例如D / E员工的最后两个拖行没有为Salary和 我也应该获得这些价值。

1 个答案:

答案 0 :(得分:1)

将列表转换为流,删除重复项并收集回新列表。 您有2个选项,其中distinct(在所有字段中删除重复项)和distinctByKey (根据特定条件或仅某些字段删除重复项)

https://howtodoinjava.com/java8/java-stream-distinct-examples/

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FindR {

public static void main(String[] args)
{
    ArrayList<Employee> l = new ArrayList();
    l.add(new FindR().new Employee("A",21,"comp_A",100.0));
    l.add(new FindR().new Employee("A",21,"comp_A",100.0));
    l.add(new FindR().new Employee("A",21,"comp_A",null));
    l.add(new FindR().new Employee("A",21,"comp_A",null));
    l.add(new FindR().new Employee("B",22,"comp_B",null));
    l.add(new FindR().new Employee("B",22,"comp_B",200.0));
    l.add(new FindR().new Employee("C",23,"comp_C",300.0));
    l.add(new FindR().new Employee("D",24,"comp_D",null));
    l.add(new FindR().new Employee("E",25,"comp_E",null));

    List<Employee> l_salaryNotNull = l.stream().filter(p->p.salary !=null).collect(Collectors.toList());
    System.out.println("_salalary defined_");
    l_salaryNotNull.stream().forEach(System.out::println);

    System.out.println("_unique by name,age,company_");
    List<String> l_uByNAC = l.stream().map(p->p.name+":"+p.age+":"+p.company).distinct().collect(Collectors.toList());

    //or remove first null values and then get keys
    //List<String> l_uByNAC = l.stream().filter(p->p.salary !=null).map(p->p.name+":"+p.age+":"+p.company).distinct().collect(Collectors.toList());

    l_uByNAC.stream().forEach(System.out::println);
    //eg: process first record 
    String aux[] = l_uByNAC.get(0).split(":");
    System.out.println("keys for search in initial list | name="+aux[0]+",age="+aux[1]+",company="+aux[2]);
}


public class Employee 
{
    public String name;
    public Integer age;
    public String company;
    public Double salary;
    public Employee(String n, Integer a,String c, Double s)
    {
        name = n;
        age = a;
        company = c;
        salary = s;
    }

    public String toString()
    {
        return "name:"+name+":age:"+age+":company:"+company+":salary:"+salary;
    }
}
}
//output
//_salalary defined_
//name:A:age:21:company:comp_A:salary:100.0
//name:A:age:21:company:comp_A:salary:100.0
//name:B:age:22:company:comp_B:salary:200.0
//name:C:age:23:company:comp_C:salary:300.0
//_unique by name,age,company_
//A:21:comp_A
//B:22:comp_B
//C:23:comp_C
//D:24:comp_D
//E:25:comp_E
//(first)key for search in initial list | name=A,age=21,company=comp_A