1

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

Skyfall
  • 57
  • 8

3 Answers3

1

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.

marcellorvalle
  • 1,631
  • 3
  • 17
  • 30
  • Thanks your response marcellorvalle. but I am using JAVA 1.6 – Skyfall Apr 07 '16 at 13:48
  • I see. I think @JonasCz answer is th way to go. I think large number of employees is not a problem. Just for the sake of curiosity: is there any way to filter the data before populating the collection? – marcellorvalle Apr 07 '16 at 14:00
0

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 ?)

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • data amount may be 100000 – Skyfall Apr 07 '16 at 13:50
  • @Skyfall, That is nothing - a program can do that no problem is a few seconds, provided the data is not very complicated (and yours isn't). – Jonas Czech Apr 07 '16 at 13:51
  • @marcellorvalle, thanks for the edit ! I don't know how I missed that – Jonas Czech Apr 07 '16 at 14:08
  • Thanks @JonasCz I am using your code like this:- – Skyfall Apr 11 '16 at 10:17
  • @Skyfall, If my answer helped you, you could mark it as the accepted answer by clicking the checkmark next to it. Glad it helped :-) – Jonas Czech Apr 11 '16 at 10:23
  • Thanks @JonasCz. empOne = [Employee [eid=1001, eName=Sam Smith, eAddress=Bangluru, eSalary=100000], 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=100000], Employee [eid=1007, eName=Nag Gam, eAddress=Pune, eSalary=50000], Employee [eid=1008, eName=Arti, eAddress=Delhi, eSalary=50000]] – Skyfall Apr 11 '16 at 10:53
  • empTwo = [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 your code like this:- for(Employee two : twoEmp){ for (Iterator 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 – Skyfall Apr 11 '16 at 10:53
  • @Skyfall, Could you edit your question and add your code there ? Honestly, code in comments is not really readable. "But this still required 2 for loop", what do you mean with this ? – Jonas Czech Apr 11 '16 at 10:59
0

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