1

I am writing a program it compiles but the code gives me a warning that it uses unchecked or unsafe operations.Recompile with -Xlint.I am unable to find the error.Please help me sort out this.

import java.io.*;
import java.util.*;
public class A1{

    private HashMap<String,ArrayList<Integer>> data= new HashMap<String,ArrayList<Integer>>();
    public  void main () throws IOException{
        BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(ob.readLine());
        for(int i=0;i<t;i++){
            String a=ob.readLine();
            String spl[]= a.split(" ");
            ArrayList<Integer> inputs= new ArrayList<Integer>();
            for(int j=0;j<Integer.parseInt(spl[0]);j++){
            int prices=Integer.parseInt(ob.readLine());
            inputs.add(prices);
            }
            Collections.sort(inputs);
            data.put(spl[1],inputs);
        }
        Iterator iter = data.entrySet().iterator();
        while(iter.hasNext()){
            Map.Entry ele = (Map.Entry)iter.next();
            int fund=Integer.parseInt((String)ele.getKey());
            System.out.println(maxhouse(fund,(ArrayList<Integer>)ele.getValue()));
        }
    }
    int maxhouse(int fund,ArrayList<Integer> a){
        int sum=0;
        int c=0;
        for(int i=0;i<a.size();i++){
            sum=sum+a.get(i);
            if(sum<fund){
                c++;
            }
            else if(sum==fund){
                c++;
                break;
            }
            else{
                break;
            }
        }
        return c;
    }
}
aryanknp
  • 1,135
  • 2
  • 8
  • 21

1 Answers1

1

Well you can do a couple of changes here, some as per the comments should never use RAW types. Hence change the iterator to -

Iterator<Entry<String, ArrayList<Integer>>> iter = data.entrySet().iterator();

and then change your Map.Entry to -

Entry<String, ArrayList<Integer>> ele = iter.next();

Also you can take advantage of java8 Map.foreach an lambda expressions to avoid all this and make the code even more better and presentable.

data.forEach((key,value) -> {
            int fund = Integer.parseInt(key);
            System.out.println(maxhouse(fund, value));
});

and avoid writing making the code more clean.

Iterator iter = data.entrySet().iterator();
while(iter.hasNext()){
 Map.Entry ele = (Map.Entry)iter.next();
 int fund=Integer.parseInt((String)ele.getKey());
 System.out.println(maxhouse(fund,(ArrayList<Integer>)ele.getValue()));
}
Parth Manaktala
  • 1,112
  • 9
  • 27
  • You can use `forEach`, but there really is no need to. An enhanced for loop is a better (simpler, more widely-available, probably faster, more powerful) choice. – Andy Turner Mar 30 '20 at 10:58