How to override duplicate keys in Java Map [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 2 years ago.
Improve this question
I have this as my query.
select a.cust_id,a.cust_name,b.cust_id,b.cust_name
from acustomer a,bcustomer b
DaoImp using Spring NamedJdbcparameterTemplate
method:
NamedJdbcparameterTemplate temp= new NJPT(datasource);
List<Map<String,Object>> out=temp.quertForList(query,parametermap);
But the problem is that whenever I get the output for this query in db tool, I get 4 columns but in program output I am only getting 2 columns, i.e cust_id and cust_name of a is getting overridden by b due to same key name in Map.
How can I fix this, please note the query will be different each time as I am using this method as a general one for my program and output will be a list of values, so cannot map any model class for the output.
Please note I want this function to be generic one which means the query will be changing each time and output will be of different types.

Well the easy solution would be to give your fields aliases, so that their keys would be different.
select a.cust_id a_cust_id, a.cust_name a_cust_name, b.cust_id b_cust_id, b.cust_name b_cust_name
from acustomer a, bcustomer b
where a.cust_id=b.cust_id
Then in the map you would find the aliases, a_cust_id, a_cust_name, b_cust_id, b_cust_name.

Related

Why the use of open bracket and close bracket to declare a variable in Java [closed]

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 1 year ago.
Improve this question
Can someone please explain the use of the following code in Java
private TableColumn<Books, Integer> colId;
I want to know the reason of using <?,?> to declare a variable in Java.
Those are 'Generics'. Specifically, the things inside the brackets are called 'Type Parameters'. It allows different types to be chosen when you create the object or 'variable' as you call it.
So you could have a list of numbers with: new ArrayList<Integer>()
or a list of strings with: new ArrayList<String>()
https://www.tutorialspoint.com/java/java_generics.htm
In your example, it's creating a column to keep track of ids for books. The ids will be ints and the object they are identifying is books. But, you may want another TableColumn (which behaves the same as the Book Id column) but for tracking the titles of the books: TableColumn<Books, String> colTitle

How to get average in 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 2 years ago.
Improve this question
How to get average using functional programming in java?
This is what I tried ...
It seems like its not working at IntStream.of
I would like to get average from a specific row of the array
public static void average(List<List<String>> rows){
IntStream stream = IntStream.of(e -> Integer.parseInt(e.get(2)));
OptionalDouble obj = stream.average();
if (obj.isPresent()) {
System.out.println(obj.getAsDouble());
}
else {
System.out.println("-1");
}
}
rows is the array are rows read from an excel file.
Stream.of(elem1, elem2) creates a stream with the stated elements.
Imagine you have a box with 100 fotos in it.
If you do Stream.of(box), you get a stream of boxes, returning 1 box.
What you wanted was a stream of fotos. To get that, you want box.stream(), not Stream.of(box).
Your next problem then is that you don't seem to understand what reduce does. You need to tell the system how to integrate two results, not just how to get a result.
What you want here isn't reducing in the first place, you want to map a given 'foto' (a List of string in your case) to an integer, which requires not just e.get(), but also an Integer.parseInt, and you want map, not reduce.

What are the pros and cons of using Stream sorting over Collections sorting in Java? [closed]

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 5 years ago.
Improve this question
Lets say I have a pojo Model as:
class Model{
String id;
String name;
}
List in Java, and I want to sort an already filled list List models.
For now, I'm considering two options:
Using Colletions.List :
models.sort(.sort(Comparator.comparing(Model::getId)))
Using sorted function of Java8 Stream API:
models..stream().sorted(Comparator.comparing(Model::getId)).collect(Collectors.toList())
Can anyone please explain pros and cons of using Method 2 over Method 1?
I believe the biggest difference is that if you use list.sort() it actually sorts the list. If you use list.stream().sorted() that returns a sorted list but doesn't actually sort the list you start from. There might be cases for both - depending on what you prefer.

regarding generating a unique value for a set of numbers and regenerating the set of values when we pass back the same unique value [closed]

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 8 years ago.
Improve this question
I need to write a function (logic) so that for a set say A{1,2,3,4,5} i generate a unique value .
and when i pass back the same value back to the function it should return me the set of values present in the set .
For example the unique value generated is say '5' then when i pass 5 as input to the function it should give me all the values of set A i.e 1,2,3,4,5.
So i need to know if it can be achieved using any statistical approach like mean median mode etc something like that .
Something like this?
$valSet = array(1, 2, 3, 4, 5);
$authNum = 5;
if($authNum == $someInputByUser)
{
print $varSet;
}
Is this what you mean?

Groovy remove value from collection [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
In groovy is there a specific way to remove a value from a collection. For example I have a list of form fields but two of them are hidden fields and I'm trying to figure out how to remove them from the collection. The two parameters I'm trying to remove are salesKey and topicSelection. Groovy newbie so code samples are most helpful
request.requestParameterMap.collect { key, value -> "$key: ${value[0].string}" }.join("\n")
key.remove("salesKey")
key.remove("topicSelection")
I think you could use findAll:
request.requestParameterMap.findAll { key, value ->
!( key in ["salesKey", "topicSelection"] )
}
Check out this answer.
Also, depending on your specific aims, there are a couple of other ways to remove a pair, including dropWhile (which is more or less iterating over your data struct) and minus (which isn't so much removing a pair as creating a new structure without the specified pair). Official doc here.

Categories

Resources