You need to sign in to do that
Don't have an account?
Tyler Harris
How to remove duplicates from a list?
Hi All,
Went through the Apex Workbook where you construct the Merchandise__c store and wanted to figure out how to remove duplicates. The workbook suggests this can be done with a Map. I have no idea where to start.
public class StoreFrontController{
list<DisplayMerchandise> products;
public list<DisplayMerchandise> getProducts(){
if(products == null){
products = new List<DisplayMerchandise>();
for (Merchandise__c item : [SELECT Id, Name, Description__c, Price__c, Total_Inventory__c FROM Merchandise__c]) {
products.add(new DisplayMerchandise(item));
}
}
return products;
}
List<DisplayMerchandise> shoppingCart = new List<DisplayMerchandise>();
public void PageReference addToCart(){
for(DisplayMerchandise p : products){
if(0 < p.qtyToBuy){
shoppingCart.add(p);
}
}
}
public String getCartContents() {
if(0 == shoppingCart.size()) {
return '(empty)';
}
String msg = '<ul>\n';
for(DisplayMerchandise p : shoppingCart){
msg += '<li>';
msg += p.name + '(' + p.qtyToBuy + ')';
msg += '</li>\n';
}
msg += '</ul>';
return msg;
}
public class DisplayMerchandise{
private Merchandise__c merchandise;
public DisplayMerchandise(Merchandise__c item){
this.merchandise = item;
}
public String name {
get{return merchandise.Name;}
}
public String description{
get {return merchandise.Description__c;}
}
public Decimal price {
get{return merchandise.Price__c;}
}
public Boolean inStock{
get{return(0 < merchandise.Total_Inventory__c);}
}
public Integer qtyToBuy{get;set;}
}
}
Went through the Apex Workbook where you construct the Merchandise__c store and wanted to figure out how to remove duplicates. The workbook suggests this can be done with a Map. I have no idea where to start.
public class StoreFrontController{
list<DisplayMerchandise> products;
public list<DisplayMerchandise> getProducts(){
if(products == null){
products = new List<DisplayMerchandise>();
for (Merchandise__c item : [SELECT Id, Name, Description__c, Price__c, Total_Inventory__c FROM Merchandise__c]) {
products.add(new DisplayMerchandise(item));
}
}
return products;
}
List<DisplayMerchandise> shoppingCart = new List<DisplayMerchandise>();
public void PageReference addToCart(){
for(DisplayMerchandise p : products){
if(0 < p.qtyToBuy){
shoppingCart.add(p);
}
}
}
public String getCartContents() {
if(0 == shoppingCart.size()) {
return '(empty)';
}
String msg = '<ul>\n';
for(DisplayMerchandise p : shoppingCart){
msg += '<li>';
msg += p.name + '(' + p.qtyToBuy + ')';
msg += '</li>\n';
}
msg += '</ul>';
return msg;
}
public class DisplayMerchandise{
private Merchandise__c merchandise;
public DisplayMerchandise(Merchandise__c item){
this.merchandise = item;
}
public String name {
get{return merchandise.Name;}
}
public String description{
get {return merchandise.Description__c;}
}
public Decimal price {
get{return merchandise.Price__c;}
}
public Boolean inStock{
get{return(0 < merchandise.Total_Inventory__c);}
}
public Integer qtyToBuy{get;set;}
}
}
I have a very simple solution to remove the duplicates from the list . I am not seeing your code but i am answering according to your question.
Try it and if your problem solve then please mark it as a best answer for other's convenience.
Thanks
Rishav
All Answers
Declare a new map, then loop through the list and add it to the map, this should get you a unique values
i.e
map<id, DisplayMerchandise> mapping = new map<id, DisplayMerchandise>();
in you for (Merchandise__c item : loop add below line
mapping.add(item.id, item);
once the loop finishes do
system.debug(mapping);
public class StoreFrontController{
list<DisplayMerchandise> products;
map<id, DisplayMerchandise> mapping = new map<id, DisplayMerchandise>();
for (Merchandise__c itemz : [SELECT Id, Name, Description__c, Price__c, Total_Inventory__c FROM Merchandise__c]) {
mapping.add(itemz.name, itemz);
}
public list<DisplayMerchandise> getProducts(){
if(products == null){
products = new List<DisplayMerchandise>();
for (Merchandise__c item : [SELECT Id, Name, Description__c, Price__c, Total_Inventory__c FROM Merchandise__c]) {
products.add(new DisplayMerchandise(item));
}
}
return products;
}
List<DisplayMerchandise> shoppingCart = new List<DisplayMerchandise>();
public PageReference addToCart(){
for(DisplayMerchandise p : products){
if(0 < p.qtyToBuy){
shoppingCart.add(p);
}
}
return null;
}
public String getCartContents() {
if(0 == shoppingCart.size()) {
return '(empty)';
}
String msg = '<ul>\n';
for(DisplayMerchandise p : shoppingCart){
msg += '<li>';
msg += p.name + '(' + p.qtyToBuy + ')';
msg += '</li>\n';
}
msg += '</ul>';
return msg;
}
I have a very simple solution to remove the duplicates from the list . I am not seeing your code but i am answering according to your question.
Try it and if your problem solve then please mark it as a best answer for other's convenience.
Thanks
Rishav
public class ReplaceDuplicate {
public static void getTemplate() {
list<string> str = new list<string>();
str.add('ram');
str.add('is');
str.add('ram');
str.add('is');
str.add('shyam');
set<string>strSet = new set<string>(str); // creating a set and assigning the list as value of set
// set is having a by default property of keeping only unique values in it.
system.debug('content is'+strset);
}
}