convert arraylist to a hash map
hey guyz how do you change all of these methods when you are replacing the arraylist with a hash map
Code :
public Order() {
this.orderNumber = orderNumberCounter;
orderNumberCounter++;
orderLines = new ArrayList<OrderLine>();
orderTime = Calendar.getInstance();
}
/**
* Constructs a new Order object with the value of the passed in parameters
*
* @param orderLines - the initial value of each order line that is in this order
*/
public Order(ArrayList<OrderLine> orderLines) {
this.orderNumber = orderNumberCounter;
orderNumberCounter++;
for (OrderLine orderLine : orderLines) {
this.orderLines.add(orderLine);
}
this.orderTime = Calendar.getInstance();
}
/**
* Return the food numbers of foods that are in this order
*
* @return a copy of attribute foodNumbers
*/
public ArrayList<OrderLine> getOrderLines() {
return (ArrayList<OrderLine>)orderLines.clone();
}
/**
* Add a food to the order along with the correponding quantity
*
* @param food - the food to add
* @param quantity - the quantity of the food
* @return true if the food item is added successfully, false otherwise
*/
public boolean addItem(Food food, int quantity) {
if (!orderLines.contains(searchOrderLine(food))) {
return this.orderLines.add(new OrderLine(food, quantity));
}
return false;
}
/**
* Add an order line to the order
*
* @param orderLine - the new order line to add in
* @return true if the orderLine is added successfully, false otherwise
*/
public boolean addItem(OrderLine orderLine) {
if (!orderLines.contains(searchOrderLine(orderLine.getFood()))) {
return this.orderLines.add(orderLine);
}
return false;
}
/**
* Remove a food from the order
*
* @param food - the food to remove
* @return true if the food item is removed successfully, false otherwise
*/
public boolean removeItem(Food food) {
return this.orderLines.remove(searchOrderLine(food));
}
/**
* Search whether a food already existed in the order
*
* @param food - the food to search
* @return the order line containing the food
*/
private OrderLine searchOrderLine(Food food) {
for (OrderLine orderLine : orderLines) {
if (orderLine.getFood().getFoodNumber() == food.getFoodNumber()) {
return orderLine;
}
}
return new OrderLine();
}
tahnx in advance
Re: convert arraylist to a hash map
Code :
package mahesh.core.util.vo;
import java.io.Serializable;
public class Dimension implements Serializable {
private long x,y;
public Dimension(){}
public Dimension(long x, long y) {
super();
this.x = x;
this.y = y;
}
public long getX() {
return x;
}
public void setX(long x) {
this.x = x;
}
public long getY() {
return y;
}
public void setY(long y) {
this.y = y;
}
public String toString(){
return x + " " + y;
}
}
Code :
package mahesh.core.util;
import mahesh.core.util.vo.Dimension;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
/**
* @author Maheshkumarp
* @version 1.0
* Date: Jul 29, 2009 - Time: 10:05:22 AM
*/
public class ListUtils<K> {
public static String camelHump(String str) {
if(str != null && (str = str.trim()).length() > 0) {
str = str.substring(0, 1).toUpperCase() + str.substring(1);
}
return str;
}
public static String convertFieldToAccessor(String filed) {
return "get" + camelHump(filed);
}
public <V> Map<K, V> toMap(List<V> list, String keyField) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String accessor = convertFieldToAccessor(keyField);
Map<K, V> map = new HashMap<K, V>();
for(V obj : list) {
Method method = obj.getClass().getDeclaredMethod(accessor);
K key = (K)method.invoke(obj);
map.put(key, obj);
}
return map;
}
public static void main(String[] args) throws Exception {
List<Dimension> dimensionList = new ArrayList<Dimension>();
dimensionList.add(new Dimension(1, 2));
dimensionList.add(new Dimension(2, 2));
dimensionList.add(new Dimension(3, 223));
dimensionList.add(new Dimension(4, 2));
Map<Long, Dimension> dimensionMap = new ListUtils<Long>().toMap(dimensionList, "x");
System.out.println(dimensionMap);
}
}