How to remove part of a hashmap value? - java

So I'm building a group of dropdowns that rely upon each other and built a query to get the code and description for a Product Type, Family, and Model object. I used nested hashmaps to story all of the data and objects. This was fine because I can just call all of the information that I need from the hashmaps. However, when it comes to the REST API's, it's going to display all of the nested information for each of the hashmaps when I call them. For each map I have it's key, and then the value consists of a Code, Desc, and the hashmap of the next object.
So, it would be like:
Main hashmap
- Key
- value
-> code
-> desc
-> product family hashmap
-- key
-- value
--> code
--> desc
--> product model hashmap
--- key
--- value
---> code
---> desc
My main question is how can I either strip these additional hashmaps from being displayed in the json format when viewing the REST API via web browser? Or can/do I need to just completely strip the additional information altogether?
#Service
public class ProductDAOImpl implements ProductDAO {
#PersistenceContext
private EntityManager em;
#Override
public Map<String, ProductType> getProductTypeStructure() {
HashMap<String, ProductType> prodTypes = new HashMap<>();
Query q = em.createNativeQuery("<query>");
List<Object[]> prodTypeEntities = q.getResultList();
final String badData = "XX-BAD-XX";
ProductType prodType = new ProductType(badData, "");
ProductFamily prodFamily = new ProductFamily(badData, "");
for(Object[] prodTypeEntity : prodTypeEntities) {
if (prodTypeEntity[1] == null || prodTypeEntity[3] == null || prodTypeEntity[5] == null) {
continue;
}
String prodTypeCd = prodTypeEntity[0].toString().toUpperCase();
String prodTypeDesc = StringUtils.trimTrailingWhitespace(prodTypeEntity[1].toString()).toUpperCase();
String prodFamilyCd = prodTypeEntity[2].toString().toUpperCase();
String prodFamilyDesc = StringUtils.trimTrailingWhitespace(prodTypeEntity[3].toString()).toUpperCase();
String prodModelCd = prodTypeEntity[4].toString().toUpperCase();
String prodModelDesc = StringUtils.trimTrailingWhitespace(prodTypeEntity[5].toString()).toUpperCase();
if(!prodType.getCode().equalsIgnoreCase(prodTypeCd)) {
prodType = new ProductType(prodTypeCd, prodTypeDesc);
prodType.setProdFamilies(new HashMap<String, ProductFamily>());
prodTypes.put(prodType.getCode(), prodType);
prodFamily.setCode(badData);
}
if(!prodFamily.getCode().equalsIgnoreCase(prodFamilyCd)) {
prodFamily = new ProductFamily(prodFamilyCd, prodFamilyDesc);
prodFamily.setProdModels(new HashMap<String, ProductModel>());
prodType.getProdFamilies().put(prodFamily.getCode(), prodFamily);
}
prodFamily.getProdModels().put(prodModelCd, new ProductModel(prodModelCd, prodModelDesc));
}
return prodTypes;
}
}

If I understood your question correctly, I think a DTO object might be the answer here. You add to it only the values that the dropdown might need and return it from the REST API.
Here's more on DTOs.

Related

use reflection to set or get array in a Java Map

Is there a way to get or set an array element stored in a Java Map?
Example:
If we have a map like this:
{
name: "Blah",
friends: ["Foo", "Bar"]
}
Map<String, Object> myMap = new HashMap<>();
List<String> friends = new ArrayList<>();
myMap.put("name", "Blah");
myMap.put("friends", friends);
Is it possible to use Reflection to get or set the first element in the friends array in the "myMap" from the string: "myMap.friends[0]"
Your question is not very clearly written and I believe that's why you are not getting the answer you expect but, If I understood your question correctly, you need to parse the following input string at runtime that you don't know beforehand:
myMap.friends[0]
And this should be parsed into components like:
mapName = "myMap"
mapKey = "friends"
valueIndex = 0
And with this information, you need to manipulate data in a Map at runtime through reflection.
Note: This only makes sense if you could potentially have more complex expressions, using different sort of objects and accessing nested properties of retrieved objects, otherwise you wouldn't need reflection at all.
Note 2: You may want to have a look at JXPath which already does a lot of this for you based on a XPath-like syntax for navigating object graphs.
That said, if my assumptions are correct and you still want to do it yourself, consider the following example.
For the sake of demonstration, let's consider our map is returned by a method myMap inside a Context.
private static class Context {
public Map<String, Object> myMap() {
Map<String, Object> myMap = new HashMap<>();
List<String> friends = new ArrayList<>();
friends.add("Foo");
friends.add("Bar");
myMap.put("name", "Blah");
myMap.put("friends", friends);
return myMap;
}
}
I'm assuming you are already parsing the input string into the different components. If not, for this simple string you could do it with simple regular expressions. If you already have the components, let's consider the following method:
public static Object readContextMap(Context context,
String mapName, String mapKey, Integer mapValueIndex) throws Exception {
// gets Context class for inspection
Class<?> cls = context.getClass();
// search for a method based on supplied mapName
Method mapMethod = cls.getDeclaredMethod(mapName);
// get a value from the retrieved map based on mapKey
Object mapValue = mapMethod.getReturnType()
.getDeclaredMethod("get", Object.class)
.invoke(mapMethod.invoke(context), mapKey);
// if the result is of type list, use the index to return the indexed element
if (List.class.isAssignableFrom(mapValue.getClass())) {
return ((List<?>)mapValue).get(mapValueIndex);
}
// otherwise return the object itself
return mapValue;
}
For testing purposes, consider the following main method:
public static void main(String[] args) throws Exception {
Context context = new Context();
String input = "myMap.friends[0]";
// parse input into...
String mapName = "myMap";
String mapKey = "friends";
Integer valueIndex = 0;
Object firstFriend = readContextMap(context, mapName, mapKey, valueIndex);
System.out.println(firstFriend);
// prints Foo
Object name = readContextMap(context, "myMap", "name", null);
System.out.println(name);
// prints Blah
}
This should be approximately what you want. You can easily create variations of this to set values as well. Please bear in mind that this code is just for demo purposes and needs a better error handling (e.g. verify if the context is really returning a map and nothing else).
This should be something along the lines you are looking for.
There's no need to use reflection here. You can simply cast it (which is also unsafe, but less so).
You can just do this:
List<String> friends = (List<String>) myMap.get("friends");
friends.set(0, "Bob");

HTTP Request waiting for a task to get complete

I am new to REST APIS and I am trying to build a local memory Cache and writing REST APIS using java and spring framework to get and retrieve data below is the detailed description of what I want to achieve:
- Building the REST API to store key-value in local memory cache
- API must have 2 endpoints following specification below
• /cache/add (This must store unique key only (existing key must be ignored), This will return true if the element was successfully added )
•/cache/take (This method retrieves and removes the most recently added element from the cache and waits if necessary until an element becomes available)
I am not able to figure out how to implement the /cache/take method. Any suggestions will be appreciated.
for your first requirement Building the REST API to store key-value in local memory cache I've used a linkedHashMap as it'll store your key value pairs, and since it is static you could use it as a localcache
for second requirement created two end points one is storing value in linkedHashMap and other one is removing the last entry you entered in linkedHashMap, One way to get last entry of a LinkedHashMap is to use "toArray" method of Set interface. that's what I did, you could have a better approach, this answer could be improved
#RestController
public class CacheController {
private static LinkedHashMap<String, String> localCache = new LinkedHashMap<String, String>();
#RequestMapping(value = { "/cache/add" }, method = RequestMethod.GET)
public Boolean cacheAdd(#RequestParam(value = "key", required = true) String key, #RequestParam(value = "value", required = true) String value) {
if (localCache.containsKey(key)) {
return false;
}
localCache.put(key, value);
return true;
}
#RequestMapping(value = { "/cache/take" }, method = RequestMethod.GET)
public String cacheTake() {
Set<Entry<String, String>> mapValues = localCache.entrySet();
int maplength = mapValues.size();
Entry<String, String>[] cacheArray = new Entry[maplength];
mapValues.toArray(cacheArray);
System.out.print("Last Key:" + cacheArray[maplength - 1].getKey());
System.out.println(" Last Value:" + cacheArray[maplength - 1].getValue());
localCache.remove(cacheArray[maplength - 1].getKey());
return cacheArray[maplength - 1].getValue();
}
}

MAPS. Java. Return of values

I am struggling with following:
I need to create a method which returns a collection of all values that specify some particular selection criterion specified by one or more arguments.
My MAP consists of PPS numbers(keys) and values( town, name, surname, place of work ) Both are strings .
However, I am not sure what I need to do to get the values after placin in the map.
/**
*This method returns a collection of all people who work for CO-OP
*/
public Set<String> selectKeys(String factory)
{
for (Set<String>eachTaxPayers : taxPayersList.values())
{
if(taxPayersList.values().contains(factory))
{
Set<String>eachClients = taxPayersList.keySet();
System.out.println(taxPayersList.keySet());
}
}
return null ;
}
Could someone help me please?
This is a code how Map is populated.
public class Tax
{
Map<String, Set<String>>taxPayersList;
public Tax()
{
taxPayersList = new HashMap<>();
Set<String>taxPayersDetails = new HashSet<>();
taxPayersDetails.add(" Eddie Donegan");
taxPayersDetails.add("Prodieco");
taxPayersDetails.add("Limerick");
taxPayersList.put("4481908A", taxPayersDetails);
taxPayersDetails = new HashSet<>();
taxPayersDetails.add(" Paddy Power");
taxPayersDetails.add("Covenant");
taxPayersDetails.add("Limerick");
taxPayersList.put("6088989B", taxPayersDetails);
taxPayersDetails = new HashSet<>();
taxPayersDetails.add(" Mikey Obama");
taxPayersDetails.add("Prodieco");
taxPayersDetails.add("Limerick");
taxPayersList.put("6788910B", taxPayersDetails);
}
}
I want only to return the key's( PPS numbers) for people who works for the same company
public Set<String> selectKeys(String factory) {
// our return Set
Set<String> factoryWorkers = new HashSet<>();
// Let's iterate over the map entries
for (Map.Entry entry : taxPayersList.entrySet()) {
// Let's grab the value of the current map entruy
Set<String> eachTaxPayers = entry.getValue()
// Risky move
if(eachTaxPayers.contains(factory)) {
// add the key (PPS) to the return set
factoryWorkers.add(entry.getKey());
}
}
return factoryWorkers;
}
FYI, the line marked as "Risky Move" is not the best approach.
Though unlikely, it's possible a city has the same name as factory.
You'd be better using an Iterator on the Set and comparing against the 2nd value.
Even better, instead of having a Map>
you could have a Map
where Employee has fields such as name, city and factoryName.

prepare a multi valued country map

I want to prepare a HashMap in such way that
Key : Country Code
Value : List of returned orderEntries
the following process data method process every 5 orderEntry which can be from any country.
let me make it more clear. I have list of orderEntries that come from different countries now I want to put these entries into map based on country key. Like if 20 entries coming from US then US will be the key and 20 Entries would be the values. But problem is that I don't want to create a list for each county inside map.
public void processSegmentData(final List resultSet)
{
for (final Object orderEntry : resultSet)
{
if (orderEntry instanceof OrderEntryModel)
{
String countryCode = null;
final OrderModel order = ((OrderEntryModel) orderEntry).getOrder();
if (order.getDeliveryAddress() != null)
{
countryCode = order.getDeliveryAddress().getCountry().getIsocode();
}
orderEntriesMap.put(Config.getParameter(countryCode+".return.pid"), orderEntries);
}
}
}
so you are after a hashmap which contains a linked list Something along the lines of:
public HashMap<String, LinkedList<OrderEntryModel>> processSegmentData(final List resultSet) {
HashMap<String, LinkedList<OrderEntryModel>> orderEntriesMap = new HashMap<String, LinkedList<OrderEntryModel>>();
for (final Object orderEntry : resultSet) {
if (orderEntry instanceof OrderEntryModel) {
String countryCode = null;
final OrderModel order = ((OrderEntryModel) orderEntry).getOrder();
if (order.getDeliveryAddress() != null) {
countryCode = order.getDeliveryAddress().getCountry().getIsocode();
}
if (!orderEntriesMap.containsKey(countryCode)) {
orderEntriesMap.put(countryCode, new LinkedList<OrderEntryModel>());
}
orderEntriesMap.get(countryCode).add((OrderEntryModel) orderEntry);
}
}
return orderEntriesMap;
}
would be an example based on the source code you provided guessing object names.
But problem is that I don't want to create a list for each county
inside map.
I understand your problem but map store unique key, you can not store same country code.
you have to use Map<String, List<String>>() that will hold your country code as key and then put your values inside List<String>.
after doing this if you have any problem edit your question will help you to resolve that.
Just Create a Map<String,List<String>>. and follow the following approach
Map<String,List<String>> countryMap = new HashMap<String, List<String>>();
for (final String orderEntry : orders){
if(countryMap.containsKey(orderEntry.getCountry())){
countryMap.get(orderEntry.getCountry()).add(orderEntry);
}else{
//create a new list and add orderEntry
countryMap.put(orderEntry.getCountry(),orderEntry);
}
}
You need to modify this according to your stuff
You could use Guava's Multimap to simplify things. A Multimap allows you to store multiple entries against a single key, e.g.:
Multimap<String, OrderEntry> orderEntriesMultimap = HashMultimap.create();
for (final Object orderEntry : resultSet) {
// omitted...
orderEntriesMultimap.put(Config.getParameter(countryCode+".return.pid"), orderEntry);
}
You can then retrieve all the associated values by key:
Collection<OrderEntryModel> entries = orderEntriesMultimap.get(key);

Store and Search on key and value pair on appengine datastore

I want to store multiple custom key and value pair on Google Datastore entity inside the another model as a child entity. I found that there are two ways to do it
HashMap<String, String> map = new HashMap<String, String>()
(or)
List<KeyValuePair> pairs = new ArrayList<KeyValuePair>()
I really do not know which is correct method.
I also wanted to search by key and value pair which will be specified by the user to get the parent entity. The search also can have multiple key and value pair.
Please help me do it.
Google AppEngine Datastore writes and reads only simple Java data types listed in the Java Datastore Entities, Properties, and Keys documentation, not HashMap<String,String> or List<KeyValuePair> collections. However, it is possible to iterate over these collections and store each member as a separate record. The Datastore uses either a String or a long integer as the key (also known as ID or name) for each record. Thus the best fit for your Java program would be a HashMap<String,String>.
As you're open to suggestions, how about using the Datastore low level API instead of JDO? Your requirement is lightweight and a low level implementation might be simpler. For example:
// Make up some sample data
java.util.HashMap<String,String> capitals = new java.util.HashMap<String,String>();
capitals.put("France","Paris");
capitals.put("Peru","Lima");
// Create the records
com.google.appengine.api.datastore.DatastoreService datastoreService;
datastoreService = com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService();
for (String country : capitals.keySet()) {
com.google.appengine.api.datastore.Entity capitalEntity;
capitalEntity = new com.google.appengine.api.datastore.Entity("Capitals", country);
capitalEntity.setUnindexedProperty("capital", capitals.get(country)); // or setProperty if you prefer
datastoreService.put(capitalEntity);
}
// Retrieve one record
String wantedCountry = "Peru", wantedCapital;
com.google.appengine.api.datastore.Query query;
com.google.appengine.api.datastore.PreparedQuery pq;
com.google.appengine.api.datastore.Entity entity;
com.google.appengine.api.datastore.Key wantedKey;
com.google.appengine.api.datastore.Query.Filter filter;
query = new com.google.appengine.api.datastore.Query("Capitals");
wantedKey = com.google.appengine.api.datastore.KeyFactory.createKey("Capitals", wantedCountry);
filter = new com.google.appengine.api.datastore.Query.FilterPredicate(
com.google.appengine.api.datastore.Entity.KEY_RESERVED_PROPERTY,
com.google.appengine.api.datastore.Query.FilterOperator.EQUAL,
wantedKey );
query.setFilter(filter);
pq = datastoreService.prepare(query);
entity = pq.asSingleEntity();
wantedCapital = (String) entity.getProperty("capital");
// Retrieve all records
java.lang.Iterable<com.google.appengine.api.datastore.Entity> entities;
java.util.Iterator<com.google.appengine.api.datastore.Entity> entityIterator;
query = new com.google.appengine.api.datastore.Query("Capitals");
pq = datastoreService.prepare(query);
entities = pq.asIterable();
entityIterator = entities.iterator();
while (entityIterator.hasNext()) {
entity = entityIterator.next();
String foundCountry = entity.getKey().getName();
String foundCapital = (String) entity.getProperty("capital");
// ... do whatever you do with the data
}

Categories

Resources