
Подсчёт символов в тексте на языке 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]
7 апреля 2021 / 13:39
Online medicine tablets shopping http://pharmcanadayou.com/ canada meds
9 апреля 2021 / 12:35
[url=https://thesisacloud.com/]thesisacloud.com[/url] write thesis thesisacloud.com thesis paper
9 апреля 2021 / 19:38
Canadian pharmacies top best interpharmnew.com canadian rx pharmacy online
9 апреля 2021 / 20:29
I’m not that muϲh oof a internet readеr to Ƅe honest but your bⅼogs really
nice, ҝeep it up! I'll go aheаd ɑnd bоokmark your dating site to come back later on.
Many thanks
11 апреля 2021 / 02:01
do doctors prescribe viagra to 20 year olds
prednisone 20 mg
where do you buy viagra uk DIT pruMn
11 апреля 2021 / 11:33
buy diltiazem
cost of prednisone treatment for dogs
paypal payment for generic viagra DIT pruMn
11 апреля 2021 / 15:04
free single chat
[url="http://datingfreetns.com/?"]single free dating sites without registering [/url]
12 апреля 2021 / 17:26
If somе one wishes expert view regrding blogging aftr that i propose
him/her to pay a visit thi webpage, Keeр up the pleasant jօb.
My blog pst ...payday loans lakewood co
14 апреля 2021 / 17:36
free local personals
[url="http://stfreeonlinedating.com/?"]single women online [/url]