Package test;
/**
* Customize to achieve a map function
* Store Key-value pairs, find a value object based on the key object, and make sure there are no duplicates
*/
Import Java.util.Map;
public class Test01 {
Sxtentry[] Arr=new sxtentry[990];
int size;
Defines a function implementation to put a key-value pair into an array
public void put (Object key,object value) {
Sxtentry entry=new sxtentry (key, value);
Unique row to ensure key-value pairs
for (int i = 0; i < size; i++) {
if (Arr[i].key.equals (key)) {
Arr[i].value=value;
Return
}
}
Arr[size++]=entry;
}
Defining function implementations to remove values from an array
public object get (object key) {
for (int i = 0; i < size; i++) {
if (Arr[i].key.equals (key)) {
return arr[i].value;
}
}
return null;
}
Defines whether a function has a key that gives a parameter
public boolean containskey (Object keyobject) {
for (int i = 0; i < size; i++) {
if (Arr[i].key.equals (Keyobject)) {
return true;
}
}
return false;
}
Defines whether a function has a value given to a parameter in the function's judgment
public boolean containsvalue (Object valueobject) {
for (int i = 0; i < size; i++) {
if (Arr[i].value.equals (Valueobject)) {
return true;
}
}
return false;
}
Main function to test
public static void Main (string[] args) {
test01 entry=new test01 ();
Entry.put ("Zhang San", New wife ("Little Red"));
Wife W = (wife) entry.get ("Zhang San");//strong turn to convert object into wife class
System.out.println (W.name);
}
}
Defining classes that hold key-value pairs
Class sxtentry{
Object key;
Object value;
Public Sxtentry (Object key, object value) {
Super ();
This.key = key;
This.value = value;
}
Define an empty constructor to make it easier to create new objects
Public Sxtentry () {
}
}
To create a new class to implement validation
Class wife{
String name;
Public Wife (String namestring) {
this.name=namestring;
}
}
Simple custom implementation of map classes helps to learn map classes
Custom write implements the functionality in map in Java (easy)