Java - merge two lists removing duplicates based on the value of a property - collections

I have a class like this:
public class MyObject {
private Long id;
private String firstProp;
private String secondProp;
private Boolean isLatest;
}
In my app, I get two lists, one that will definitely have isLatest set (Boolean.TRUE or Boolean.FALSE) and the other list that will definitely have isLatest set to null
I want a resulting List or Set which contains items from both lists but duplicates removed where isLatest == null. That is, while removing duplicates, I want to retain the elements where isLatest != null. The definition of duplicates is same id.
equals() and hashCode() methods in MyObject are based on id, firstProp and secondProp and cannot be changed as they are predefined and changing them will disturb other parts of the app.
List<MyObject> listWithIsLatestSet = list with items below
myObj1 --> id = 1, isLatest = true;
myObj2 --> id = 2, isLatest = false;
myObj3 --> id = 3, isLatest = true;
myObj4 --> id = 4, isLatest = true;
List<MyObject> listWithIsLatestNull = list with items below
myObj5 --> id = 1, isLatest = null;
myObj6 --> id = 2, isLatest = null;
myObj7 --> id = 5, isLatest = null;
The resulting list would look like below:
myObj1 --> id = 1, isLatest = true;
myObj2 --> id = 2, isLatest = false;
myObj3 --> id = 3, isLatest = true;
myObj4 --> id = 4, isLatest = true;
myObj7 --> id = 5, isLatest = null;
Can this be advised, please?
THIS WORKED FOR ME
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MAIN {
public static void main(String[] args) {
Set<MyObject> setWithIsLatestSet = getSetWithIsLatestSet();
Set<MyObject> setWithIsLatestNull = getSetWithIsLatestNull();
setWithIsLatestSet.addAll(setWithIsLatestNull);
setWithIsLatestSet.forEach(System.out::println);
}
private static Set<MyObject> getSetWithIsLatestSet() {
MyObject obj1 = MyObject.builder().id(new Long(1)).isLatest(Boolean.TRUE).build();
MyObject obj2 = MyObject.builder().id(new Long(2)).isLatest(Boolean.FALSE).build();
MyObject obj3 = MyObject.builder().id(new Long(3)).isLatest(Boolean.TRUE).build();
MyObject obj4 = MyObject.builder().id(new Long(4)).isLatest(Boolean.TRUE).build();
MyObject obj5 = MyObject.builder().id(new Long(5)).isLatest(Boolean.TRUE).build();
return Stream.of(obj1, obj2, obj3, obj4, obj5).collect(Collectors.toCollection(HashSet::new));
}
private static Set<MyObject> getSetWithIsLatestNull() {
MyObject obj1 = MyObject.builder().id(new Long(1)).build();
MyObject obj2 = MyObject.builder().id(new Long(2)).build();
MyObject obj6 = MyObject.builder().id(new Long(6)).build();
MyObject obj7 = MyObject.builder().id(new Long(7)).build();
return Stream.of(obj1, obj2, obj6, obj7).collect(Collectors.toCollection(LinkedHashSet::new));
}
}
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
#Getter
#Builder
#ToString
class MyObject {
#EqualsAndHashCode.Include
private Long id;
private Boolean isLatest;
}
Output:
MyObject(id=5, isLatest=true)
MyObject(id=6, isLatest=null)
MyObject(id=7, isLatest=null)
MyObject(id=1, isLatest=true)
MyObject(id=2, isLatest=false)
MyObject(id=3, isLatest=true)
MyObject(id=4, isLatest=true)

You can try this. Work fine for your example,
BinaryOperator<MyObject> mergeFunction = (old, lst) -> {
if (lst.getLatest() != null && lst.getLatest()) {return lst;}
else if (old.getLatest() == null && lst.getLatest() == null) {return lst;}
else if (old.getLatest() == null && lst.getLatest() != null && !lst.getLatest()) {return lst;}
else {return old;}
};
List<MyObject> collect = new ArrayList<>(Stream.concat(listWithIsLatestSet.stream(), listWithIsLatestNull.stream())
.collect(Collectors.toMap(MyObject::getId, Function.identity(), mergeFunction))
.values());

Related

how to Add list of products from cart?

dears,
i have an API working with ASP.Net Core 3.1 posting orders
i want to post order head and get all items from another api in cart items and post it in order items my code as below
[HttpPost("addOrderHead")]
public async Task<ActionResult<OrderDto>> Posting(OrderDto dto)
{
try
{
if (dto == null)
{
return BadRequest(ModelState);
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var mappedEntities = _mapper.Map<Order>(dto);
_orderRepository.Add(mappedEntities);
if (await _orderRepository.Save())
{
int id = mappedEntities.OrderID;
var cartItems = await _cartItemRepository.GetCartItems(mappedEntities.ApplicationUserId);
var cartDto = new CartItemDto();
foreach(var item in cartItems)
{
cartDto.ItemID = item.ItemID;
cartDto.ItemLookupCode = item.ItemLookupCode;
cartDto.CategoryID = item.CategoryID;
cartDto.DepartmentID = item.DepartmentID;
cartDto.itemDescription = item.itemDescription;
cartDto.SubDescription3 = item.SubDescription3;
cartDto.Quantity = item.Quantity;
cartDto.Weight = item.Weight;
cartDto.SnapShotPrice = item.SnapShotPrice;
cartDto.StoreId = item.StoreId;
cartDto.barcode = item.barcode;
cartDto.Email = item.Email;
cartDto.ItemImage = item.ItemImage;
};
var items = new OrderItems()
{
OrderId = id,
ItemID = cartDto.ItemID,
ItemLookupCode = cartDto.ItemLookupCode,
CategoryID = cartDto.CategoryID,
DepartmentID = cartDto.DepartmentID,
itemDescription = cartDto.itemDescription,
SubDescription3 = cartDto.SubDescription3,
Quantity = cartDto.Quantity,
Weight = cartDto.Weight,
SnapShotPrice = cartDto.SnapShotPrice,
StoreId = cartDto.StoreId,
barcode = cartDto.barcode,
Email = cartDto.Email,
ItemImage = cartDto.ItemImage,
};
_orderItemsRepository.Add(items);
await _orderItemsRepository.Save();
return Ok(id);
}
return BadRequest(ModelState);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.InnerException.Message);
}
}
every time i run this api order header added successfully and order items add first item only
which cart items return with array of items ,
can any one help me in that ,
You need to put Add (and maybe .Save()) inside the foreach:
var cartItems = await _cartItemRepository.GetCartItems(mappedEntities.ApplicationUserId);
foreach(var item in cartItems)
{
var cartDto = new CartItemDto(); // inside foreach
cartDto.ItemID = item.ItemID;
....
var item = new OrderItems() // one item - not: items
{
OrderId = id,
ItemID = cartDto.ItemID,
...
ItemImage = cartDto.ItemImage,
};
_orderItemsRepository.Add(item); // add item before moving to next item.
}
await _orderItemsRepository.Save();
BTW. I'm not sure why you need cartDto; I think you can eliminate it:
var cartItems = await _cartItemRepository.GetCartItems(mappedEntities.ApplicationUserId);
foreach(var item in cartItems)
{
var item = new OrderItems() // one item - not: items
{
OrderId = id,
ItemID = item.ItemID,
...
ItemImage = item.ItemImage,
};
_orderItemsRepository.Add(item); // add item before moving to next item.
}
await _orderItemsRepository.Save();
The code is not correct.In the below line you it should be List instead of object
var cartItems = await _cartItemRepository.GetCartItems(mappedEntities.ApplicationUserId);
var cartDtoList = new List<CartItemDto>();
foreach(var item in cartItems)
{
carDto carDto= new CarDto();
cartDto.ItemID = item.ItemID;
cartDto.ItemLookupCode = item.ItemLookupCode;
cartDto.CategoryID = item.CategoryID;
cartDto.DepartmentID = item.DepartmentID;
cartDto.itemDescription = item.itemDescription;
cartDto.SubDescription3 = item.SubDescription3;
cartDto.Quantity = item.Quantity;
cartDto.Weight = item.Weight;
cartDto.SnapShotPrice = item.SnapShotPrice;
cartDto.StoreId = item.StoreId;
cartDto.barcode = item.barcode;
cartDto.Email = item.Email;
cartDto.ItemImage = item.ItemImage;
carDtoList.Add(carDto)
};
Similarly the orderItems will also be list.
The other simplest solution is to put all the things inside foreach loop like this
foreach(var item in cartItems)
{
cartDto.ItemID = item.ItemID;
cartDto.ItemLookupCode = item.ItemLookupCode;
cartDto.CategoryID = item.CategoryID;
cartDto.DepartmentID = item.DepartmentID;
cartDto.itemDescription = item.itemDescription;
cartDto.SubDescription3 = item.SubDescription3;
cartDto.Quantity = item.Quantity;
cartDto.Weight = item.Weight;
cartDto.SnapShotPrice = item.SnapShotPrice;
cartDto.StoreId = item.StoreId;
cartDto.barcode = item.barcode;
cartDto.Email = item.Email;
cartDto.ItemImage = item.ItemImage;
var items = new OrderItems()
{
OrderId = id,
ItemID = cartDto.ItemID,
ItemLookupCode = cartDto.ItemLookupCode,
CategoryID = cartDto.CategoryID,
DepartmentID = cartDto.DepartmentID,
itemDescription = cartDto.itemDescription,
SubDescription3 = cartDto.SubDescription3,
Quantity = cartDto.Quantity,
Weight = cartDto.Weight,
SnapShotPrice = cartDto.SnapShotPrice,
StoreId = cartDto.StoreId,
barcode = cartDto.barcode,
Email = cartDto.Email,
ItemImage = cartDto.ItemImage,
};
_orderItemsRepository.Add(items);
await _orderItemsRepository.Save();
return Ok(id);
}

customize the height and width of ESRI map callout

here is the formatted String which i am binding to callout but i am able to see only some contents of my string in callout
private async void WebMapView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
{
try
{
WebMapView.DismissCallout();
MapPoint mapLocation = null;
var layers = await WebMapView.IdentifyLayersAsync(e.Position, 20, false);
if (layers.Count > 0)
{
foreach (var idResults in layers)
{
FeatureLayer idLayer = idResults.LayerContent as FeatureLayer;
await idLayer.LoadAsync();
var result = layers.First();
var feature = result.GeoElements.First() as ArcGISFeature;
await feature.LoadAsync(); // Load feature to get all attributes
Feature idFeature = result.GeoElements.First() as Feature;
featureAttrs = idFeature.Attributes;
var stateExtent = idFeature.Geometry;
Graphicoverlay = new GraphicsOverlay();
Graphic graphicLine = null;
graphicLine = new Graphic(stateExtent, AppConstant.HighLight1);
Graphicoverlay.Graphics.Add(graphicLine);
EnvelopeBuilder myEnvelopeBuilder = new EnvelopeBuilder(SpatialReferences.WebMercator);
//Geometry abd = oneGraphic.Geometry;
myEnvelopeBuilder.UnionOf(graphicLine.Geometry.Extent);
myEnvelopeBuilder.Expand(1.3);
mapLocation = myEnvelopeBuilder.Center;
Geometry myGeometry = GeometryEngine.Project(mapLocation, SpatialReferences.WebMercator);
MapPoint projectedLocation = (MapPoint)myGeometry;
string formattedString = "";
if (featureAttrs.Count > 0)
{
foreach (var attributes in featureAttrs)
{
string recordOneAttribute = $"{attributes.Key} {attributes.Value}";
formattedString = $"{formattedString}\n{ recordOneAttribute}";
}
}
CalloutDefinition calloutDef = new CalloutDefinition("Feature:",formattedString);
WebMapView.ShowCalloutAt(mapLocation, calloutDef);
}
}

VaultCustomQueryCriteria for State in Corda

I am trying to query a state from vault without using the linear Id of the state and instead an Int(unique) variable present in Schema
val sNumber = AState.ASchemaV1.AEntity::SNumber
val QueryCriteria = QueryCriteria.VaultCustomQueryCriteria(sNumber.equal(SalesNumber))
val StateAndRef = serviceHub.vaultService.queryBy<AState>(QueryCriteria).states.single()
val outState = StateAndRef.state.data
The Query criteria is not throwing any error but I am also not getting any output but on debugging I got an error response
javax.persistence.PersistenceException: org.hibernate.InstantiationException: No default constructor for entity: AState.ASchemaV1.AEntity
but I have defined all the columns in the function. What am I missing?
Here is code for Schema
override fun supportedSchemas() = listOf(ASchemaV1)
override fun generateMappedObject(schema: MappedSchema) = ASchemaV1.AEntity(this)
object ASchemaV1 : MappedSchema(AState::class.java, 1, listOf(AEntity::class.java)) {
#Entity
#Table(name = "Table")
class AEntity(A: AState) : PersistentState() {
#Column
var CONumber: String = A.linearId.id.toString()
#Column
var SalesNumber: Int = A.SalesNumber
#Column
var ProductID: Int = A.ProductID
#Column
var Quantity: Int = A.Quantity
#Column
var Rate: Double = A.Rate
#Column
var DeliveryDate: Date = A.DeliveryDate
#Column
var DeliveryLocation: String = A.DeliveryLocation
#Column
var Status: String = A.Status.toString()
}
}
AState.ASchemaV1 is missing the constructor.
object ASchemaV1 : MappedSchema(AState::class.java, 1, listOf(AEntity::class.java)) {
#Entity
#Table(name = "Table")
class AEntity(
#Column
var CONumber: String,
#Column
var SalesNumber: Int,
#Column
var ProductID: Int,
#Column
var Quantity: Int,
#Column
var Rate: Double,
#Column
var DeliveryDate: Date,
#Column
var DeliveryLocation: String,
#Column
var Status: String
): PersistentState() {
constructor(A: AState): this(A.linearId.id.toString(), A.SalesNumber, A.ProductID, A.Quantity, A.Rate, A.DeliveryDate, A.DeliveryLocation, A.Status.toString())
}
}

Join two dynamic Linq in Asp.net

I have used linq to store data, below is the code:
var result = (dynamic)null;
var serviceData = (dynamic)null;
var userData = (dynamic)null;
/****Linq 1*****/
serviceData= dtPasscode.AsEnumerable().Select(m => new
{
ACCOUNT_ID = intAccountId,
SUB_ACC_ID = m.Field<string>("ACCOUNT_ID_ALIAS")
});
/**Linq 2**/
userData = DisplyCustomerDetails(Convert.ToInt64(strSubAccountID));
result = serviceData.Concat(userData);
And another linq through function:
/**Function**/
System.Data.EnumerableRowCollection DisplyCustomerDetails(Int64 intAccountId)
{
var result = (dynamic)null;
/** Data Display if no service avaiable **/
IAccount_BL objAccount_BL = new Account_BL();
Customer objCustomer = new Customer();
DataTable dtCustomer = null;
int intErrorCount = 0;
objCustomer.Account_Id = Convert.ToInt64(intAccountId);
dtCustomer = objAccount_BL.GetCustomerDetails(objCustomer, ref intErrorCount);
objAccount_BL = null;
objCustomer = null;
if (intErrorCount == 0)
{
if (dtCustomer != null)
{
if (dtCustomer.Rows.Count > 0)
{
result = dtCustomer.AsEnumerable().Select(m => new
{
ACCOUNT_ID = intAccountId,
SUB_ACC_ID = m.Field<string>("ACCOUNT_ID_ALIAS")
});
}
}
}
return result;
}
I wanted to join both the result of Linq1 & Linq2, I tired Concat & Union, getting below error
'System.Data.EnumerableRowCollection<<>f__AnonymousTypea>' does not contain a definition for 'Concat'
To Concat both enumerables must of the same class; you cannot use anonymous classes. Define a class that has the two fields and change the code to Select them.
Also, don't use ... = (dynamic) null; just assign the variable directly
var serviceData= dtPasscode ...
var userData = DisplyCustomerDetails ...
var result = serviceData.Concat(userData);

To retrieve the value from session and assign it to a variable

In the below code i get all the ids in a arraylist and store it in a session in sample.aspx and retrieve the session value in test.aspx.Now i want to assign the project id to DataSet dsField in page load .How can i get that value separately.
sample.aspx
Button btnView = (Button)e.CommandSource;
Label lblProjectId = (Label)btnView.Parent.FindControl("ProjectID");
Label lblBatchID = (Label)btnView.Parent.FindControl("BatchID");
Label lblImageID = (Label)btnView.Parent.FindControl("ImageID");
Label lblReasons = (Label)btnView.Parent.FindControl("Reasons");
Label lblLayerID = (Label)btnView.Parent.FindControl("LayerID");
Label lblStatusID = (Label)btnView.Parent.FindControl("StatusID");
Label lblProcessID = (Label)btnView.Parent.FindControl("ProcessID");
ArrayList SearchUrlValues = new ArrayList();
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblBatchID);
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblImageID);
SearchUrlValues.Add(lblReasons);
SearchUrlValues.Add(lblLayerID);
SearchUrlValues.Add(lblStatusID);
SearchUrlValues.Add(lblProcessID);
Session["ProjectDetails"] = SearchUrlValues.ToArray();
Response.Write(SearchUrlValues);
test.aspx:
Array SearchUrlValues = (Array)Session["ProjectDetails"];
if (!IsPostBack)
{
DataSet dsField = GetFieldData(10);//how to assign projectid instead of 10
gmasFieldsContr.dtFieldsInfo = dsField.Tables[0];
gmasFieldsContr.EnumTable = dsField.Tables[1];
gmasFieldsContr.RegularExpressionTable = dsField.Tables[3];
gmasFieldsContr.BindData();
}
public DataSet GetFieldData(int iProjectID)
{
try
{
SqlParameter[] SqlParam = new SqlParameter[1];
SqlParam[0] = new SqlParameter("#i_ProjectID", SqlDbType.Int);
SqlParam[0].Value = iProjectID;
return ExecuteQuery(SqlParam, "spGetFieldData");
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
Edited
In Sample.aspx don't store SearchUrlValues as Array
Button btnView = (Button)e.CommandSource;
Label lblProjectId = (Label)btnView.Parent.FindControl("ProjectID");
Label lblBatchID = (Label)btnView.Parent.FindControl("BatchID");
Label lblImageID = (Label)btnView.Parent.FindControl("ImageID");
Label lblReasons = (Label)btnView.Parent.FindControl("Reasons");
Label lblLayerID = (Label)btnView.Parent.FindControl("LayerID");
Label lblStatusID = (Label)btnView.Parent.FindControl("StatusID");
Label lblProcessID = (Label)btnView.Parent.FindControl("ProcessID");
ArrayList SearchUrlValues = new ArrayList();
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblBatchID);
SearchUrlValues.Add(lblProjectId);
SearchUrlValues.Add(lblImageID);
SearchUrlValues.Add(lblReasons);
SearchUrlValues.Add(lblLayerID);
SearchUrlValues.Add(lblStatusID);
SearchUrlValues.Add(lblProcessID);
Session["ProjectDetails"] = SearchUrlValues; // Store it as ArrayList
Response.Write(SearchUrlValues);
Then test.aspx, convert Session object to ArrayList;
var SearchUrlValues = (ArrayList)Session["ProjectDetails"];
if (!IsPostBack)
{
var projectId = int.Parse(SearchUrlValues[0].ToString());
DataSet dsField = GetFieldData(projectId);//how to assign projectid instead of 10
gmasFieldsContr.dtFieldsInfo = dsField.Tables[0];
gmasFieldsContr.EnumTable = dsField.Tables[1];
gmasFieldsContr.RegularExpressionTable = dsField.Tables[3];
gmasFieldsContr.BindData();
}
By the way, please note that you're adding lblProjectId twice;
SearchUrlValues.Add(lblProjectId); // First
SearchUrlValues.Add(lblBatchID);
SearchUrlValues.Add(lblProjectId); // Second
Additionally, I would prefer to use an object to store these values in the session.
public class SearchUrlValues
{
public int lblProjectId { get; set; }
public int lblBatchID { get; set; }
public int lblImageID { get; set; }
public int lblReasons { get; set; }
public int lblLayerID { get; set; }
public int lblStatusID { get; set; }
public int lblProcessID { get; set; }
}
Then, instead of arraylist;
var newSearchUrlValues = new SearchUrlValues()
{
lblProjectId = lblProjectId,
lblBatchID = lblBatchID,
lblImageID = lblImageID,
lblReasons = lblReasons,
lblLayerID = lblLayerID,
lblStatusID = lblStatusID,
lblProcessID = lblProcessID
};
Session["ProjectDetails"] = newSearchUrlValues;
And retrieve it like;
var searchUrlValues = (SearchUrlValues)Session["ProjectDetails"];
var projectId = searchUrlValues.lblProjectId;
Try Like This
ArrayList SearchUrlValues = (ArrayList)Session["ProjectDetails"];
if (!IsPostBack)
{
DataSet dsField = GetFieldData(Convert.ToInt32(SearchUrlValues[0].ToString()));
//fetech 1st element of array List
gmasFieldsContr.dtFieldsInfo = dsField.Tables[0];
gmasFieldsContr.EnumTable = dsField.Tables[1];
gmasFieldsContr.RegularExpressionTable = dsField.Tables[3];
gmasFieldsContr.BindData();
}
public DataSet GetFieldData(int iProjectID)
{
try
{
SqlParameter[] SqlParam = new SqlParameter[1];
SqlParam[0] = new SqlParameter("#i_ProjectID", SqlDbType.Int);
SqlParam[0].Value = iProjectID;
return ExecuteQuery(SqlParam, "spGetFieldData");
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}

Resources