How do we loop Java object [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Below is the output of my code...
System.out.println(CompanyStructure.get());
Output: [com.some.spf.b2bac.facilit.api.parameter.GetkingResult$king#2357662d, com.some.spf.b2bac.facilit.api.parameter.GetkingResult$king#633ced71, com.some.spf.b2bac.facilit.api.parameter.GetkingResult$king#312aac03]
I tried to convert to json string.
jsonString = CommonUtil.convertFromEntityToJsonStr(CompanyStructure.get());
System.out.println(jsonString);
Output:[{"customerId":"1"},{"customerId":"2"},{"customerId":"3"}];
I want to fetch all names with the ids 1,2,3 through sql iam using postgresql. how do i fix this?

You get the Object representation of object. To loop though the objects that get() method use a for each:
for (GetkingResult result : CompanyStructure.get()) {
if (result.getId() == 1) // do something
}
Also you can override the toString() method of the GetkingResult object.

Related

Why does SonarQube report: The return value of "format" must be used [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 27 days ago.
Improve this question
I'm getting this code smell in SonarQube "the return value of "format" must be used".
Sample code:
ShiftStartDate.format(DateTimeFormatter.ofPattern("MMddyyy")
ShiftEndDate.format(DateTimeFormatter.ofPattern("MMddyyyy")):
I tried, but I'm not sure what to return.
format is not mutating the Date, but rater returns a new String with the provided pattern. Sonarqube is telling you, that you do nothing with the return value. So either assign it to a variable, which you will use or remove the invocations altogether.

Format the String data for its key and value using regex into json key-value pair [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Here is the sammple data which we have to wrap in double quotes "Key":"value" to format :
{Data:{Management:[{individual:{individual_Suffx:,individual_FName:XYZ,individual_LName:ABC,individual_Emplyee_Title:BOARD SECRETARY&PRESIDENT/CEO,individual_Directng_MName:MNO,individual_DOB:1960-05-21},individual_Tin:{},individual_NPI_Id:{},individual_OrgIndvdl:[{Ownrshp_MngngCntrl_EfctvDt:2009-11-30,Ownrshp_MngngCntrl_RoleCd:W},{Ownrshp_MngngCntrl_EfctvDt:2009-11-30,Ownrshp_MngngCntrl_RoleCd:10},{Ownrshp_MngngCntrl_EfctvDt:2009-11-30,Ownrshp_MngngCntrl_RoleCd:15}],Mngng_Mdcr:{}},{Mngng_Indvdl:{Mngng_Indvdl_FName:TIMOTHY,Mngng_Indvdl_LName:TOOLEY,Mngng_Indvdl_MName:C,Mngng_Indvdl_DOB:1958-07-02},Mngng_Tin:{},Mngng_NPI_Id:{},Mngng_OrgIndvdl:{OwnrshpIntrst_MngngCntrl_EfctvDt:2014-05-01,Ownrshp_MngngCntrl_RoleCd:W},Mngng_Mdcr:{}}}}
Not sure if this will cover all corner cases, but this regex should work for your example:
String json = ...
json = json.replaceAll("[^{}\\[\\]:,]+", "\"$0\"");

How do I use only one element of an ArrayList? Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If I have an initialized array in Java and I want to do a method to one element in that array I would do
array[n].method();
but how do I do the same thing with an arraylist?
arraylist[n].method() gives me an error.
With Java use get method with ArrayList:
arraylist.get(n).method();

Search without iterator for Map [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to search without iterator without using map.
Map<String,Short> map = new HashMap<String,Short>();
map.put("String2", (short)2);
map.put("String1", (short)1);
map.put("String3", (short)4);
I am looking for a way to get the value based on the key (Return 2 for value String2). Is Map the right one to use in this scenario.
Thanks !
Returning values based on a key is precisely what maps are for.
To retrieve your value you can simply do:
short returnVal = map.get("String2");

Getting Resource from strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got many resources with names based on a pattern:
R.drawable.small_house_red
R.drawable.small_house_blue
R.drawable.big_house_black
R.drawable.small_tree_red
I've got 3 strings: size, type, color. I can put all required resources to map, and get them by concated string but is it possible to get them without manually entering all combinations to the map?
You can use this
int id = context.getResources().getIdentifier("imageNameAsString", "drawable", context.getPackageName());

Categories