Java集合:检查对象属性

时间:2014-05-23 11:31:11

标签: java optimization collections arraylist

我有一个带对象的Arraylist

我的对象模型

 class MyData{
        public  int property;
        public String section;
    }

我有这样的功能

  boolean preselectionDefined;
  int position=0;
  private String setSection(ArrayList < MyData > myDatas) {
  int accountCount = myDatas.size();

  String sectionId = "";

  if (preselectionDefined) {
            //First i check the "section" property of object .
            //If i found a object "section" equals "HEAD" i will stop iteration
    for (int i = 0; i < accountCount; i++) {
        MyData sectionA = myDatas.get(i);
        if (sectionA.section.equalsIgnoreCase("HEAD")) {
            sectionId = "A";
                            position=i;
            return sectionId;
        }
    }
              //if i didn't find "section" property of object as "HEAD" in any item in list.
            //Again i iterate. If i found a object "section" equals "SENIOR" i will stop iteration
    for (int i = 0; i < accountCount; i++) {
        MyData sectionA = myDatas.get(i);
        if (sectionA.section.equalsIgnoreCase("SENIOR")) {
            sectionId = "B";
                            position=i;
            return sectionId;
        }
    }
              //if i didn't find "section" property of object as "SENIOR" in any item in list.
            //Again i iterate. If i found a object "section" equals "JUNIOR" i will stop iteration
    for (int i = 0; i < accountCount; i++) {
        MyData sectionA = myDatas.get(i);
        if (sectionA.section.equalsIgnoreCase("JUNIOR")) {
            sectionId = "C";
                            position=i;
            return sectionId;
        }
    }
 }
     // if  all the above conditions failed default will happen .
     // Default Section Id if it's selection is not defined
     sectionId = myDatas.get(position).section;
   return sectionId;
}

我认为这段代码可以帮助你理解我在做什么,(我很抱歉,我无法用语言表达我的需求)。有没有更好的方法来实现这个功能,没有这么多的迭代和检查??

编辑: 我没有将id设置为数组列表中的对象。如果对象的属性满足我的需要,我将停止迭代。首先我将迭代列表以找到任何&#34; HEAD&#34; 如果我找到一个我将停止我的迭代,如果没有&# 39;找不到任何&#34; HEAD&#34; ,我将再次通过重复迭代整个列表来检查&#34; SENIOR&#34; 。如果我在列表中找到任何&#34; SENIOR&#34; ,我将在此时停止迭代。如果不是我将再次迭代检查&#34; JUNIOR&#34; 。而且我真的很抱歉,我忘了提一件事,我需要在数组列表中迭代停止的对象的位置。

这是我的情景。我认为我的方法是一件坏事,任何人都有更好的主意

4 个答案:

答案 0 :(得分:1)

我会实现Comparable并且会创建一个静态方法来从Compareable返回值获取SectionId;然后调用Collections.min()而不是循环并调用静态方法来获取最小值id的值的String。然后每个细节都在MyData类中。

Class MyData

class MyData implements Comparable, SectionId {
    public  int property;
    public String section;

    public int compareTo(MyData  o)
        if (o.section.equals("HEAD") {
             return 100;
        } else if (o.section.equalsIgnoreCase("SENIOR")) {
             return 50;
        } else if (o.section.equalsIgnoreCase("JUNIOR")) {
             return 10;
        }
        return 0;
    }

    public static String parseSectionId(MyData o) {
        if (o.section.equals("HEAD")) {
            return "A";
        } else if (o.section.equalsIgnoreCase("SENIOR")) {
            return "B";
        } else if (o.section.equalsIgnoreCase("JUNIOR")) {
            return "C";
        } else {
            return ""; // DEFAULT
        }
    }
}

不需要循环,而是Collection.max() - Method将返回最大的SectionId - 之后你可以将这个maxId解析为适当的String-Literal:

boolean preselectionDefined;

private String setSection(ArrayList < MyData > myDatas) {
    int accountCount = myDatas.size();

    String sectionId = ""; // Set Default here
    if (preselectionDefined) {
        sectionId = MyData.parseSectionId(Collections.max(myDatas));
    }

    return sectionId;
}

答案 1 :(得分:0)

试试这个:

 if (preselectionDefined) {
    for (int i = 0; i < accountCount; i++) {
        MyData sectionA = myDatas.get(i);
        if (sectionA.section.equalsIgnoreCase("HEAD")) {
            sectionId = "A";
            return sectionId;
        } else if (sectionA.section.equalsIgnoreCase("SENIOR")) {
            sectionId = "B";
            return sectionId;
        } else if (sectionA.section.equalsIgnoreCase("JUNIOR")) {
            sectionId = "C";
            return sectionId;
        }
    }
 } 

答案 2 :(得分:0)

更好的方法

if (preselectionDefined) {
    for ( MyData data : myDatas) {
        if (data.section.equalsIgnoreCase("HEAD")) {
            sectionId = "A";
        } else if (data.section.equalsIgnoreCase("SENIOR")) {
            sectionId = "B";
        } else if (data.section.equalsIgnoreCase("JUNIOR")) {
            sectionId = "C";
        }
    }
      return sectionId;
 } 

在Java 7中

if (preselectionDefined) {
    for ( MyData data : myDatas) {
        switch (data) {
            case "HEAD":
                sectionId = "A";
            break;
            case "SENIOR":
                sectionId = "B";
            break;
            case "JUNIOR":
                sectionId = "C";
            break;
        }                   
    }
 return sectionId;
 } 

答案 3 :(得分:0)

我做的是,我修改了我的模型类

class MyData{
        public  int property;
        public String section;
        public int sectionId; //A new property 

    }

为部分

定义了ID
public static final int HEADER_ID= 1;
public static final int SENIOR_ID = 2;
public static final int JUNIOR_ID=3;
public static final int DEFAULT_ID =4;

在创建MyData的对象时,我将设置此sectionId还取决于

部分
boolean preselectionDefined;
  int position=0;
  private String setSection(ArrayList < MyData > myDatas) {
  int accountCount = myDatas.size();

  String sectionId = "";

  if (preselectionDefined) {
     MyData data=Collections.min(myDatas, new Comparator<MyData >() {
    @Override
    public int compare(MyData lhs, MyData rhs) {
    return Integer.valueOf(lhs.sectionId).compareTo(Integer.valueOf(rhs.sectionId));
        }
    });
    position=myDatas.indexOf(data)

 }
     // if  all the above conditions failed default will happen .
     // Default Section Id if it's selection is not defined
     sectionId = myDatas.get(position).section;
   return sectionId;
}

这解决了我的问题。谢谢@ duffy356给了我这个想法