-2

I have a list of objects which i want to iterate and for all objects that have particular value add them to a new list. Can i do that or i need a Map? example code:

List<Object> object = new ArrayList();
Iterator iterator = object.iterator();
while(iterator.hasNext()){
    Object listobject = (Object)iterator.next();
    if(object.getId() == 1){
       newlist.add(object);

The above code adds only the first object with this id. How can add all objects with this id in new list? thanx for your advice.

user2699406
  • 85
  • 3
  • 8
  • Are you sure you have more than one object in your input list whose Id is 1 ? Can we have a sample input and output ? – Agemen Apr 29 '14 at 15:49
  • I think you made an error in your code sample, because object is a List in if(object.getId() == 1){ newlist.add(object); – BaptisteL Apr 29 '14 at 15:52
  • Thanx for quick reply. Yes i have 10 objects with the same id and returns only the first – user2699406 Apr 29 '14 at 16:02

2 Answers2

1

Are you looking for something like this? However please note its better to use for-each loop instead of iterator

public class ObjectCollector {

    static class LstObj {
        int id;
        String name;

        public int getId() {
            return id;
        }

        public LstObj(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public String toString() {
            return "LstObj [id=" + id + ", name=" + name + "]";
        }
    }


    public static void main(String[] args) {
        List newlist = new ArrayList();
        List objects = new ArrayList();

        //Populating random objects
        objects.add(new LstObj(1, "Name 1"));
        objects.add(new LstObj(2, "Name 2"));
        objects.add(new LstObj(1, "Name 1"));
        objects.add(new LstObj(1, "Name 1"));
        objects.add(new LstObj(3, "Name 3"));
        objects.add(new LstObj(4, "Name 4"));

        Iterator iterator = objects.iterator();
        while(iterator.hasNext()){
            LstObj listobject = (LstObj)iterator.next();
            if(listobject.getId() == 1){
               newlist.add(listobject);
            }
        }
        System.out.println(newlist);
    }

}

For-each method

import java.util.ArrayList;
import java.util.List;

public class ObjectCollector {

    static class LstObj {
        int id;
        String name;

        public int getId() {
            return id;
        }

        public LstObj(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public String toString() {
            return "LstObj [id=" + id + ", name=" + name + "]";
        }
    }

    public static void main(String[] args) {
        List<LstObj> newlist = new ArrayList<LstObj>();
        List<LstObj> objects = new ArrayList<LstObj>();

        // Populating random objects
        objects.add(new LstObj(1, "Name 1"));
        objects.add(new LstObj(2, "Name 2"));
        objects.add(new LstObj(1, "Name 1"));
        objects.add(new LstObj(1, "Name 1"));
        objects.add(new LstObj(3, "Name 3"));
        objects.add(new LstObj(4, "Name 4"));

        for (LstObj obj : objects) {
            if (obj.getId() == 1) {
                newlist.add(obj);
            }
        }

        System.out.println(newlist);
    }

}
Syam S
  • 8,421
  • 1
  • 26
  • 36
  • you wrote exactly the code of question which returns the first elements only. Anyway thanz for trying. – user2699406 Apr 29 '14 at 17:56
  • Sorry, but what do you mean by first elements only? I am getting the list of all objects having id as 1. – Syam S Apr 29 '14 at 18:09
  • Hi and thank you again! The above code returns only the first object having this id. I think that when finds the first object having id=1 it stops the iteration. any suggestion? – user2699406 Apr 30 '14 at 16:19
  • I don't think it'll stop iteration unless you put a break statement in it. I tested this and it does return multiple value. Still as an alternative you can use for-each loop.. Answer updated. – Syam S Apr 30 '14 at 16:55
  • If this also didn't work could you please share your full code? – Syam S Apr 30 '14 at 16:59
  • Hi! I figured it out how to do it with for-each loop. Thank you for your time and your valuable help. – user2699406 May 02 '14 at 11:29
-1

You can use the guava library. It allows you to filter a list using the predicate. And so you get only the elements of the list that ID as the one you want.

Exemples :

Community
  • 1
  • 1
helTech
  • 95
  • 1
  • 1
  • 7