I am trying to set manufacture price code ,that value is in my map
object but when I want to get getName() from map object I am not able
to get that particular value. If I use
ipcToMFPNameMap.getClass().getName()
this line of code to get particular value I get "java.util.HashMap" in
my manufacture price code filed for your reference I post my code what I tried to get the particular result
private Item getItemManufacturerPriceCodes(Item item) {
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
List<String> priceCodeList = new ArrayList<String>();
for (ItemPriceCode ipc : itemPriceCodes) {
//get the string value from the list
priceCodeList.add(ipc.getPriceCode());
}
//pass this string value in query
List<ManufacturerPriceCodes>mpc = manufacturerPriceCodesRepository.
findByManufacturerIDAndPriceCodeInAndRecordDeleted(item.getManufacturerID(),priceCodeList,NOT_DELETED);
//Convert list to map
Map<String, ManufacturerPriceCodes> ipcToMFPNameMap = mpc.stream().collect(
Collectors.toMap(ManufacturerPriceCodes :: getPriceCode,Function.identity()));// Object
for (ItemPriceCode ipcs : itemPriceCodes) {
ipcs.setManufacturerPriceCode(ipcToMFPNameMap.getClass().getName());
}
item.getItemPriceCodes()
.removeIf(ipcs -> DELETED.equals(ipcs.getRecordDeleted()));
return item;
}
I got this type of Result
But I want this this type of Result
I get issue exact at this point
ipcs.setManufacturerPriceCode(ipcToMFPNameMap.getClass().getName());
my manufacture price code is a string type
Your Map contains ManufacturerPriceCodes objects keyed on their priceCode which is defined as a String. So you can get the name of an item in the Map as follows (where priceCode is a String).
String manufacturerPriceCodesName = ipcToMFPNameMap.get(priceCode).getName();
The ManufacturerPricesCodes appears to capture the price code as a String but Item has a List<ItemPriceCode>. You'll have to figure out how to map back and forth.
Related
I want to modify my existing code and my new code work same as my old code . My requirement is I want to iterate for each loop only once how may value I pass and map the list of string value then find and store the value using map. In my old code if I pass ten value my loop iterate ten time but I do not want this type for your reference I post my old code and new code
private Item getItemManufacturerPriceCodes(Item item) {
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
//Optional<ManufacturerPriceCodes> mpc = manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), itemPriceCodes, NOT_DELETED);
for(ItemPriceCode ipc : itemPriceCodes) {
Optional<ManufacturerPriceCodes> mpc = manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), ipc.getPriceCode(), NOT_DELETED);
if(mpc.isPresent())
ipc.setManufacturerPriceCode(mpc.get().getName());
}
item.getItemPriceCodes()
.removeIf(ipc -> DELETED.equals(ipc.getRecordDeleted()));
return item;
}
Above is my old code. I want my new code work same as my old code.
private Item getItemManufacturerPriceCodes(Item item) {
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
List<String> priceCodeList = new ArrayList<String>();
for (ItemPriceCode ipc : itemPriceCodes) {
//get the string value from the list
priceCodeList = ipc.getPriceCode();
}
//pass this string value in query
List<ManufacturerPriceCodes>mpc = manufacturerPriceCodesRepository.
findByManufacturerIDAndPriceCodeInAndRecordDeleted(item.getManufacturerID(),priceCodeList,NOT_DELETED);
//Convert list to map
Map<String, ManufacturerPriceCodes> ipcToMFPNameMap = mpc.stream().collect(
Collectors.toMap(ManufacturerPriceCodes :: getPriceCode,Function.identity()));
itemPriceCodes.forEach(ipc ->{
if(ipcToMFPNameMap.get(ipc.getManuf)
});
return item;
}
I want to assign list of ItemPriceCode to priceCodeList and pass the priceCodeList in side my query but I am getting issue while assign the itempricOdeList to priceCodeList. Here is my query declaration inside my repository.
List<ManufacturerPriceCodes> findByManufacturerIDAndPriceCodeInAndRecordDeleted(String manufacturerID, List<String> priceCode, Byte notDeleted);
I tried this way but I am not able get same result as my old code.
I am new to java I want to map my for each loop list elements using map and get the value from map, but I am not able to use map in my for each statement. For your reference I post my code
private Item getItemManufacturerPriceCodes(Item item) {
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes;
for(ItemPriceCode ipc : itemPriceCodes) {
Optional<ManufacturerPriceCodes> mpc = manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), ipc.getPriceCode(), NOT_DELETED);
if(mpc.isPresent())
ipc.setManufacturerPriceCode(mpc.get().getName());
}
item.getItemPriceCodes()
.removeIf(ipc -> DELETED.equals(ipc.getRecordDeleted()));
return item;
}
I want to use my query code line above for each loop and pass list of price code inside map then get the values from map. this above code works fine . when I pass one price code value the loop move one time but when pass ten value in that case loop move ten times. But I want loop always move one time how many value I pass using map. how can I do it.
I want to use below line above for each loop
Optional<ManufacturerPriceCodes> mpc = manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), ipc.getPriceCode(), NOT_DELETED);
Getting same result using map. First of pass list of elements price code inside map and get the values from map then set those values.
I tried below way but it not working as above programme
private Item getItemManufacturerPriceCodes(Item item) {
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
Optional<ManufacturerPriceCodes> mpc = manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), itemPriceCodes, NOT_DELETED);
for(ItemPriceCode ipc : itemPriceCodes) {
if(mpc.isPresent())
ipc.setManufacturerPriceCode(mpc.get().getName());
}
item.getItemPriceCodes()
.removeIf(ipc -> DELETED.equals(ipc.getRecordDeleted()));
return item;
}
How can I map these list of price code and set them. My main aim is
modify those piece of code using map and work same as my above code
that i explain in my problem statement.
Is it possible to modify those codes using map.
Seems like you are trying to find a way to call setManufacturerPriceCode on a list of itemPriceCodes based on the ManufacturerPriceCodes that references them. Assuming there is no reference from the ItemPriceCode to the ManufacturerPriceCodes, I'd go about this differently:
In your ManufacturerPriceCodesRepository repository:
#Query("Select ipc.id, mpc.name from ManufacturerPriceCodes mpc join mpc.priceCode ipc where mpc.id = :id and ipc in :itemPriceCodes and mpc.recordDeleted = :notDeleted")
List<Object[]> findMFPNameByIdAndRecordDeletedAndPriceCodes(String Id, <type> recordDeleted, List<ItemPriceCode> itemPriceCodes)
This will return a List<Object[]>, which each Object[] representing one ItemPriceCode id/name pair. You can use this in your getItemManufacturerPriceCodes method:
private Item getItemManufacturerPriceCodes(Item item) {
List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();
List<Object[]> keyPairs= manufacturerPriceCodesRepository.findMFPNameByIdAndRecordDeletedAndPriceCodes(item.getManufacturerID(), NOT_DELETED, itemPriceCodes);
Map<String,String> ipcToMFPNameMap = keyPairs.stream().collect(
Collectors.toMap(x -> x[0], x->x[1]));
itemPriceCodes
.forEach(ipc ->{
if (ipcToMFPNameMap.get(ipc.getId())!=null)
ipc.setManufacturerPriceCode(ipcToMFPNameMap.get(ipc.getId());
})
.removeIf(ipc -> DELETED.equals(ipc.getRecordDeleted()));
return item;
}
I'm sure there are more elegant ways than dealing with object[] with Spring, and certainly for the stream handling, but this is a general idea.
How to group all the Objects in a List with the same Object property? Without mentioning the Object property value.
Model Class:
public class Item {
private String id;
private String name;
private String team
}
List<item> items = new ArrayList();
I have tried this:
items.stream().filter(item -> "Elites".equals(item.team)).collect(Collectors.toList());
But this requires passing the team name as a parameter.
How can we group the items without specifying a team value?
And Making a HashMap with Key as the item. team and value as a list of key-value pairs with that team.name & item.id
Like this:
"item.team":{
"item.id":"item.name",
"item.id":"item.name",
"item.id":"item.name",
.....
}
If we can return a Map<String, List<Item>>, where the key is the team and the value is a List<Item> belonging to that team, we can use
final Map<String, List<Item>> itemsByTeam =
items.stream().collect(Collectors.groupingBy(item -> item.team));
Ideone demo
Remark: This solution was first posted in a comment by another user and they deleted the comment shortly after. I do not remember the user's name. If they post an answer, I will delete mine. If they do not want to post an answer, but contact me, I will credit them by name.
A comment on the code: I would recommend to introduce getters for the attributes since the stream-operation is most likely to be called outside of class Item itself, hence attribute team will not be visible. Also, this would lead to an implementation like
final Map<String, List<Item>> itemsByTeam =
items.stream().collect(Collectors.groupingBy(Item::getTeam));
which may or may not be regarded as "more pleasing" to the reader.
From the accepted answer by Turing85.
I have created a complete solution for the Question I asked.
To create an output with the following structure:
"item.team":{
"item.id":"item.name",
"item.id":"item.name",
"item.id":"item.name",
.....
}
Source data:
List<Item> itemsListData = //Get the data
Function to group the Items:
public static Map<String, List<Item>> groupItemsByTeam(Collection<Item> itemsList) {
return itemsList.stream().collect(Collectors.groupingBy(Item::team));
}
Structure the items list returned by groupItemsByTeam:
//GET THE GROUPED DATA
Map<String, List<Item>> result = groupItemsByTeam(itemsListData);
//STRUCTURE THE GROUPED DATA
for (Entry<String, List<Item>> parentItem : result .entrySet()) {
System.out.println(parentItem .getKey() + " : "); // item.team value
for (Item childItem : parentItem.getValue()) {
System.out.println(childItem.getKEY() + " = " + childItem.getVALUE());
}
System.out.println("-------------------------------------------");
}
OUTPUT:
Team A :
Item 1= Item 1 name
Item 2= Item 2 name
-------------------------------------------
Team G :
Item 456= Item 456 name
Item 254= Item 254 name
-------------------------------------------
Reference from baeldung.com
Hi i Was looking for method to retrieve ArrayList object by object's ID, some guy in topic from 2013 told that you should use Map to do this. But how can i use Object ID as a Map key?
Map<Item.getId(),Item> items = new TreeMap<>();
class Item {
// id could be any type you like. Mostly this is simple types: int, long, String, UUID
private final int id;
// ... other fields
}
Map<Integer, Item> map = new HashMap<>();
map.put(666, new Item(666));
Item item = map.get(666); // get an item with id=666;
P.S. Additionally, make sure that you realize how hashCode() and equals() work.
Is there a collection object or a approach to hold a combination of elements?
For instance, I need to create a list that contains the combination of the elements name, age, height and weight.
Creating an object for this is not a good idea in my case. Because the number of fields keep changing.
I need to create this list to pass to a query.
Any solution?
class MyContainer {
String someString;
int someInt;
}
List <MyContainer> myList = new List<>();
Something like that!?
I donĀ“t know exactly, what you mean by "Creating an object for this is not a good idea in my case". You could as an alternative create a List<Object> and put in whatever you have or even a List<List<Object>> if you want to have a List of a number of grouped objects.
The best approach would be to make an Object with all the possible elements in it.
class myObject {
String name;
Integer age;
Float weight;
// Etc
}
Or have a base class then have another class which extends this with additional elements
class myExtendedObject extends myObject{
String streetAddress;
String city;
// etc;
}
Then if you don't have an element set it to null... you could always build your query from the object itself by including a method to return your query, juct check if its null and not include in your query (Assuming you mean an sql style query)
public String buildQuery{
String query = "Select * from blahtable Where ";
query += (name != null)?" name = " + name : "";
// etc, or what ever your query needs to be
return query
}
Other wise you could just have a method which returns a map of your elements then you know what the type of each element is based on the key
public Map<String, Object> getElements{
Map<String, Object> myMap = new HashMap<String, Object>();
if(name != null)
myMap.put("Name", name);
// etc
return myMap
}
What about just using a Map for that and use attribute name as key (e.g. Weight )?
You can use any combination of attributes you want and it would be convenient to pass such collection to the query
Consider Enum map should you require more column names type safety