Go Back   Java Programming Forums > Java Standard Edition Programming Help > Collections and Generics

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-10-2008, 10:16 PM
Junior Member
 

Join Date: Sep 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nadman123 is on a distinguished road
Default 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
Java 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



Reply With Quote Share this thread on Facebook
Sponsored Links
  #2 (permalink)  
Old 29-07-2009, 09:24 AM
Junior Member
 

Join Date: Jul 2009
Location: Chennai
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
maheshrpm is on a distinguished road
Thumbs up Re: convert arraylist to a hash map

Java 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;
    }
}
Java 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);
    }
}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert String to int JavaPF Java Code Snippets and Tutorials 17 23-01-2010 02:33 PM
How to use an ArrayList - java.util.ArrayList JavaPF Java Code Snippets and Tutorials 1 17-05-2009 06:12 PM
creating objects from file input and storing in arraylist LabX File I/O & Other I/O Streams 4 14-05-2009 08:52 AM
Convert DOC and XLS to PDF comm File I/O & Other I/O Streams 1 04-03-2009 01:25 PM
Comparing Hash Functions and Collision Resolutions dansongarcia Collections and Generics 0 11-11-2008 03:50 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java ipod touch java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 09:18 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.