HashMap學習小結
阿新 • • 發佈:2019-01-28
import java.util.*;
publicclass HashMaps
{
publicstaticvoid main(String[] args)
{
HashMap aMap =new HashMap();
for (int i =0; i<10; i++)
aMap.put(new Element(i), new Figureout());
System.out.println("aMap: get result:");
Element test =new Element(5);
if (aMap.containsKey(test))
System.out.println((Figureout)aMap.get(test));
else
System.out.println("Not found");
}
}
class Element
{
int num;
public Element(int n)
{
num = n;
}
}
class Figureout
{
Random r =new Random();
boolean possible = r.nextDouble() >0.5;
public String toString()
{
if (possible)
return"OK";
elsereturn"Impossible";
}
}
publicclass HashMaps
{
publicstaticvoid main(String[] args)
{
HashMap aMap =new HashMap();
for (int i =0; i<10; i++)
aMap.put(new Element(i), new Figureout());
System.out.println("aMap: get result:");
Element test =new Element(5);
if (aMap.containsKey(test))
System.out.println((Figureout)aMap.get(test));
System.out.println("Not found");
}
}
class Element
{
int num;
public Element(int n)
{
num = n;
}
}
class Figureout
{
Random r =new Random();
boolean possible = r.nextDouble() >0.5;
public String toString()
{
if (possible)
return"OK";
elsereturn"Impossible";
}
}