By: Saravanan in Java Tutorials on 2009年10月03日 [フレーム]
In this Tutorial we are going to see how to implement Map with arraylist. This is an example of a Map. We must import some of the packages as below.
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class MapExample extends AbstractMap
{
private List listk = new ArrayList();
private List listv = new ArrayList();
public Object put(Object key, Object value)
{
Object result = get(key);
if (!listk.contains(key))
{
listk.add(key);
listv.add(value);
}
else
listv.set(listk.indexOf(key), value);
return result;
}
public Object get(Object key)
{
if (!listk.contains(key))
return null;
return listv.get(listk.indexOf(key));
}
public Set entrySet()
{
Set entries = new HashSet();
Iterator ki = listk.iterator();
Iterator vi = listv.iterator();
while (ki.hasNext())
entries.add(new Map11(ki.next(), vi.next()));
return entries;
}
public String toString()
{
StringBuffer s = new StringBuffer("{");
Iterator ki = listk.iterator(), vi = listv.iterator();
while (ki.hasNext())
{
s.append(ki.next() + "=" + vi.next());
if (ki.hasNext())
s.append(", ");
}
s.append("}");
return s.toString();
}
public static void main(String[] args)
{
MapExample m = new MapExample();
m.put("Netscape", "Mountain View, CA");
m.put("Adobe", "Mountain View, CA");
m.put("Sun", "Mountain View, CA");
m.put("Microsoft", "Redmond, WA");
m.put("IBM", "White Plains, NY");
m.put("O'Reilly", "Sebastopol, CA");
m.put("Learning Tree", "Los Angeles, CA");
System.out.println(m);
}
}
class Map11 implements Entry, Comparable
{
Object key, value;
Map11(Object k, Object v)
{
key = k;
value = v;
}
public Object getKey()
{
return key;
}
public Object getValue()
{
return value;
}
public Object setValue(Object v)
{
Object result = value;
value = v;
return result;
}
public boolean equals(Object o) {
return key.equals(((Map11)o).key);
}
public int compareTo(Object rv) {
return ((Comparable)key).compareTo(
((Map11)rv).key);
}
}
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Java )
Step by Step guide to setup freetts for Java
Open a .docx file and show content in a TextArea using Java
concurrent.Flow instead of Observable class in Java
DateFormat sample program in Java
Simple Port Scanner application using Java
Using the AWS SDK for Java in Eclipse
Read a file having a list of telnet commands and execute them one by one using Java
Calculator application in Java
Of Object, equals (), == and hashCode ()
Latest Articles (in Java)
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate