Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: convert arraylist to a hash map

  1. #1
    Junior Member
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
     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


  2. #2
    Junior Member
    Join Date
    Jul 2009
    Location
    Chennai
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Re: convert arraylist to a hash map

    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;
        }
    }

    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);
        }
    }

Similar Threads

  1. Convert DOC,XLS to PDF with Java
    By comm in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 2nd, 2013, 04:10 AM
  2. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  3. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  5. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM