Java hashMap and ArrayList problems static HashMap Q1ArrayLi

Java hashMap and ArrayList problems static HashMap Q1(ArrayList inputList){//Return a HashMap containing all the input Strings as keys with values equal to the length of each String return null;} static int Q2(ArrayList inputList){//Return that sum of all the provided Integers. Note that you are given a ArrayList containing an unknown//number of ArrayLists of Integers (A Matrix of Integers) return 0;}

Solution

Methods:

private static int Q2(ArrayList<ArrayList<Integer>> arls) {
       int sum = 0;
       for (int i = 0; i < arls.size(); i++) {

           Iterator itr = arls.get(i).iterator();

           while (itr.hasNext()) {
               Integer ii = (int) itr.next();
               sum += ii;
           }

       }

       return sum;
   }

   private static HashMap<String, Integer> Q1(ArrayList<String> al) {
       HashMap<String, Integer> res = new HashMap<String, Integer>();
       for (int i = 0; i < al.size(); i++) {
           res.put(al.get(i), new Integer(al.get(i).length()));
       }
       return res;
   }

_____________________

Complete Program To make You understand:

HashMapArrayListDemo.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapArrayListDemo {

   public static void main(String[] args) {
       ArrayList<String> al = new ArrayList<String>();
       al.add(\"Williams\");
       al.add(\"Johnson\");
       al.add(\"Kanes\");
       al.add(\"Bobby\");
       HashMap<String, Integer> hm = Q1(al);
       Set s = hm.entrySet();
       Iterator i = s.iterator();
       while (i.hasNext()) {
           Map.Entry m = (Map.Entry) i.next();
           System.out
                   .println(\"Key = \" + m.getKey() + \" Value=\" + m.getValue());
       }
       ArrayList<Integer> arl1 = new ArrayList<Integer>();
       arl1.add(3);
       arl1.add(4);
       arl1.add(5);
       ArrayList<Integer> arl2 = new ArrayList<Integer>();
       arl2.add(6);
       arl2.add(7);
       arl2.add(8);
       ArrayList<Integer> arl3 = new ArrayList<Integer>();
       arl3.add(9);
       arl3.add(10);
       arl3.add(11);
       ArrayList<ArrayList<Integer>> arls = new ArrayList<ArrayList<Integer>>();
       arls.add(arl1);
       arls.add(arl2);
       arls.add(arl3);

       int tot = Q2(arls);
       System.out.println(\"The Sum of All elements is :\" + tot);

   }

   private static int Q2(ArrayList<ArrayList<Integer>> arls) {
       int sum = 0;
       for (int i = 0; i < arls.size(); i++) {

           Iterator itr = arls.get(i).iterator();

           while (itr.hasNext()) {
               Integer ii = (int) itr.next();
               sum += ii;
           }

       }

       return sum;
   }

   private static HashMap<String, Integer> Q1(ArrayList<String> al) {
       HashMap<String, Integer> res = new HashMap<String, Integer>();
       for (int i = 0; i < al.size(); i++) {
           res.put(al.get(i), new Integer(al.get(i).length()));
       }
       return res;
   }

}

___________________

Output:

Key = Johnson Value=7
Key = Williams Value=8
Key = Bobby Value=5
Key = Kanes Value=5
The Sum of All elements is :63

________Thank You

 Java hashMap and ArrayList problems static HashMap Q1(ArrayList inputList){//Return a HashMap containing all the input Strings as keys with values equal to the
 Java hashMap and ArrayList problems static HashMap Q1(ArrayList inputList){//Return a HashMap containing all the input Strings as keys with values equal to the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site