简体   繁体   中英

How can I retrieve all Objects in a List that have certain attributes?

For example, I've created a list of "Incidents" from a service portal:

List<Incident> incidentObjectList = new ArrayList<>();
for (int i = 0; i < incidentNumberList.size(); i++) {
    Incident incident = new Incident();
    incident.setIncidentNumber(incidentNumberList.get(i));
    incident.setSummary(summaryList.get(i));
    incident.setRequestId(requestIdList.get(i));
    incident.setPriority(priorityList.get(i));
    incident.setLastModifiedDate(lastModifiedDateList.get(i));
    incidentObjectList.add(incident);
}
System.out.println("Incident Object List" + incidentObjectList);

Each Incident has several attributes, but I want only to create a new list from that original List composed of Incident Objects with Priority Low . In this case, incident.getPriority() will return the String Low for all Objects in the new List.

String[] dataGreenArray = incidentObjectList.get();
retrieveTickets(dataGreenArray, listViewGreen);

In Java 8

List<Incident> list = incidentObjectList.stream()
                                        .filter(e->e.getPriorty().equals("Low"))
                                        .collect(Collectors.toList());

Loop over the incidentObjectList and check the priority of each element.

for (Incident incident: incidentObjectList)
{
    if (incident.getPriority().equals("Low")
        desiredList.add(incident);         // or whatever you want to do
}

This is a very basic solution, but unless you are specifically looking for something else, it should work. Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM