This is a bad way to do it, but your current data structure doesn't leave me much options.
public static void main(String[] args) {
//GIVEN
Map<String, LinkedList<Object>> data = new HashMap<>();
data.put("id", Stream.of(1, 2, 3)
.collect(Collectors.toCollection(LinkedList::new)));
data.put("name", Stream.of("a", "b", "c")
.collect(Collectors.toCollection(LinkedList::new)));
data.put("jobrole", Stream.of("x", "y", "z")
.collect(Collectors.toCollection(LinkedList::new)));
data.put("salary", Stream.of(10.0, 20.0, 30.0)
.collect(Collectors.toCollection(LinkedList::new)));
//Let's fix your data structure first
Employee[] employeesArr = null;
for (Map.Entry<String, LinkedList<Object>> entry : data.entrySet()) {
int count = 0;
if (employeesArr == null) {
employeesArr = new Employee[entry.getValue().size()];
for (int i = 0; i < employeesArr.length; i++) {
employeesArr[i] = new Employee();
}
}
switch (entry.getKey()) {
case "id":
for (Object o : entry.getValue()) {
employeesArr[count++].setId((Integer) o);
}
break;
case "name":
for (Object o : entry.getValue()) {
employeesArr[count++].setName((String) o);
}
break;
case "jobrole":
for (Object o : entry.getValue()) {
employeesArr[count++].setRole((String) o);
}
break;
case "salary":
for (Object o : entry.getValue()) {
employeesArr[count++].setSalary((Double) o);
}
break;
}
}
//employeesArr is a much better data structure
for (int i = 0; i < employeesArr.length; i++) {
//use PreparedStatement or an ORM
System.out.println("insert into DBROLTA.Employee(id, name, jobrole, salary) values ("
+ employeesArr[i].getId() + ", '"
+ employeesArr[i].getName() + "', '"
+ employeesArr[i].getRole() + "', "
+ employeesArr[i].getSalary()
+ ");");
}
}
Employee.java
public class Employee {
private int id;
private String name;
private String role;
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//other getters/setters
}
List<Map<Object, Object>>would be used instead of your approach. This way, we can easily create the insert statement for each element in the list, using the map. My advice, rather than trying to answer this, would be to fix your data structure.List<Employee>:)