Comparing Hashmap to download data in excel - java

I have these 4 hashmaps and use them in code so I can show the comparison in an excel sheet.
HashMap 1 - is with a key of unique id and value as another hashmap containing tagid as key and description of fields to compare.
[343, ((id_1,Plan Features),(a, Deductible),(b,Individual),(c,Family),(id_4,Individual Out-of-network),(id_2, Out-of-pocket Annual Maximum),(d,Individual),(e,Family),(u, Life Time Maximum))]
HashMap 2 - is with a key of unique id same as Hashmap 1 and value as another hashmap containing tagid as key and value of description used in Hashmap 1.
[343, ((id_1,""),(a, Calendar Year),(b,5000),(c,10000)(id_4,15000),(id_2,""),(d,5000),(e,10000),(u,"Unlimited"))]
Same is the case with HashMap 3 and HashMap 4
[347, ((id_1,Plan Features),(a, Deductible),(b,Individual),(id_5, Individual Out-of-network),(c,Family),(id_4,Family Out-of-network),(id_2, Out-of-pocket Annual Maximum),(d,Individual),(e,Family),(u, Life Time Maximum))]
[347, ((id_1,""),(a, Calendar Year),(b,7000),(id_5, 9000),(c,12000),(id_4,14000),(id_2, ""),(d,6000),(e,15000),(u, "Unlimited"))]
I want to show the comparison in an excel sheet by showing all descriptions in one column and respective values in another 2 columns.

I'd first suggest you to normalize your suboptimal data representation to something like below. Then you just need to maintain TWO Maps. Then is is easy to iterate between them and display whichever way you want.
If you can use google-guava library then it is even easier to group then by id using Multiset.
Below are highlevel details on my approach. You can use the return type of "reportBuilder.build(plan1, plan2)" and use Apache POI as suggested by others to create the excel
A Carrier offers 1 or more Plan's
Each Plan has and id and 1 or more Features
Each Feature has id, decsription, value
public class Main
{
private static Map> plan1Map;
private static Map> plan1AdditionalDetailsMap;
private static Map> plan2Map;
private static Map> plan2AdditionalDetailsMap;
private static Plan plan1;
private static Plan plan2;
public static void main(final String[] args)
{
initiaizeData();
normalizeData();
System.out.println(plan1);
System.out.println(plan2);
PlanComaprisionReportBuilder reportBuilder = new PlanComaprisionReportBuilder();
System.out.println(reportBuilder.build(plan1, plan2));
}
private static void normalizeData()
{
plan1 = buildPlan(plan1Map, plan1AdditionalDetailsMap);
plan2 = buildPlan(plan2Map, plan2AdditionalDetailsMap);
}
private static Plan buildPlan(final Map<String, Map<String, String>> planMap,
final Map<String, Map<String, String>> planAdditionalDetailsMap)
{
String planId = Iterables.getOnlyElement(planMap.keySet());
Plan plan = new Plan(planId);
Map<String, String> planDetails = planMap.get(planId);
Iterator<Entry<String, String>> features = planDetails.entrySet().iterator();
Map<String, String> additionalDetails = planAdditionalDetailsMap.get(planId);
while (features.hasNext())
{
Entry<String, String> entry = features.next();
String tagId = entry.getKey();
String tagDescription = entry.getValue();
String tagValue = additionalDetails.get(tagId);
plan.addFeature(new Feature(tagId, tagDescription, tagValue));
}
return plan;
}
private static void initiaizeData()
{
plan1Map = Maps.newHashMap();
Map map1Value = Maps.newTreeMap();
map1Value.put("id_1", "Plan Features");
map1Value.put("a", "Deductible");
map1Value.put("b", "Individual");
map1Value.put("c", "Family");
map1Value.put("id_4", "Individual Out-of-network");
map1Value.put("id_2", "Out-of-pocket Annual Maximum");
map1Value.put("d", "Individual");
map1Value.put("e", "Family");
map1Value.put("u", "Life Time Maximum");
plan1Map.put("343", map1Value);
plan1AdditionalDetailsMap = Maps.newHashMap();
Map<String, String> policy1ExtensionValue = Maps.newTreeMap();
policy1ExtensionValue.put("id_1", "");
policy1ExtensionValue.put("a", "Calendar Year");
policy1ExtensionValue.put("b", "5000");
policy1ExtensionValue.put("c", "10000");
policy1ExtensionValue.put("id_4", "15000");
policy1ExtensionValue.put("id_2", "");
policy1ExtensionValue.put("d", "5000");
policy1ExtensionValue.put("e", "10000");
policy1ExtensionValue.put("u", "Unlimited");
plan1AdditionalDetailsMap.put("343", policy1ExtensionValue);
plan2Map = Maps.newHashMap();
Map<String, String> policy2Value = Maps.newTreeMap();
policy2Value.put("id_1", "Plan Features");
policy2Value.put("a", "Deductible");
policy2Value.put("b", "Individual");
policy2Value.put("id_5", "Individual Out-of-network");
policy2Value.put("c", "Family");
policy2Value.put("id_4", "Family Out-of-network");
policy2Value.put("id_2", "Out-of-pocket Annual Maximum");
policy2Value.put("d", "Individual");
policy2Value.put("e", "Family");
policy2Value.put("u", "Life Time Maximum");
plan2Map.put("347", policy2Value);
plan2AdditionalDetailsMap = Maps.newHashMap();
Map<String, String> policy2ExtensionValue = Maps.newTreeMap();
policy2ExtensionValue.put("id_1", "");
policy2ExtensionValue.put("a", "Calendar Year");
policy2ExtensionValue.put("b", "7000");
policy2ExtensionValue.put("id_5", "9000");
policy2ExtensionValue.put("c", "12000");
policy2ExtensionValue.put("id_4", "14000");
policy2ExtensionValue.put("id_2", "");
policy2ExtensionValue.put("d", "6000");
policy2ExtensionValue.put("e", "15000");
policy2ExtensionValue.put("u", "Unlimited");
plan2AdditionalDetailsMap.put("347", policy2ExtensionValue);
}
}
public class Plan
{
private final String id;
private final Set<Feature> features = Sets.newHashSet();
public Plan(final String id)
{
this.id = id;
}
public String getId()
{
return id;
}
public void addFeature(final Feature f)
{
features.add(f);
}
public Set<Feature> getFeatures()
{
return Collections.unmodifiableSet(features);
}
#Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
#Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
Plan other = (Plan) obj;
if (id == null)
{
if (other.id != null)
{
return false;
}
}
else if (!id.equals(other.id))
{
return false;
}
return true;
}
#Override
public String toString()
{
return "Plan [features=" + features + ", id=" + id + "]";
}
}
public class Feature
{
private final String id;
private final String description;
private final String value;
public Feature(final String id, final String description, final String value)
{
this.id = id;
this.description = description;
this.value = value;
}
public String getId()
{
return id;
}
public String getDescription()
{
return description;
}
public String getValue()
{
return value;
}
#Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
#Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
Feature other = (Feature) obj;
if (id == null)
{
if (other.id != null)
{
return false;
}
}
else if (!id.equals(other.id))
{
return false;
}
return true;
}
#Override
public String toString()
{
return "Attribute [description=" + description + ", id=" + id + ", value=" + value + "]";
}
}
public class PlanComaprisionReportBuilder
{
Multimap<String, String> build(final Plan... plans)
{
Multimap<String, String> rows = ArrayListMultimap.create(100, plans.length);
for (Plan p : plans)
{
for (Feature f : p.getFeatures())
{
rows.put(f.getDescription(), f.getValue() != null ? f.getValue() : "");
// if (!rows.containsKey(f.getDescription()))
// {
// }
// else
// {
// existing row needs separate handling
// }
}
}
return rows;
}
}

So you have two sets of hashmaps you want to compare in an excel file and both sets might or might not have the same informations in it... (and lets assume that if they have the same information, they might or not have the same keys in the maps)
I'm not sure I get what is the problem you're stuck on exactly, but here is how I would quickly go about this.
I would have a small class holding the row label ("Plan Features", for example), and holding the values of both maps you want to compare (valA, valB, for example), so something like this:
class ThisIsARow {
String label, valA, valB;
}
I would combine both sets of hashmaps in a resulting one HashMap<String, ThisIsARow> for which the key would be the label itself.
I would then loop through the first set of hashmaps, creating new instances of ThisIsARow, setting their label and valA values for each.
Then I would loop through the second set of hashmaps, looking first if there is already a ThisIsARow instance in the resulting HashMap<String, ThisIsARow> for each label, creating and adding a new one (setting its label and valB) if there's none yet for this label, or else just set the valB of the existing ThisIsARow instance.
Then I'd use Apache POI to write everything down in an Excel file. (You then only have to loop through the resulting hashmap, printing one instance of ThisIsARow per row.)
label valA valB
label valA valB
label valA valB
label valA valB
...
I hope this helps. Let me know if you need clarifications or if I'm offtrack!

Related

Inserting distinct values from XML into database table

I have XML as follows:
<employee>
<code>13</code>
<label>Admin</label>
</employee>
<employee>
<code>13</code>
<label>Admin</label>
</employee>
<employee>
<code>09</code>
<label>Logistics</label>
</employee>
In my Oracle database, I have 2 columns, namely CODE1, CODE2.
The data should be inserted like CODE1= 13 and CODE2= 09.
But, currently what is happening is that CODE1= 13 and CODE2= 13. And 09 is not been inserted in database.
It just stores the first 2 values ignoring the rest.
My requirement is that, duplicate values must be inserted only once in DB.
Expected result:
CODE1= 13, CODE2= 09
Following is my java code:
for (int i = 0; i < 2; i++) {
final int count = i + 1;
String code = null;
final Emploi[] employee = tabLieuTrav.getEmployee();
code = employee[i].getCode();
if (code != null) {
mapParam.addParamValue(CODE + count,
code);
} else {
mapParam.addParamValue(CODE + count, null,
Types.VARCHAR);
}
getCode() returns the value (e.g. 13) from tag .
Thanks in advance.
try with following solutions,
firstly you should create a Employee class including with hasCode() and equals() methods as follows,
public class Employee {
private int code;
private String lable;
public Employee(int code, String lable) {
super();
this.code = code;
this.lable = lable;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getLable() {
return lable;
}
public void setLable(String lable) {
this.lable = lable;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + code;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (code != other.code)
return false;
return true;
}
}
above hasCode() and equals() methods are generated by eclipse ide. you can creates these methods manually like this,
#Override
public boolean equals(Object obj) {
if (obj instanceof Employee) {
return Objects.equals(code, ((Employee) obj).code);
}
return false;
}
#Override
public int hashCode() {
return this.code;
}
equals Method : Indicates whether some other object is "equal to" this one. for more info
hashCode Method : Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap. for more info
then, add employee array to ArrayList. because below mentioned methods describe how to get distinct values from ArrayList.
Emploi[] employee = tabLieuTrav.getEmployee();
List<Employee> empList = new ArrayList(Arrays.asList(employee));
then, you can use one of the following methods for remove duplicate values from ArrayList (empList)
method one, remove duplicates from ArrayList using Set (A collection that contains no duplicate elements) for more info
HashSet<Employee> uniqueEmployee = new HashSet(empList);
method two, remove duplicates from ArrayList using java 8 stream distinct method (return distinct element from collection) for more info
List<Employee> uniqueEmployee = empList..stream().distinct().collect(Collectors.toList();
finally, you can use uniqueEmployee collection as follows,
for (Employee employee : uniqueEmployee) {
code = employee.getCode();
if (code != null) {
mapParam.addParamValue(CODE + count, code);
} else {
mapParam.addParamValue(CODE + count, null, Types.VARCHAR);
}
}

Two same arrays but contains method not working

I have a SmsClass:
public class SmsClass {
private String numberInside;
private String name;
public SmsClass( String numberInside ,String name) {
this.numberInside = numberInside;
this.name = name;
}
and I try to make two same ArrayList of this class:
SmsClass SmsClass3 = new SmsClass("name" , "19");
SmsClass SmsClass4 = new SmsClass("name" , "19" );
ArrayList<SmsClass> c1 = new ArrayList<>();
ArrayList<SmsClass> c2 = new ArrayList<>();
c1.add(SmsClass1);
c1.add(SmsClass2);
c2.add(SmsClass3);
c2.add(SmsClass4);
I have problem with .contains method . When I run this code :
for(int i = 0 ; i < c1.size() ; i++){
if (c1.contains(c2.get(i))) {
System.out.println("victory");
}
}
I have same arrays but nothing found.
You should override this equals method like this in SmsClass;
public class SmsClass {
private String numberInside;
private String name;
public SmsClass(String numberInside, String name) {
this.numberInside = numberInside;
this.name = name;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SmsClass)) return false;
SmsClass smsClass = (SmsClass) o;
if (numberInside != null ? !numberInside.equals(smsClass.numberInside) : smsClass.numberInside != null)
return false;
return name != null ? name.equals(smsClass.name) : smsClass.name == null;
}
}
Just an idea, you can check easily without a for loop equations of two list .
c1.removeAll(c2);
if (c1.isEmpty()) {
System.out.println("victory!");
}
You must define equals in your Object.
Something like this:
public class SmsClass {
private String numberInside;
private String name;
public SmsClass( String numberInside ,String name){
this.numberInside = numberInside;
this.name = name; }
public boolean equals(Object obj) {
if(obj instanceof SmsClass ) {
SmsClass smsObj = (SmsClass)obj;
if(smsObj.getNumberInside() == this.getNumberInside() &&
smsObj.getName() == this.getName())
return true;
}
return false;
}
}
You have not defined equals (and implicitly hashCode), thus contains can not compare your Objects. In case you do not override those both method: Object::equals will be used (which just uses reference comparison, via ==) and hashCode (in java-8) will be computed as a pseudo-random number (based on Marsaglia XOR shift algorithm).
Variable names should not start with capital letter as recommendation.
Also, you added SmsClass1, and SmsClass2 to c1 list, while adding SmsClass3, and SmsClass4 to c2 list. Although, smsClass1 and smsClass3 have same values in their fields, they are stored on different part of memory. ArrayList.contains() method check equality according to elements' references(where they placed in memory).
SmsClass SmsClass3 = new SmsClass("name" , "19");
SmsClass SmsClass4 = new SmsClass("name" , "19" );
ArrayList<SmsClass> c1 = new ArrayList<>();
ArrayList<SmsClass> c2 = new ArrayList<>();
c1.add(SmsClass1);
c1.add(SmsClass2);
c2.add(SmsClass3);
c2.add(SmsClass4);

DynamoDBMappingException: no HASH key value present

I have a class named SampleEntity i.e a POJO which will help me create my dynamoDB table.The hash key and range key have been defined clearly in POJO object but still i get an exception that the hash key is not being defined
#DynamoDBTable(tableName = "sampletable1")
public class SampleEntity {
public static final String HASH_KEY = "f1_hash";
public static final String RANGE_KEY = "f2_range";
#DynamoDBAttribute(attributeName = HASH_KEY)
#DynamoDBHashKey
private Integer feild1;
#DynamoDBAttribute(attributeName = RANGE_KEY)
#DynamoDBRangeKey
private String field2;
#DynamoDBAttribute(attributeName = "f3")
private String feild3;
#DynamoDBAttribute(attributeName = "f4")
private String feild4;
#DynamoDBAttribute(attributeName = "f5")
private String feild5;
public Integer getFeild1() {
return feild1;
}
public void setFeild1(Integer feild1) {
this.feild1 = feild1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getFeild3() {
return feild3;
}
public void setFeild3(String feild3) {
this.feild3 = feild3;
}
public String getFeild4() {
return feild4;
}
public void setFeild4(String feild4) {
this.feild4 = feild4;
}
public String getFeild5() {
return feild5;
}
public void setFeild5(String feild5) {
this.feild5 = feild5;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SampleEntity)) return false;
SampleEntity that = (SampleEntity) o;
if (!getFeild1().equals(that.getFeild1())) return false;
if (!getField2().equals(that.getField2())) return false;
if (!getFeild3().equals(that.getFeild3())) return false;
if (!getFeild4().equals(that.getFeild4())) return false;
return getFeild5().equals(that.getFeild5());
}
#Override
public int hashCode() {
int result = getFeild1().hashCode();
result = 31 * result + getField2().hashCode();
result = 31 * result + getFeild3().hashCode();
result = 31 * result + getFeild4().hashCode();
result = 31 * result + getFeild5().hashCode();
return result;
}
}
This my class and i am issuing a create table request on this class but i got DynamoDBMappingException that is no HASH key value present.
server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", "8005"});
server.start();
dynamoDBClient = new AmazonDynamoDBClient(new BasicAWSCredentials("any", "thing")).withEndpoint("http://localhost:8005");
dbMapper = new DynamoDBMapper(dynamoDBClient);
CreateTableRequest createTableRequest = ddbMapper.generateCreateTableRequest(SampleEntity.class);
createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(5L, 5L));
dynamoDBClient.createTable(createTableRequest);
SampleLoginEntity data= new SampleLoginEntity();
data.setLogin(123);
data.setField2("range");
data.setFeild3("abc");
dbMapper.save(data);
There are two possible issues I can see (one that I ran into recently), but your setup is a little different than mine.
You're using both #DynamoDBAttribute and #DynamoDBHashKey on a single item - that's not necessary, and might be messing it up, though I don't have time to test it right now. You should be able to just do, #DynamoDBHashKey(attributeName=HASH_KEY) and you'll be fine. I think as is, you might be declaring an attribute as "f1_hash", and a hash key named as "field1", both mapping to the same internal value (though I could be wrong).
The issue I was having though is actually a result of this error message being really poorly worded - it will throw this exception when you call dbMapper.save() with an object with the hash key value set to null, though if your setLogin() was supposed to be setField1() that shouldn't be the issue here.
I got the same exeption when tried to get the record by the null hashCode:
myClassMapper.load(MyClass.class, null))
Try to set:
data.setField1("some_hash");

How can I sort a collection of custom entities?

I have a list of custom entities which I need to sort in this order: valueOne, valueTwo, and valueThree.
Here is my code
public class AppRunner {
public static void main(String[] args) {
Detail d1 = new Detail("valueOne");
Detail d2 = new Detail("valueTwo");
Detail d3 = new Detail("valueFive");
Detail d4 = new Detail("valueTen");
Detail d5 = new Detail("valueOne");
Detail d6 = new Detail("valueOne");
List<Detail> details = new ArrayList<Detail>(Arrays.asList(d1, d2, d3, d4, d5, d6));
Collections.sort(details);
System.out.println(details);
}
}
My entity class
public class Detail implements Comparable<Detail> {
private String value; // there are three options: valueOne, valueTwo, and some other value
public Detail(String value) {
this.value = value;
}
#Override
public int compareTo(Detail detail) {
String val = detail.getValue();
if (val.equals(this.value) && val.equals("valueOne")) {
return 1;
} else if (val.equals(this.value) && val.equals("valueTwo")) {
return -1;
} else {
return 0;
}
}
// getter, setters, toString
}
I think that need help with compareTo method. In the end I need to get list in this order:
valueOne
valueOne
valueOne
valueTwo
valueFive
Use an Enum and compare ordinals:
enum ValidValues { valueOne, valueTwo, valueThree }
... and then:
public int compareTo(Object detail) {
return Integer.compare(ValidValues.valueOf(this.value).ordinal(),
ValidValues.valueOf(((Detail)detail).value).ordinal());
}
For a larger number of values it might be worth it to do this with a collection or map, rather than an enum.
You can have a simple getter that return a numeric value based on your String value and simply return the difference of both numeric values in your compare() method (remember that compare() should return 0 if the two objects are equals, see Comparable javadoc.
public static class Detail implements Comparable<Detail> {
private String value; // there are three options: valueOne, valueTwo, and some other value
public Detail(String value) {
this.value = value;
}
#Override
public int compareTo(Detail detail) {
return this.getNumericValue() - detail.getNumericValue();
}
private int getNumericValue() {
switch (this.value) {
case "valueOne": return 1;
case "valueTwo": return 2;
default: return 3;
}
}
}
See note at the end on possible better designs.
I suggest adding another field to the Detail class, populating that baeed on the desired sort order and value, and using this to sort.
public class Detail implements Comparable<Detail> {
private String value;
private int sortOrder;
public Detail(String value) {
this.value = value;
if(value.equals("valueOne") {
sortOrder = 1;
}
if(value.equals("valueTwo") {
sortOrder = 2;
}
...
...
}
This makes compareTo simple:
public int compareTo(Detail detail) {
if(this.sortOrder < detail.sortOrder) {
return -1;
}
if(this.sortOrder == detail.sortOrder) {
return 0;
}
if(this.sortOrder > detail.sortOrder) {
return 1;
}
}
Note:
The above is solution to your problem as it stands. However, you may have better options. If you are using Detail to represnet a fixed set of values, look at Enums. Enums in Java are very powerful and versatile. You can have a enum repsesenting a fixed set of values and use the enum in your Detail class.
If you may have a large set of values that are not predefined, you can have a Map that maps the sort order with the value.
private static Map<Int, String> sortOrder = new HashMap<Int, String>();
sortOrder.put(1, "valueOne");
...
And use this map sorting. Depending on your situation, you can popuklate this map from a database, a file on initialization or just dueing runtime.

problem with setSelectedValue Method for JList

i am having troubles when i run the main class for the application. The fact is that the setSelectedValue method doesn't work.
the code for the main class is the following:
DatabaseConnection.getInstance().connect("org.sqlite.JDBC", "jdbc:sqlite:db/Universidad.sqlite");
DatabaseTableManagers managers = DatabaseTableManagers.getInstance();
DataBaseManagerJList.getInstance().setSelectedValue("Alumnos");
system.out.println(DataBaseManagerJList.getInstance().devolver() + "1");
AlumnosTableManager atm = AlumnosTableManager.getInstance();
System.out.println(DataBaseManagerJList.getInstance().devolver() + "2");
CarrerasTableManager ctm = CarrerasTableManager.getInstance();
System.out.println(DataBaseManagerJList.getInstance().devolver() + "3");
managers.add(atm);
System.out.println(DataBaseManagerJList.getInstance().devolver() + "4");
managers.add(ctm);
System.out.println(DataBaseManagerJList.getInstance().devolver() + "5");
DataBaseManagerJFrame dbmf = new DataBaseManagerJFrame();
System.out.println(DataBaseManagerJList.getInstance().devolver() + "6");
dbmf.setVisible(true);
}
}
and i get the following result in console which shows me that the method doesn't set the default value i've put:
null1
null2
null3
null4
null5
null6
the code for the jlist class is the following:
public class DataBaseManagerJList extends JPanel
{
private static final long serialVersionUID = 1L;
private static JList tablas;
DefaultListModel model;
DatabaseTableManagers dtm = DatabaseTableManagers.getInstance();
private static DataBaseManagerJList instance = null;
public static DataBaseManagerJList getInstance()
{
if (instance == null)
{
instance = new DataBaseManagerJList();
}
return instance;
}
public DataBaseManagerJList()
{
model = new DefaultListModel();
ArrayList<String> aux = new ArrayList<String>(dtm.getTableNames());
for(int i =0;i<aux.size();i++)
{
model.addElement(aux.get(i));
}
tablas = new JList(model);
//tablas.setSelectedValue("Alumnos",true);
add(tablas);
}
public String devolver()
{
return (String) tablas.getSelectedValue();
}
public void setSelectedValue(String name)
{
tablas.setSelectedValue(name, true);
}
}
The problem you are describing usually means that the object wasn't found in the list.
The setSelectedValue() method works doing calls to object.equals() (in your case String.equals()), so the probable cause is that the strings contained in aux are capitalized in a different way than the string you are trying to select.
For instace aux may contain the string "alumnos" instead of "Alumnos". There are two alternatives for this, if you don't care about capitalization (then change the string "Alumnos" to whatever is inside aux) if you do care about the capitalization and you still want to use strings, you could simple create a MyString class that extends String and overrides the equals method, something like:
public class MyString extends String {
#Override
public boolean equals(Object obj){
if (obj instanceof String){
String obj2 = (String)obj;
return this.equalsIgnoreCase(obj2);
}
return false;
}
}
Another alternative (perhaps a nicer one) is to create a wrapper object for your list.
public class MyWrapper {
private String payload;
public MyWrapper(String payload){
this.payload = payload;
}
#Override
public String toString(){
return payload;
}
#Override
public boolean equals(Object obj){
if (obj instanceof MyWrapper){
MyWrapper obj2 = (MyWrapper)obj;
return payload.equalsIgnoreCase(obj2.payload);
}
return false;
}
}

Categories

Resources