首页 > 代码库 > hashmap&linkedlist

hashmap&linkedlist

import java.util.HashMap;
import java.util.Iterator;

public class hashmap1 {
public static void main(String [] args)
     {
    HashMap hm=new HashMap();
    sp sp1=new sp("001","话梅",12);
    sp sp2=new sp("002","苹果",14);
    hm.put("001", sp1);//键 值
    hm.put("002", sp2);
if(hm.containsKey("001"))
         {
    sp sp5=(sp)hm.get("001");
    System.out.println(sp5.getMingcheng());
         }    
else{System.out.println("对不起 没有该食品!");}

Iterator it =hm.keySet().iterator();//遍历
while(it.hasNext())
            {
    String key=it.next().toString();
    sp sp6=(sp)hm.get(key);
    System.out.println(sp6.getMingcheng());
            }

     }


}

 

linkedlist

import java.util.LinkedList;



public class linkedlist2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LinkedList ll=new LinkedList();
        sp sp1=new sp("001","话梅",12);
        sp sp2=new sp("002","苹果",14);
        
        ll.addFirst(sp1);//后进先出
        ll.addFirst(sp2);
        for (int i=0;i<ll.size();i++)
           {
            sp sp3=(sp)ll.get(i);
            sp sp4=(sp)ll.getFirst();//getfirst()是最后一个放进去,getlast是第一个放进去的
            System.out.println(sp3.getMingcheng());//后放的先取
            
            System.out.println(sp4.getMingcheng());
           }
    
    }

}

 

hashmap&linkedlist