user9370081
user9370081

Reputation: 19

Java storing and getting from class

Is it possible to get values from the class based one one value? Something very much like a SQL WHERE statement.

class :

private String aid;
public String text;
public class B{
    public B(String a){
        this.aid = a;
    }
    public B(String a, String c)
    {
        this.aid = a;
        this.cid = c;
    }
    /** getter setter here **/

And from another file, I would like to retrieve the data from this class with the value of aid = "John"

Upvotes: 0

Views: 39

Answers (1)

Lothar
Lothar

Reputation: 5449

There might be libraries out there that provide such a functionality with a query language in place but with the standard feature set of a JVM, Streams should come the closest to what you look for:

Assuming a List containing your POJOs you can do the following to get a new List of entries with the "John" as value for aid:

List<B> entries = getSourceList();
List<B> filteredEntries = entries.stream()
    .filter(elem -> String.valueOf(elem.getAid()).equals("John"))
    .collect(Collectors.toList());

Upvotes: 1

Related Questions