
Подсчёт символов в тексте на языке Java.
Код программы на java который подсчитывает символы в тексте и сортирует в порядке возростания.
package javaapplication3; import java.util.ArrayList; import java.util.Comparator; import java.util.Map; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; /** * * @author komar */ public class JavaApplication3 { /** * @param args the command line arguments */ public static void main(String[] args) { TreeMap map = new TreeMap(); char arr[]="Тестовое сообщение! Ура!!!".toLowerCase().toCharArray(); for(char x:arr){ try{ map.put(x,map.get(x).hashCode()+1); } catch(NullPointerException e){ map.put(x,1); } } // This is code adding only letters into TreeMap. // for(char x:arr){ // if (Character.isLetter(x)){ // try{ // map.put(x,map.get(x).hashCode()+1); // } // catch(NullPointerException e){ // map.put(x,1); // } // } // } //Add key and values into entriesSortedByValues (SortedSet) SortedSet entriesSortedByValues = entriesSortedByValues(map); //Add keys and values into array (ArrayList) ArrayList entriesArrayListByValues=new ArrayList(); for(Object x:entriesSortedByValues){ entriesArrayListByValues.add(x); } //Show entriesSortedByValues and entriesArrayListByValues System.out.println("SortedSet---->"+entriesSortedByValues); System.out.println("ArrayList---->"+entriesArrayListByValues); } /** * The magic metod which doing sorting the TreeMap to SortedSet * */ static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) { SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<>( new Comparator<Map.Entry<K,V>>() { @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) { int res = e1.getValue().compareTo(e2.getValue()); return res != 0 ? res : 1; } } ); sortedEntries.addAll(map.entrySet()); return sortedEntries; } }
Результат:
SortedSet---->[а=1, б=1, в=1, и=1, н=1, р=1, у=1, щ=1, =2, с=2, т=2, !=4, е=4, о=4]
ArrayList---->[а=1, б=1, в=1, и=1, н=1, р=1, у=1, щ=1, =2, с=2, т=2, !=4, е=4, о=4]
Результат перебора Библии:
SortedSet---->[(=59, )=59, ё=89, ъ=458, "=2027, !=2185, ]=2466, [=2467, э=3240, 0=3256, 9=3374, ?=3383, 8=3548, 7=3765, 6=4096, 5=4638, -=5496, 4=5620, ф=5835, 3=7740, ==8492, _=8594, :=11065, щ=11862, 2=11868, ;=12348, ц=15517, 1=16575, ю=23439, ш=25191, .=27932, ж=29909, й=31287, ч=32969, х=35423, ь=50038, з=52460, б=60470, ы=61791, я=66468, г=72282, к=83675, п=83962, у=85409, ,=87279, м=105539, д=115654, р=130840, л=142902, в=162454, н=179485, с=181963, т=184448, а=238995, е=286468, и=291640, о=356734, =684227]
ArrayList---->[(=59, )=59, ё=89, ъ=458, "=2027, !=2185, ]=2466, [=2467, э=3240, 0=3256, 9=3374, ?=3383, 8=3548, 7=3765, 6=4096, 5=4638, -=5496, 4=5620, ф=5835, 3=7740, ==8492, _=8594, :=11065, щ=11862, 2=11868, ;=12348, ц=15517, 1=16575, ю=23439, ш=25191, .=27932, ж=29909, й=31287, ч=32969, х=35423, ь=50038, з=52460, б=60470, ы=61791, я=66468, г=72282, к=83675, п=83962, у=85409, ,=87279, м=105539, д=115654, р=130840, л=142902, в=162454, н=179485, с=181963, т=184448, а=238995, е=286468, и=291640, о=356734, =684227]