Hafeez PK
Hafeez PK

Reputation: 27

Which collection best suits this

I have 100's of employee objects. Employee object has name, age, salary. I should retrieve an employee or employees with specific name at some point.

I'm just thinking

HashMap<String, List>

name in to the key, all objects in to the list.

I know map is not a collection :P

Any idea of a best collection instead of a map. to retrieve at any time.

Upvotes: 0

Views: 205

Answers (6)

Tapas Bose
Tapas Bose

Reputation: 29806

If you have a List<Employee> and you want to filter the list by the name or age etc I guess lambdaj is good for this purpose.

Here is an example, say you want to filter all employee by their name:

HasArgumentWithValue<Employee, String> matcher = Lambda.having(Lambda.on(Employee.class).getName(), Matchers.equalTo(name));
List<Employee> filteredEmployess = Lambda.filter(matcher, allEmployess);

By static import the above code would be:

List<Employee> filteredEmployess = filter(having(on(Employee.class).getName(), equalTo(name)), employees);

You can find more example here

For age it would be usefull to find employees having age greater than 30:

HasArgumentWithValue<Employee, Integer> matcher = Lambda.having(Lambda.on(Employee.class).getAge(), Matchers.greaterThan(30)) 

Upvotes: 1

Lurvas777
Lurvas777

Reputation: 35

If you want to use the name attribute in the Employee class as the key make sure you don't have employees with the same name. The map uses a key, in your case a String, and each key has to be unique. If they are you're fine with HashMap<String name, Employee>.

(create the Employee class with all attributes)

However if they're not unique, many employee can have the same name, you should consider adding a attribute to the Employee class. Something like

private int ID;

and use it as key like this:

HashMap<int, Employee>

HashMap is very good in time complexity. In average a get() call would get the time T(n) = O(1) (i.e. in constant time). Same goes for a put call.

Upvotes: 0

Kent
Kent

Reputation: 195029

I should retrieve an employee or employees with specific name at some point.

from the line above, I thought you have multiple employees with same name. If this is the case, I suggest guava's Multimap. e.g. HashMultimap

Upvotes: 0

Vincent van der Weele
Vincent van der Weele

Reputation: 13177

I don't know why HashMap doesn't suit your needs, but if you want to be able to iterate all employees (as if it was a collection), you can still use:

Map<String, List<Employee>> map = new HashMap<String, List<Employee>>();

// populate your map

for (List<Employee> list : map.values()) {
  for (Employee employee : list) {
    // do something
  }
}

Upvotes: 0

BobTheBuilder
BobTheBuilder

Reputation: 19284

You should first create Employee class with all relevant fields (name, age, salary etc) Then you can use HashMap:

 Map<String, Employee> map = new HashMap<String, Employee>();

Upvotes: 2

rai.skumar
rai.skumar

Reputation: 10667

Though Map doesn't extend Collection class; but still Map is a type of Collection. So if your purpose is served by HashMap; then will suggest go for it.

 HashMap<String, Employee> map = new HashMap<String, Employee>();

Upvotes: 0

Related Questions