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
I have code like this and this is pure java class which does not involve and DB Connections
if(transfer.getAmount().getDestination().getCountryCode().equals("CN"){
transfer.getBankDetails().setCountryCode("CN");
transfer.setDeliveryOptions(null);
return new Event(this, "D2B");
}else if(transfer.getAmount().getDestination().getCountryCode().equals("US"){
transfer.getBankDetails().setCountryCode("CN");
transfer.setDeliveryOptions(null);
return new Event(this, "D2B");
}
and this code repeats for all the countries now I want optimize the code.
For that I have a table which has country name and country table.
So I want to get the country code from DB and reduce most of the code.
Use a HashMap<String, String> in which you will insert your list of countries. Then you just need to call you code once, like
String aCountryCode = transfer.getAmount().getDestination().getCountryCode();
String aBankCountryCode = aMapOfCountries.get(aCountryCode );
if (aBankCountryCode != null) {
transfer.getBankDetails().setCountryCode(aBankCountryCode );
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Sorry for the basic question but I have a Java String that I need to return as part of a method as a Set<String>
I cannot find a reference on how to do this. Any clues?
The usual way to do this, with a normal set:
String s = "hello";
Set<String> set = new HashSet<>();
set.add(s)
return set;
Or if you prefer an immutable set with just a single element:
String s = "hello";
return Collections.singleton(s);
If you are using Java 9 or newer, use Set.of():
return Set.of("foo");
Collections.singleton("yourString")
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 5 years ago.
Improve this question
I want to make a method return that return several times a String from a list of String. any idea ??
How about something like this:
String getRandomString(List<String> list) {
return list.get(new Random().nextInt(list.size()));
}
This isn't the most efficient way, but should do the job.
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.
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
When I execute this query I get the result in list
List<Object> movlist=movquery.list();
the result is like
[["VOD1000","sdf","Malayalam"],["VOD1002","sdf","English"],["VOD1004","sdf","Hindi"]]
But I need to get the result like
[{"channelId":"VOD1000","channelName":"sdf","channelLanguage":"Malayalam",},
{"channelId":"VOD1000","channelName":"sdf","channelLanguage":"Malayalam",},
{"channelId":"VOD1000","channelName":"sdf","channelLanguage":"Malayalam",}]
How can I do this?
If what you need is a JSON String output, I'd suggest the quick and dirty solution below:
List<Object> movlist=movquery.list();
String s = "[";
for(Object o:movlist){
Object[] array = (Object[]) o;
s += "{\"channelId\":\""+array[0]+"\",\"channelName\":\""+array[1]+"\",\"channelLanguage\":\""+array[2]+"\",},";
}
s += "]";
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");