如果列表包含某个值,如何将列表拆分到另一个列表?

时间:2016-12-14 20:10:52

标签: java

我在控制器中有一个从另一个类获得的值列表。该列表包含“状态”的值。状态具有存储在其中的不同值,例如,Ok,Bad,CRITICAL。我试图提取每个的值。 IF列表值“status”包含OK,将其放入OKlist,与其他列表相同。下面是我的代码片段和我做了什么。

类摘要

public class Album implements Runnable {        
    //doing many stuff then 
    memoryUtilization = 100 - memoryUtilization; 
        if (memoryUtilization <= badThreshold
                    && memoryUtilization > criticalThreshold) {
        Status = "BAD";

            }else if (memoryUtilization <= criticalThreshold) {
                Status = "CRITICAL";

            }
            else {
                Status = "OK";

            }

            data.add(new AlbumModel (Status));

控制器代码段

    @Controller
public class IndexController {
    @Autowired
    private AlbumList albumL;

    public ModelAndView getdata() throws IOException {

List<AlbumModel> list = albumL.getList();
System.out.println(list);

输出

[Status: OK,Status: OK,Status: OK,Status: OK,Status: OK,Status: OK,Status: OK,Status: OK,Status: OK,Status: OK,Status: BAD,Status: BAD,Status: BAD,Status: BAD,Status: BAD,Status: BAD,Status: BAD,Status: BAD,Status: BAD,Status: CRITICAL,Status: CRITICAL,Status: CRITICAL,Status: CRITICAL,Status: CRITICAL,Status: CRITICAL]

尝试

在我确定天气状态为坏,暴击或确定的类中,我为每个状态创建了List值,然后我将值添加到IF语句的列表中,它确定状态值。

public class Album implements Runnable {  
static List<AlbumModel> data = new ArrayList<AlbumModel>();
static List<AlbumModel> criticalList = new ArrayList<AlbumModel>(); 
static List<AlbumModel> badList = new ArrayList<AlbumModel>(); 
static List<AlbumModel> okList = new ArrayList<AlbumModel>(); 
 //getters and setters      
    /**************doing many stuff ****************/
    memoryUtilization = 100 - memoryUtilization; 
        if (memoryUtilization <= badThreshold
                    && memoryUtilization > criticalThreshold) {

        Status = "BAD";
                badList.add(new AlbumModel (Status));
            }else if (memoryUtilization <= criticalThreshold) {

                Status = "CRITICAL";
                criticalList.add(new AlbumModel (Status));
            }
            else {
                Status = "OK";
                okList.add(new AlbumModel (Status));
            }

            data.add(new AlbumModel (Status));

然后在我的控制器类中,我只调用了类来检索值。这是解决我问题的正确方法吗?

索引控制器

@Controller
public class IndexController {
    @Autowired
    private AlbumList albumL;    
    public ModelAndView getdata() throws IOException {
List<AlbumModel> list = albumL.getList();      
List<AlbumModel> albumCritList = Album.getCritList();
List<AlbumModel> albumBadList = Album.getBadList();
List<AlbumModel> albumOkList = Album.getOkList();     

1 个答案:

答案 0 :(得分:0)

使用Java 8,类似这样:

final List<AlbumModel> critcalList =
        originalList.filter(albumModel -> "CRITICAL".equals(albumModel.getFoo()))
                    .findAny()
                    .orElse(new LinkedList<AlbumModel>());

//冲洗并重复其他清单