wood burning stoves 2.0*
The moose likes Java in General and the fly likes Converting a HashMap(implemented as ArrayList of LinkedLists) into a Set via entrySet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting a HashMap(implemented as ArrayList of LinkedLists) into a Set via entrySet " Watch "Converting a HashMap(implemented as ArrayList of LinkedLists) into a Set via entrySet " New topic
Author

Converting a HashMap(implemented as ArrayList of LinkedLists) into a Set via entrySet

arpita singh
Greenhorn

Joined: Oct 08, 2010
Posts: 8
Hi all,

I am implementing module for a scenario where there is a Stock of Products which can be veiwed and added to shopping cart. I am using collections for this , just for sake of practicing Collections.

I am stuck at a point .

For implementation purpose , i have implemented following classes :

Product
Stock
See
MyHashMap
The issue is , MyHashMap extends HashMap and is actually implemented as an ArrayList of LinkedLists .In See i call view_all() , there on converting the mmap (instance of MyHashMap ) to Set
it seems there are no elemnets in the Set as is displayed from output of line 48(comment)



//package p1;

import java.util.*;
import java.io.*;
import java.text.*;
public class Master
{
public static void main(String args[])
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("\n to buy hit 1 \n to window shop hit 2 \n to add to stock hit 3");

try{
Integer i= Integer.valueOf(br.readLine());
switch(i)
{
/*case 1:new Buy().buy();
break;*/
case 2:Stock.addStock();//////////NOTICE effect of sequencing in switch
case 3:new See().see();
//break;
//case 2:new Stock().addStock();
//break;
//default:System.out.println("No valid option chosen");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
System.out.println("You would veiw relevant output shortly");

}
}
}
//////////////////////////////

/*public class CustomColls

{*/

/*static*/ class MyHashMap extends HashMap /* ASK here if i add the generic types in the declaration , as done initially*/
{
//static LinkedList<Product> ArrayList=new ArrayList(LinkedList<Product>);
//LinkedList<Product> []arr=(LinkedList<Product>)arr1;
LinkedList<Product> temp= new LinkedList<Product>();
MyHashMap(){
for(int i=0;i<9;i++)// initialising an array of empty ArrayLists
{

LinkedList<Product> ll= new LinkedList<Product>();

Stock.al.add(i,ll);
}}

public Object put( Object i, Object p)
{
Integer key =(Integer)i;
System.out.println("PUT 111111"+(key%1000));
Product value= (Product)p;
System.out.println("PUT 2222222");
temp=Stock.al.get(key%10);
System.out.println("PUT 333333");
temp.add((Product)p);
System.out.println("PUT 4444444");
System.out.println("added to list");
Stock.count[key%10]++;
Product.count++;
return temp;
}

public LinkedList<Product> get(Object key)
{
Integer i=(Integer)key;
return Stock.al.get(i%10);//////ASK why didn't (Integer)key%10 work???
}
/*boolean get(K k)
{on lync
List l=*/
}
//////////////////////////

class Product
{ static int count=0;
private String gender;
private String category;
private String color;
private int barcode;
private float price;
private int discount;
private String brand;
private Date DateofArrival;
public Date getDateofArrival()
{
return this.DateofArrival;
}

public void setDateofArrival(Date d)
{
this.DateofArrival=d;
}
public String getGender()
{
return this.gender;
}

public String getCategory()
{
return this.category;
}

public String getColor()
{
return this.color;
}

public int getBarcode()
{
return this.barcode;
}

public float getPrice()
{
return this.price;
}

public String getBrand()
{
return this.brand;
}


public void setDiscount(Integer i)
{
this.discount=i;
}


public void setGender(String b)
{
this.gender=b;
}

public void setCategory(String b)
{
this.category=b;
}

public void setColor(String b)
{
this.color=b;
}

public void setBarCode(int b)
{
this.barcode=b;
}

public void setPrice(float b)
{
this.price=b;
}

public void setBrand(String b)
{
this.brand=b;
}

/*public String getGender()
{
return this.gender;
}

public String getGender()
{
return this.gender;
}*/


public int hashCode()
{
int gen=0,cat=0;
String gender=this.getGender().toUpperCase();
String category=this.getCategory().toUpperCase();
for(int i=0;i<gender.length();i++)

gen+=(int)gender.charAt(i);

for(int i=0;i<category.length();i++)
cat+=(int)category.charAt(i);

return(gen+cat);
}
public boolean equals(Product p)
{
//boolean val=false;
System.out.println("in equals");
if((this.getGender().equalsIgnoreCase(p.getGender()))&&(this.getCategory().equalsIgnoreCase(p.getCategory()))&&(this.getColor().equalsIgnoreCase(p.getColor()))&&(this.getColor().equalsIgnoreCase(p.getColor()))&&(this.getBarcode()==p.getBarcode())&&(this.getPrice()==p.getBarcode()))
return true;
else
return false;
}
}

///////////////////////////////////

class Stock
{
static int count[]=new int[9];
static ArrayList <LinkedList<Product>>al =new ArrayList<LinkedList<Product>>();
//static LinkedList<Product> array[]=new LinkedList[8];
static MyHashMap mmap=new MyHashMap();
/// see whether changes to the Map are reflected in its corresponding Collection


public static void addStock()
{
String str="";
//String inputz[]=new String[8];
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(ir);
SimpleDateFormat formatter= new SimpleDateFormat("dd-mm-yyyy");
try
{
System.out.println("maximum of 8 objs cn b inserted");

System.out.println("no. of objs to b inserted");
int c=Integer.valueOf(br.readLine());
for(int i=1;i<=c;i++)
{
/*private String gender;
private String category;
private String color;
private int barcode;
private float price;
private String discount;
private String brand;
private Date DateofArrival;*/



Product p= new Product();
System.out.println("gender,category,color,barcode,price,discount,brand,DateofArrival?");
str+=br.readLine();
String[]inputz=str.split(",");
System.out.println("has read line");
p.setGender((String)inputz[0]);
System.out.println("first in");
p.setCategory(inputz[1]);
p.setColor(inputz[2]);
p.setBarCode(Integer.valueOf(inputz[3]));
System.out.println("2nd in");
p.setPrice(Integer.valueOf(inputz[4]));
System.out.println("3rd in");
p.setDiscount(Integer.valueOf(inputz[5]));
System.out.println(p+""+""+"sm props set");
p.setBrand(inputz[6]);
p.setDateofArrival(formatter.parse(inputz[7]));
System.out.println(p+""+""+"has been set");
LinkedList<Product> itemz=(LinkedList<Product>)mmap.put(new Integer(p.hashCode()),p);
System.out.println(p+""+""+p.hashCode()+"has been added");
System.out.println(itemz);
}
}
catch(Exception e)
{
System.out.println("error");/////////////////ASK: hw cm its running without a try
e.printStackTrace();
}
}
}

////////////////////////////////////////////////////////////////////////////////////////////

class See{
//Collection<ArrayList<LinkedList<Product>>> c= Stock.mmap.values();///////////ASK why didn;t dis wrk ???
//ArrayList<LinkedList<Product>> c= (ArrayList<LinkedList<Product>>)Stock.mmap.values();

//System.out.println(c);
Iterator it=Stock.mmap.entrySet().iterator();
public void see() throws Exception
{
System.out.println("Select your options for the order in which you want to see the items\n 1:View all \n 2 :View price wise \n 3:view Brand wise\n 4:View latest arrivals");
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
Integer i=Integer.valueOf(br.readLine());

switch(i)
{
case 1:view_all();
break;
/*case 2:view_pricewise();
break;
case 3:view_brandwise();
break;
case 4:view_latest();*/

}
}
public void view_all(){
//for(int i=0;i<
System.out.println(it.hasNext());///////////LINE 48
while(it.hasNext()){
System.out.println("ll");
//for(LinkedList<Product> ll :c)
LinkedList<Product> ll=(LinkedList<Product>)it.next();
System.out.println(ll.peekFirst().getBrand());
//ListIterator<Product> li=ll.listIterator(0);
Iterator lt=ll.iterator();
while(lt.hasNext())
{
//LinkedList<Product> ll=li.next();

Product p=(Product)lt.next();
System.out.println(p.getGender()+"\n"+p.getCategory()+"\n"+p.getPrice()+"\n"+p.getColor()+"\n"+p.getBrand());
}
}
}
/*public void view_pricewise(){};
public void view_brandwise(){};
public void view_latest(){};*/

}




Where has it gone wrong ???
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

arpita singh wrote:...it seems there are no elemnets in the Set as is displayed from output of line 48(comment)...

Arpita,

1. UseCodeTags (←click) - otherwise nobody will have the slightest clue where line 48 is. You can use the 'Edit' key to add them to your post.
2. Before you do, please read the DontWriteLongLines page. Some of yours are very long which, if you do add code tags, will make your thread very hard to read (and it's actually bad coding practice).
The rules here are:
80 characters max.
(the SSCCE (←click) page actually recommends 62)
And that includes string literals AND comments AND long method calls.

However, in answer to your main question, I would suggest having a look at the keySet() and values() methods of java.util.Map.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
arpita singh
Greenhorn

Joined: Oct 08, 2010
Posts: 8
Basically , i need to know how to iterate over elements in a HashMap implemented as an ArrayList of LinkedLists ???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
There is no such thing as a hash map implemented as an array list of linked lists.
You can iterate the lists, both sorts, very easily. If you actually have a Map, you can get sets of the “K”s and the “V”s, as Winston has already hinted.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting a HashMap(implemented as ArrayList of LinkedLists) into a Set via entrySet