Reputation: 31
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.
Upvotes: 2
Views: 79
Reputation: 1436
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);
}
}
Upvotes: 1