Java container in-depth study using the abstract class customization

Source: Internet
Author: User
Tags lenovo



Package Org.rui.collection3.map;import Java.util.abstractmap;import Java.util.abstractset;import Java.util.arraylist;import Java.util.hashmap;import Java.util.hashset;import Java.util.hashtable;import Java.util.iterator;import Java.util.linkedhashmap;import Java.util.linkedhashset;import java.util.LinkedList; Import java.util.list;import java.util.map;import java.util.set;import java.util.treemap;import java.util.TreeSet;/ * Set * Use abstract class * * @author Lenovo * */public class Countries{public static final string[][] DATA ={//africa{" A "," a "},{" B "," B "},{" C "," C "},{" D "," D "},{" E "," E "},{" F "," F "},{" G "," G "},{" H "," H "},{" I "," I "},{" J "," J "},{" K "," K "},{" L "," L "},{" M "," M "},{" n "," n "},{" O "," O "},{" P "," P "},{" Q "," Q "}};p rivate static class Fly Weightmap extends Abstractmap<string, string>{private Static class Entry implements Map.entry<string, String >{int index; Entry (int index) {this.index = index;} @Overridepublic String GetKey () {return DATA[INDEX][0];} @Overridepublic String GetValue () {return data[index][1];} @Overridepublic string SetValue (string value) {throw new Unsupportedoperationexception ();} public int hashcode () {return index;} public boolean equals (Object o) {return data[index][0].equals (o);}} Use Abstractset by implementing size & Iterator by implementing Dimensions & Iterato using Set abstract static class Enterset extends ABSTRACTSET&L T map.entry<string, string>>{private int size; The enterset (int size)//share section only stores its index instead of the actual health and value {if (Size < 0) {this.size = 0;} else if (Size > data.length) {this.size = Data.length;} else{this.size = size;}} @Overridepublic int size () {return size;} Private class Iter implements Iterator<map.entry<string, string>>{//each iterator has only one entry object private Entry Entry = new E Ntry ( -1); @Overridepublic boolean hasnext () {return entry.index < size-1;} @Overridepublic java.util.map.entry<string, string> next () {Entry.index++;return Entry;} @Overridepublic void Remove () {throw new unsupportedoperationexception();}} @Overridepublic iterator<java.util.map.entry<string, string>> Iterator () {return new Iter ();}} public static set<java.util.map.entry<string, string>> entries = new Enterset (data.length); Overridepublic set<java.util.map.entry<string, string>> EntrySet () {return entries;}} Create a partial map of size countries creates a part size country map static map<string, string> Select (final int size) {return new Flyweightmap () {@Overridepublic set<java.util.map.entry<string, string>> EntrySet () {return new Flyweightmap.enterset (size);}};} Static map<string, string> map = new Flyweightmap ();p ublic static map<string, string> capitals () {return Map;} public static map<string, string> capitals (int size) {return select (size);//A partial map}static list<string > names = new arraylist<string> (Map.keyset ()),//All the Names;public static list<string> names () {return n Ames;} A partial listpublic static list<string> names (int size) {return new arraylist<string> (select (size). KeySet ());} /********************main******************************/public static void Main (string[] args) {System.out.println ( Capitals (10)); System.out.println (Names (10)); System.out.println (New hashmap<string, string> (Capitals (3))); System.out.println (New treemap<string, string> (Capitals (3))); System.out.println (New linkedhashmap<string, string> (Capitals (3))); System.out.println (New hashtable<string, string> (Capitals (3))); System.out.println (New hashset<string> (names (6))); System.out.println (New Treeset<string> (Capitals (6). KeySet ())); System.out.println (New linkedhashset<string> (names (6)));//System.out.println (new hashtable<string, String> (Capitals (3))); System.out.println (New arraylist<string> (names (6))); System.out.println (New linkedlist<string> (names (6))); SYSTEM.OUT.PRINTLN (capitals (). Get ("F"));}} /**output:{a=a, B=b, C=c, D=d, E=e, F=f, G=g, H=h, I=i, J=j}[a, B, C, D, E, F, G, H, I, J]{a=a, B=b, C=c}{a=a, B=b, C=c}{a=a, B=b, C=c}{a=a, C=c, B=b}[d, E, F, A, B, C][a, B, C, D, E, F][a, B, C, D, E, F][a, B , C, D, E, F][a, B, C, D, E, f]f*/




Package Org.rui.collection3.map;import java.util.abstractlist;/*** * It can have any size and is pre-initialized efficiently with integer data * @author Lenovo * */public class Countingintegerlist extends abstractlist<integer>{private int size;public countingintegerlist (int size) {this.size=size<0?0:size;} @Overridepublic an Integer get (int index) {return integer.valueof (index);} @Overridepublic int size () {return size;} public static void Main (string[] args) {System.out.println (new Countingintegerlist (30));}} /**output: * [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ten, one,,,, * 16, 17,  18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29] * */


Package Org.rui.collection3.map;import Java.util.abstractmap;import Java.util.linkedhashset;import java.util.Map; Import java.util.set;/** * This is using Linkedhashset instead of the custom Set class, so the element is not fully implemented * @author Lenovo * */public class Countingmapdata Ext  Ends Abstractmap<integer, string>{private int size;private static string[] chars = "A B C D E F G". Split ("");p ublic Countingmapdata (int size) {if (size < 0) This.size = 0;this.size = size;} private static class Entry implements Map.entry<integer, String>{private int index; Entry (int index) {this.index = index;} @Overridepublic Integer GetKey () {return index;} @Overridepublic String GetValue () {return chars[index% chars.length]+ integer.tostring (index/chars.length);} @Overridepublic string SetValue (string value) {throw new Unsupportedoperationexception ();} @Overridepublic boolean equals (Object obj) {return integer.valueof (index). Equals (obj); @Overridepublic int hashcode () {return integer.valueof (index). Hashcode ();}} @Overridepublic Set<java.util.map.Entry<integer, string>> EntrySet () {//Linedhashset reserved Initialization order Set<map.entry<integer, String>> Entries = new Linkedhashset<map.entry<integer, string>> (); for (int i = 0; i < size; i++) {Entries.Add (new En) Try (i)); return entries;} public static void Main (string[] args) {System.out.println (new Countingmapdata (60));}} /** * {0=a0, 1=b0, 2=c0, 3=d0, 4=e0, 5=f0, 6=g0, 7=A1, * 8=b1, 9=C1, 10=d1, 11=e1, 12=f1, 13=g1, 14=A2, 15=B2, * 16=c2, 17=D2, 18=e2, 19=f2, 20=g2, 21=a3, 22=B3, 23=C3, * 24=d3, 25=e3, 26=f3, 27=g3, 28=a4, 29=b4, 30=C4, 31=d4, * 32=E4, 33 = F4, 34=g4, 35=a5, 36=b5, 37=c5, 38=D5, 39=e5, * 40=f5, 41=g5, 42=a6, 43=b6, 44=c6, 45=d6, 46=e6, 47=f6, * 48=g6, 49=A 7, 50=b7, 51=c7, 52=d7, 53=e7, 54=f7, 55=g7, * 56=a8, 57=b8, 58=c8, 59=D8} * */


Java container in-depth study using the abstract class customization

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.