- Import java. util. ArrayList;
- Import java. util. Collections;
- Import java. util. HashMap;
- Import java. util. List;
- Import java. util. Map;
- /**
- * Sorts the occurrences of Characters in the character set.
- *
- * @ Author Zhao Xueqing www.java2000.net
- */
- Public class T {
- Public static void main (String args []) {
- String str = "12345678hfdjkslahfkj932189oiefsjkar94werfdsf ";
- Map <Character, KeyValue> map = new HashMap <Character, KeyValue> ();
- Char c;
- KeyValue kv = null;
- For (int I = 0; I <str. length (); I ++ ){
- C = str. charAt (I );
- Kv = map. get (c );
- If (kv = null ){
- Kv = new KeyValue ();
- Kv. ch = c;
- Kv. count = 1;
- Map. put (c, kv );
- } Else {
- Kv. count ++;
- }
- }
- List <KeyValue> list = new ArrayList <KeyValue> (map. values ());
- Collections. sort (list );
- For (KeyValue o: list ){
- System. out. println (o. ch + "=" + o. count );
- }
- }
- }
- Class KeyValue implements Comparable {
- Public int compareTo (Object obj ){
- If (obj instanceof KeyValue ){
- KeyValue kv = (KeyValue) obj;
- Return kv. count-this. count;
- }
- Return-1;
- }
- Char ch;
- Int count;
- }
- Bytes -----------------------------------------------------------------------------------------------------------
- Public class TestCount
{
/**
* @ Param args
*/
Public static void main (String [] args)
{
String str = "12455547464515475354635 ";
Int [] x = new int [10];
Char [] ca = str. toCharArray ();
For (int I = 0; I <ca. length; I ++)
{
X [ca [I]-'0'] ++;
}
For (int I = 0; I <x. length; I ++)
{
System. out. println ("character" + (char) ('0' + I) + "appears" + x [I] + "times ");
}
}
}
Bytes --------------------------------------------------------------------------------------
Public static void main (String [] args ){
Int [] data = {3, 3, 5, 1, 3, 5, 3, 5, 5, 8 };
Map <Integer, Integer> m = new HashMap <Integer, Integer> ();
For (int I = 0; I <data. length; I ++ ){
If (m. get (data [I]) = null ){
M. put (data [I], 1 );
} Else {
M. put (data [I], m. get (data [I]) + 1 );
}
}
System. Out. println ("elements in map:" + M );
Int T = 0;
Set <map. Entry <integer, integer> set = M. entryset ();
For (Entry <integer, integer> entry: Set ){
If (entry. getvalue ()> T ){
T = entry. getvalue ();
}
}
System. Out. println ("the most frequently used :");
For (Entry <Integer, Integer> entry: set ){
If (entry. getValue () = t ){
System. out. println (entry. getKey ());
}
}
}
}---------------------------------------------------------------------------------
Public static void main (String [] args ){
String str = "12455547464515475354635 ";
String out = "";
Char [] ca = str. toCharArray ();
Arrays. Sort (CA );
For (char C: Ca ){
Out + = C;
}
Matcher M = pattern. Compile ("(// d) // 1 *"). matcher (out );
While (M. Find ())
System. Out. println (M. Group (). charat (0) + "count :"
+ M. Group (). Length ());
}----------------------------------------------------------------------
Import javax. Swing .*;
Public class {
Private static String s = JOptionPane. showInputDialog ("Enter a Integer number :");
Public static char getCh (String s ){
Int max = 0;
Char ch = '';
For (int I = 0; I <s. length (); I ++ ){
Char c = s. charAt (I );
Int ntimes = s. length ()-s. replaceAll (Character. toString (c), ""). length ();
If (ntimes> max ){
Max = ntimes;
Ch = c;
}
}
Return ch;
}
Public static void main (String [] args ){
System. out. println ("The number is:" + getCh (s ));
}
}
---------------------------------------------------------------------------
Import javax. Swing .*;
Public class {
Private Static string S = joptionpane. showinputdialog ("enter a integer number :");
Public static char getch (string s ){
Int max = 0;
Char CH = '';
For (INT I = 0; I <S. Length (); I ++ ){
Char c = S. charat (I );
Int ntimes = S. Length ()-S. replaceall (character. tostring (C), ""). Length ();
If (ntimes> MAX ){
Max = ntimes;
Ch = C;
}
}
Return ch;
}
Public static void main (string [] ARGs ){
System. out. println ("The number is:" + getCh (s ));
}
}