Reputation: 8081
I have a Arraylist with objects
my Object Model
class MyData{
public int property;
public String section;
}
And i have a function like this
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;
}
I think this code helps you to understand what i am doing ,( I am very sorry i am not able express my needs in words). Is there any better way to achieve this functionality without these much iterations and checking ??
EDIT : I am not setting the id to my objects in array list . If the property of object satisfy my need i will stop the Iteration .First i will iterate list to find any "HEAD" if i found one i will stop my iteration ,if didn't find any "HEAD" , i will check for "SENIOR" by a repeat iteration of entire list for once again. If i find any "SENIOR" in list i will stop iteration at that point .if not i will again Iterate to check "JUNIOR" . and I'm really sorry one thing i forget to mention and i need the position of object in array-list where iteration stops .
This is my scenario . I think my approach is a bad thing does any one have a better idea
Upvotes: 0
Views: 97
Reputation: 8081
What i did was , I modify my model class
class MyData{
public int property;
public String section;
public int sectionId; //A new property
}
And defined IDs for section
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;
And while creating object of MyData i will set this sectionId also depends on section
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;
}
this solved my issue .Thanks @duffy356 for giving me the idea
Upvotes: 0
Reputation: 3718
I would implement Comparable and would make a static Method to get the SectionId from the value Compareable returns; then call Collections.min() instead of the loop and call the static method to get the String for the value of the minimun id. Then every detail would be in the MyData class.
The 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
}
}
}
The loop would be not needed, instead the Collection.max() - Method would return the maximum SectionId - afterwards you can parse this maxId to the appropriate 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;
}
Upvotes: 1
Reputation: 119
Even better approach
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;
}
In 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;
}
Upvotes: 0
Reputation: 69440
try this:
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;
}
}
}
Upvotes: 0