Custom write implements the functionality in map in Java (easy)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.