How to convert a string list to my specific java class

18
June 13, 2019, at 1:30 PM

My problem is that I write a method that takes as parameter a list of my specific java class, and convert it to a String thanks to a getter of an information of the class and I return the string list. I would now like to do the opposite, that is, I give it a string list, and convert the string list to my specific class, and return the list. Thank for help.

private List < String > convertClassIntoString(List < HouseBean > houseBean) {
 List < String > myList = new ArrayList < > ();
 if (houseBean != null) {
  for (HouseBean list: houseBean) {
   String name = list.getName();
   if (name != null) {
    myList.add(name);
   }
  }
 }
 return myList;
}

I would now like to do the opposite, that is, I give it a string list, and convert the string list to my specific class, and return the list. this code does not work. Thank for help.

private List < HouseBean > convertStringIntoHouseBean (List < String > listOfHouseBeans) {
 List < HouseBean > myList = new ArrayList < > ();
 if (listOfHouseBeans!= null) {
  for (int i = 0; i < listOfHouseBeans.size(); i++) {
   myList .add((HouseBean ) listOfHouseBeans);
  }
 }
 return myList ;
}
Answer 1

In order to answer your question, you need to show the code for HouseBean class. If your class has a setter for its name and that is the only relevant attribute to build a HouseBean object, than your code will be:

private List<HouseBean> convertStringIntoHouseBean(List<String> listOfHouseBeans) {
 List<HouseBean> myList = new ArrayList<>();
 if(listOfHouseBeans != null) {
  for(String s : listOfHouseBeans) {
    HouseBean hb = new HouseBean();
    hb.setName(s);
    myList.add(hb);
  }
 }
 return myList;
}
Answer 2

Just define a constructor in HouseBean with a String as a parameter and change convertStringIntoHouseBean to:

private List < HouseBean > convertStringIntoHouseBean (List < String > listOfHouseBeans) {
 List < HouseBean > myList = new ArrayList < > ();
 if (listOfHouseBeans!= null) {
  for (int i = 0; i < listOfHouseBeans.size(); i++) {
   myList .add(new HouseBean(listOfHouseBeans.get(i)));
  }
 }
 return myList ;
}
READ ALSO
Jenkins Sonar Scanner vs Maven sonar:sonar goal

Jenkins Sonar Scanner vs Maven sonar:sonar goal

I tried running a sonar analysis using a java project on Jenkins using the standard maven sonar:sonar goal and also using the post step>> Execute SonarQube scannerThe maven goal I used is

25
Shuffling Characters in a String [on hold]

Shuffling Characters in a String [on hold]

guysHow do I shuffle characters in a string using only Math

18
Unable to build with Java on Sublime Text

Unable to build with Java on Sublime Text

I am trying to use java on sublime textI set the build system to JavaC, and put some simple code from a text book I have and press ctrl+B to run the program

45
Can we call synchronized method of an object inside the constructor of the object in Java?

Can we call synchronized method of an object inside the constructor of the object in Java?

I am new to JavaI am wondering if it is possible to call a synchronized method inside constructor

10