The definition and special function realization of java--set frame TreeMap

Source: Internet
Author: User

Import Java.util.scanner;import java.util.set;import java.util.treemap;/* * Requirements: "AABABCABCDABCDE", Gets the number of occurrences of each letter in a string requires results: A (5) B (4) C (3) d (2) e (1) * * Analysis: * A: Define a string (can be improved for keyboard entry) * B: Define a TREEMAP set * key: Character * Value: Integer * C: Convert a string to a character array * D: Iterate through the character array to get each character * E: Take the character you just got as a key into the collection to find the value, see the return value * is null: The key is not present, the character as a key, 1 as the value of the store * is NOT NULL: The key exists, the value of 1 Then override store the key and value * F: Define the string buffer variable * G: Iterate through the collection, get the keys and values, perform the stitching as required * H: Convert the string buffer to a string output * * Input: Linqingxia * Results: result:a (1) G (1) I (3) L (1) n (2) Q (1) x (1) */public class Treemapdemo {public static void main (string[] args) {//define a string (can be improved for keyboard entry) Scanner sc = new Scanner ( system.in); System.out.println ("Please enter a string:"); String line = Sc.nextline ();//define a TreeMap collection Treemap<character, integer> TM = new Treemap<character, integer> ();//Convert a string to a character array char[] CHS = Line.tochararray ();//Iterate through the character array, get each character for (char Ch:chs) {///take the obtained character as a key into the collection to find the value, see the return value of the integer i = Tm.get (ch);//IS null: Indicates that the key does not exist, put the character as a key, and 1 as the value to store if (i = = null) {tm.put (ch, 1);} else {//is NOT NULL: Indicates that the key exists, adds a value of 1, and then overrides store the key and value I++;tm.put (Ch,i);}} Defines the string buffer variableVolume StringBuilder sb= new StringBuilder ();//Iterate through the collection, get the keys and values, and make the stitching set<character> set = Tm.keyset () as required; for (Character key: Set) {Integer value = Tm.get (key); Sb.append (Key). Append ("("). Append (Value). Append (")");} Converts a string buffer to a string output of the strings result = Sb.tostring (); SYSTEM.OUT.PRINTLN ("Result:" +result);}}


Import Java.util.set;import java.util.treemap;/* * TreeMap: is the implementation of the map interface based on the red-black tree. *  * hashmap<string,string> * Key: String * Value: String */public class Treemapdemo {public static void main (string[] Ar GS) {//Create collection Object treemap<string, string> tm = new treemap<string, string> ();//create element and add element tm.put ("Hello", "Hello"); Tm.put ("World"), Tm.put ("Java", "Javanese"), Tm.put ("World", "Worlds 2"), Tm.put ("Java ee", "java ee");//Traversal collection set<string> Set = Tm.keyset (); for (string key:set) {String value = Tm.get (key); SYSTEM.OUT.PRINTLN (key + "---" + value);}}}


Import Java.util.comparator;import java.util.set;import java.util.treemap;/* * treemap<student,string> * key: Student * Value: String */public class TreeMapDemo2 {public static void main (string[] args) {//Create collection Object Treemap<student, Strin g> TM = new treemap<student, string> (new comparator<student> () {@Overridepublic int compare (Student s1, St Udent s2) {//main condition int num = s1.getage ()-s2.getage ();//Secondary condition int num2 = num = = 0? s1.getname (). CompareTo (S2.getname ()): Num;return num2;}); /Create student object Student S1 = new Student ("Koh Phangan", 30); Student s2 = new Student ("Willow Xia Hui", 35); Student s3 = new Student ("Tong Pak Fu", 33); Student S4 = new Student ("燕青", 32); Student S5 = new Student ("Tong Pak Fu", 33);//Storage element Tm.put (S1, "Song Dynasty"), Tm.put (S2, "Yuan Dynasty"), Tm.put (S3, "Ming Dynasty"), Tm.put (S4, "Qing Dynasty"); Tm.put (S5 , "Han Dynasty");//traverse set<student> set = Tm.keyset (); for (Student key:set) {String value = Tm.get (key); System.out.println (Key.getname () + "---" + key.getage () + "---" + value);}}}

Import Java.util.arraylist;import java.util.hashmap;import java.util.set;/* * In order to better meet the requirements: * This time the data is regarded as a student object. * * Preach Wisdom Podcast * BJ Beijing Campus * JC Basic class * Brigitte 27 * Wind Qingyang * JY Employment class * Gigi 28 * Wu Xin * SH Shanghai Campus * JC Basic class * Guo Meimei 20 * sharp Brother X JY Employment class * Luo yufeng 21 * Ma Zheng 23 * GZ Guangzhou Campus * JC Basic class * Leehom 30 * Li Jinglei * JY Employment class * Lang 31 * spokesperson * XA Xian Campus * JC Basic class * Fan Bingbing 27 * Elina * JY Employment class * Li Bingbing 28 * Zhangzhihao */public CL Hashmapdemo {public static void main (string[] args) {//Create large collection hashmap<string, hashmap<string, arraylist< student>>> Czbkmap = new hashmap<string, hashmap<string, arraylist<student>>> ();// Beijing Campus Data hashmap<string, arraylist<student>> bjczbkmap = new hashmap<string, arraylist<student> > (); arraylist<student> array1 = new arraylist<student> (); Student S1 = new Student ("Brigitte", 27); Student s2 = new Student ("Wind Qing"), Array1.add (S1); Array1.add (S2); arraylist<student> array2 = new arraylist<student> (); Student s3 = new Student ("Gigi", 28); Student S4 = new Student ("Wu Xin"); arrAy2.add (S3), Array2.add (S4), Bjczbkmap.put ("Basic Course", array1), Bjczbkmap.put ("Employment Class", array2), Czbkmap.put ("Beijing Campus", BJCZBKMAP);//You can practice it yourself//Shanghai Campus Data do//Guangzhou campus Data do//XI ' an campus data hashmap<string, arraylist<student>> Xaczbkmap = New hashmap<string, arraylist<student>> (); arraylist<student> array3 = new arraylist<student> (); Student S5 = new Student ("Fan Bingbing", 27); Student s6 = new Student ("Elina", +) Array3.add (S5); Array3.add (S6); arraylist<student> array4 = new arraylist<student> (); Student s7 = new Student ("Li Bingbing", 28); Student s8 = new Student ("Zhangzhihao"), Array4.add (S7), Array4.add (S8), Xaczbkmap.put ("Basic Class", Array3), Xaczbkmap.put ("Employment Class", ARRAY4); Czbkmap.put ("Xi ' an campus", xaczbkmap);//Traverse collection set<string> Czbkmapset = Czbkmap.keyset (); for (String Czbkmapkey: Czbkmapset) {System.out.println (Czbkmapkey); hashmap<string, arraylist<student>> czbkmapvalue = Czbkmap.get (Czbkmapkey); set<string> Czbkmapvalueset = Czbkmapvalue.keyset (); for (String CZBKMAPVALUEKEY:CZBKmapvalueset) {System.out.println ("\ T" + czbkmapvaluekey); arraylist<student> Czbkmapvaluevalue = Czbkmapvalue.get (Czbkmapvaluekey); for (Student S:czbkmapvaluevalue) { System.out.println ("\t\t" + s.getname () + "---" + s.getage ());}}}}




The definition and special function realization of java--set frame TreeMap

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.