5

I have a List<Student> and String firstName="George". I want to extract Student object from List matching with "George" without iterating list. Is it possible?

May be if some method override required or anything to make it work.

public Class Student {
     private String firstName;
     private String lastName;
     private String class;

     // getter, setter methods goes here....
}

Please help me thanks.

changeme
  • 640
  • 2
  • 12
  • 36
  • 3
    Use a `Map`. Anything else is a form of iteration in one manner or another – MadProgrammer Aug 13 '15 at 04:05
  • 1
    have you tried any thing? – Piyush Mittal Aug 13 '15 at 04:13
  • 1
    possible duplicate of [Java List.contains(Object with field value equal to x)](http://stackoverflow.com/questions/18852059/java-list-containsobject-with-field-value-equal-to-x) – Abhishek Aug 13 '15 at 04:20
  • @madprogrammer is right. You need a [Map](http://docs.oracle.com/javase/7/docs/api/java/util/Map.html). – glen3b Aug 13 '15 at 04:33
  • Not possible with `List` – Ankit Sharma Aug 13 '15 at 04:54
  • If you want to avoid iterating and want to use a list you could implement a SortedList by extending AbstractList. Then update Student to implement comparable in such a way that George will be the first entry in a sorted collection. Or just use a Map – beresfordt Aug 13 '15 at 05:44
  • Thank you all for your responses. The LIST I got from SOAP request which I don't want to iterate. If I have to build a map means I am iterating list rather I will do my lookup of required Student. – changeme Aug 13 '15 at 12:21

4 Answers4

1

If you are using Java 8 then its possible using Stream and Lambda:-

List<Student> myStudent = studentsList.stream()
                                      .filter(s -> s.name.equals("George"))
                                      .collect(Collectors.toList());
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Vineet Tyagi
  • 300
  • 1
  • 15
0

No you have to iterate.. That's how list is. if you want retrieval to happen in O(1) then you need a map.

Ankit
  • 55
  • 7
0

You can Override the equal() method Then create a Student object with firstname="George" When you do it, you can use list.indexOf(Student_object) method to find out the position of that object and you can use list.get(index) to get Student object in the list

Trung Pham
  • 28
  • 5
  • 1
    But you can't use `list.indexOf("George")` and internally, it's still a form of iteration – MadProgrammer Aug 13 '15 at 04:38
  • I mean creating new object Student and setFirstname is "George". And with this, at least we don't have to use loop – Trung Pham Aug 13 '15 at 05:00
  • I know what you mean, but I'm reasonable certain it's not what the OP is looking for an internally, it's still using an iteration of some kind. A `Map` would be the only solution which didn't require some form of iteration at one level of another, just saying ;) – MadProgrammer Aug 13 '15 at 05:04
  • This is how I have implemented, I thought without creating a new Student().setFirstName("George") I can achieve just by .contains("George"). Thank you for all responses. – changeme Aug 13 '15 at 12:16
0

You should have used hash map instead of List ,and add student name as a key with object. HashMap=new HashMap;

RatheeshTS
  • 411
  • 3
  • 15