Storing and Retrieving ArrayList values from hashmap

后端 未结 7 2532
感动是毒
感动是毒 2020-12-13 13:27

I have a hashmap of the following type

HashMap> map=new HashMap>();    
<         


        
7条回答
  •  庸人自扰
    2020-12-13 13:51

    You can use like this(Though the random number generator logic is not upto the mark)

    public class WorkSheet {
        HashMap> map = new HashMap>();
    
    public static void main(String args[]) {
        WorkSheet test = new WorkSheet();
        test.inputData("mango", 5);
        test.inputData("apple", 2);
        test.inputData("grapes", 2);
        test.inputData("peach", 3);
        test.displayData();
    
    }
    public void displayData(){
        for (Entry> entry : map.entrySet()) {
            System.out.print(entry.getKey()+" | ");
            for(int fruitNo : entry.getValue()){
                System.out.print(fruitNo+" ");
            }
            System.out.println();
        }
    }
    public void inputData(String name ,int number) {
        Random rndData = new Random();
        ArrayList fruit = new ArrayList();
        for(int i=0 ; i

    OUTPUT

    grapes | 7 5 
    apple | 9 5 
    peach | 5 5 8 
    mango | 4 7 1 5 5 
    

提交回复
热议问题