J2SE Container _map Interface _ generic type

Source: Internet
Author: User

Map Interface

Examples of Map methods

ImportJava.util.*; Public classTESTMAP2 { Public Static voidMain (String args[]) {Map<string, integer> m1 =NewHashmap<string, integer>(); M1.put ("One", 1); M1.put ("Both", 2); M1.put ("Three", 3);    System.out.println (M1.size ()); System.out.println (M1.containskey ("One")); if(M1.containskey ("the")) {      //int i = ((Integer) m1.get ("II")). Intvalue ();      inti = M1.get ("II"));    System.out.println (i); }  }}
Testmap2.java
ImportJava.util.*; Public classTestargswords {//private static final integer one = new integer (1);  Private Static Final intone = 1;  Public Static voidMain (String args[]) {Map m=NewHashMap ();  for(inti = 0; i < args.length; i++) {      //integer freq = (integer) m.get (Args[i]);            intFreq = (Integer) m.get (args[i]) = =NULL? 0: (Integer) M.get (Args[i]); //M.put (Args[i], (freq = = null?) One:new Integer (Freq.intvalue () + 1 ));M.put (Args[i], freq==0? One:freq + 1); } System.out.println (M.size ()+ "Distinct words detected:");  System.out.println (m); }}
TestargswordsJDK1.5 generics

ImportJava.util.*; Public classBasicgeneric { Public Static voidMain (string[] args) {List<String> C =NewArraylist<string>(); C.add ("AAA"); C.add ("BBB"); C.add ("CCC");  for(inti=0; I<c.size (); i++) {String s=C.get (i);        System.out.println (s); } Collection<String> C2 =NewHashset<string>(); C2.add ("AAA"); C2.add ("BBB"); C2.add ("CCC");  for(Iterator<string> it =c2.iterator (); It.hasnext ();) {String s=It.next ();        System.out.println (s); }    }}classMyNameImplementsComparable<myname> {    intAge ;  Public intcompareTo (MyName mn) {if( This. Age > Mn.age)return1; Else if( This. Age < Mn.age)return-1; Else return0; }}
Basicgeneric.java
ImportJava.util.*; Public classTestArgsWords2 {Private Static Final intone = 1;  Public Static voidMain (String args[]) {Map<string, integer> m =NewHashmap<string, integer>();  for(inti = 0; i < args.length; i++) {            if(!M.containskey (Args[i]))      {M.put (args[i], one); }      Else {          intFreq =M.get (Args[i]); M.put (Args[i], freq+ 1); }} System.out.println (M.size ()+ "Distinct words detected:");  System.out.println (m); }}
Testargswords2.javaSummary

J2SE Container _map Interface _ generic type

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.