本文介绍了如何在一个列表搜索,我的表是这样的:清单<对象> myList中=新的ArrayList<对象>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个列表来搜索和我的名单是像

I want to search in a List and my List is look like

List<Employee> oneEmp= new ArrayList<Employee>();
List<Employee> twoEmp= new ArrayList<Employee>();



oneEmp= [Employee [eid=1001, eName=Sam Smith, eAddress=Bangluru, eSalary=10000000], Employee [eid=0, eName=, eAddress=, eSalary=null], Employee [eid=1003, eName=Amt Lime, eAddress=G Bhagyoday, eSalary=200000], Employee [eid=1004, eName=Ash Wake, eAddress=BMC, eSalary=200000], Employee [eid=1005, eName=Will Smith, eAddress= Delhi, eSalary=200000], Employee [eid=1006, eName=Shya Ymwar, eAddress=Madras, eSalary=50000], Employee [eid=1007, eName=Nag Gam, eAddress=Pune, eSalary=10000000], Employee [eid=1008, eName=Arti, eAddress=Delhi, eSalary=10000000]]

twoEmp= [Employee [eid=0, eName=null, eAddress=null, eSalary=100000], Employee [eid=0, eName=null, eAddress=null, eSalary=50000], Employee [eid=0, eName=null, eAddress=null, eSalary=200000]]

我使用code是这样的: -

I am using code like this:-

for(Employee two : twoEmp){
        for (Iterator<Employee> iterator = oneEmp.iterator(); iterator.hasNext(); ) {
        Employee e = iterator.next();
        if (e.geteSalary() != null && two.geteSalary() != null && e.geteSalary().compareTo(two.geteSalary()) == 0) {
            finalEmpList.add(e);
        }
        }
    }

但是,这仍然需要2个循环

But this still required 2 for loop

我使用JAVA 1.6

我的员工类有属性:

//Employee class
int eid;
BigInteger eSalary;
String eName, eAddress;

现在我想在列表谁的工资=千万

Now I want to get all the objects in List who's Salary = 10000000

结果应该是:

[雇员[EID = 1001,易名=萨姆·史密斯,eAddress = Bangluru,eSalary =千万],雇员[EID = 1007,易名=纳格邯,eAddress =浦那,eSalary =千万],雇员[EID = 1008,易名= ARTI,eAddress =德里,eSalary =千万] .........................]

[Employee [eid=1001, eName=Sam Smith, eAddress=Bangluru, eSalary=10000000], Employee [eid=1007, eName=Nag Gam, eAddress=Pune, eSalary=10000000], Employee [eid=1008, eName=Arti, eAddress=Del eSalary=10000000],.........................]

我想实现这个不使用任何循环或因为所需的最小环数据将是大

I would like to achieve this without using any loop or minimum loop required because data will be large

推荐答案

是的,这是可能避免的循环中使用的。

Yes, it is possible to avoid the loop using streams.

首先,考虑使用泛型集合:

First, consider using a generic collection:

List<Employee> employees = new ArrayList<>():
//add all employees to the list

现在,您可以使用过滤列表

Now you can use streams to filter your list

List<Employee> filtered = employees.stream()    
            .filter(emp -> emp.getSalary() == 10000000)
            .collect(Collectors.toList());

编辑:大概流库仍在使用某种循环的内部,但同时其实现向我隐瞒我不担心

Probably Stream library is still using some kind of loop internally but while its implementation is hidden from me I do not worry.

这篇关于如何在一个列表搜索,我的表是这样的:清单&LT;对象&gt; myList中=新的ArrayList&LT;对象&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:20