How to search in an List and my List is look like

2019-07-26 17:13发布

This question already has an answer here:

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]]

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);
        }
        }
    }

But this still required 2 for loop

I am using JAVA 1.6

My Employee class has attributes:

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

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

result should be :

[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=Delhi, eSalary=10000000],.........................]

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

3条回答
Melony?
2楼-- · 2019-07-26 17:21

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());

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

查看更多
该账号已被封号
3楼-- · 2019-07-26 17:21

Something like this should do the trick; iterate over the List, and remove the items which you don't want, leaving only the ones which you do want.

List myList = new ArrayList(); //... add items

[...]

for (Iterator<Employee> iterator = myList.iterator(); iterator.hasNext(); ) {
    Employee e = iterator.next();
    if (e.getSalary() != 10000000) {
        iterator.remove();
    }
}

//your list now contains only employees whose salary = 10000000

Edit: And no, you cannot do this without a loop. In order to do this kind of thing, you have to iterate over your Collection using a loop. Even if you use a library or the Java Streams API to do this, it will still use a loop of some sort under the hood. However, this will be quite efficient, even with as large dataset. (How large ? Why do you want to avoid using a loop ?)

查看更多
闹够了就滚
4楼-- · 2019-07-26 17:42

A List is a sequential container, to do any kind of filtering on a list, your only option is to iterate over it.

For the query you mentioned,you can use the Map data structure with a BigInteger type for the key (representing the salary) and a List<Employee> for the mapped value type. This will enable you to look for all the employees that earn a certain salary in constant time without having to iterate over the whole list.

Unfortunately though, this solution can't help you do any other queries like "how many employees earn more than 60000", to preform all types of queries on a large data set you should use a database.

PS: You don't need to use the BigInteger type for the salary, unless you think someone earns more than 2,147,483,647

查看更多
登录 后发表回答