0
*import java.util.*;  
public class UserArrayList {
    public static void main(String[] args) {
        //Creating user defined class objects  
        Student s1=new Student(1,"AAA",13);  
        Student s2=new Student(2,"BBB",14);  
        Student s3=new Student(3,"CCC",15); 
        ArrayList<Student> al=new ArayList<Student>();
        al.add(s1);
        al.add(s2);  
        al.add(s3);  
        Iterator itr=al.iterator();  
        //traverse elements of ArrayList object  
        while(itr.hasNext()){  
            Student st=(Student)itr.next();  
            System.out.println(st.rollno+" "+st.name+" "+st.age);  
        }  
    }
}
class Student{  
    int rollno;  
    String name;  
    int age;  
    Student(int rollno,String name,int age){  
        this.rollno=rollno;  
        this.name=name;  
        this.age=age;  
    }  
} 

How do I retrieve a single attribute of Student class using ArrayList or check whether "AAA" is present in the list?

4
  • 1
    Why would you tag this JavaScript when it's not that Commented Jun 21, 2018 at 12:42
  • 1
    If you are using Java 8 you can use : Student find = al.stream() .filter(s -> s.name.equals("AAA")) .findFirst() .orElse(null); Commented Jun 21, 2018 at 12:44
  • "or check whether "AAA" is present in the list?" This part is easy: in the while-loop you already have, you can use if("AAA".equals(st.name)). I would suggest adding getter and setter methods to your Student-DTO, though. In which case .name becomes .getName(). Commented Jun 21, 2018 at 13:03
  • You did it in your code acutally. Inside System.out.println you get st.name. All you have to do is just do st.name.equals("AAA"); Commented Jun 21, 2018 at 13:03

1 Answer 1

2

Just added two lines and btw. you have error in that code when you create ArrayList. You wrote "ArayList" which is wrong.

Method 1.

public static void main(String[] args) {
            //Creating user defined class objects  
            Student s1=new Student(1,"AAA",13);  
            Student s2=new Student(2,"BBB",14);  
            Student s3=new Student(3,"CCC",15); 
            ArrayList<Student> al=new ArrayList<Student>(); // YOU HAVE MISTAKE HERE ArayList<Student>()
            al.add(s1);
            al.add(s2);  
            al.add(s3);  
            Iterator itr=al.iterator();  
            //traverse elements of ArrayList object  
            while(itr.hasNext()){  
                Student st=(Student)itr.next();  
                System.out.println(st.rollno+" "+st.name+" "+st.age);  
                if(st.name.equals("AAA"))  // LINE ADDED
                    System.out.println("present!"); // LINE ADDED

            }  
        }
    }
    class Student{  
        int rollno;  
        String name;  
        int age;  
        Student(int rollno,String name,int age){  
            this.rollno=rollno;  
            this.name=name;  
            this.age=age;  
        }  

Method 2 -> Java 8 or higher

  Student find = al.stream().filter(s -> s.name.equals("AAA")).findFirst().orElse(null);
        if(find != null)
            System.out.println("AAA is present");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.