How to get Count of Users Starts with “A” where user have two fields Id and name
Solution:-
User.java
package v1;
public class User {
int id;
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
*************************************************************************
Test.java
package v1;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<User> list=new ArrayList<>();
list.add(new User(1, "Bharat"));
list.add(new User(2, "Aditya"));
list.add(new User(3, "Mohan"));
list.add(new User(4, "Apurva"));
list.add(new User(5, "Aditi"));
list.add(new User(6, "Neha"));
Long countOfUserA=list.stream().filter(i->i.getName().startsWith("A")).count();
System.out.println("CountOfUserA is: "+countOfUserA);
}
}
*************************************************************************
Leave a Reply