当前位置:   article > 正文

打印输出一个字符串中重复的字符_java输出一个字,要求输入相同的字

java输出一个字,要求输入相同的字

使用 Map

package org.feng.string;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by Feng on 2020/1/10 8:48
 * CurrentProject's name is java8
 * 打印输出一个字符串中重复的字符:
 * 字符串短的时候,时间较长、但是稳定。字符串长时,也是稳定的。
 * @author Feng
 */
public class StringDemo {
    public static void main(String[] args) {
        StringDemo demo = new StringDemo();
        // 生成字符串
        String item = demo.createStringByLength(10000);
        System.out.println(item);

        long mapBeginTime = System.currentTimeMillis();
        demo.printRepeatCharByMap(item);
        long mapEndTime = System.currentTimeMillis();
        System.out.println("花费时间:" + (mapEndTime - mapBeginTime));
    }

    /**
     * 输出重复字符:通过 Map
     * @param item 目标字符串
     */
    private void printRepeatCharByMap(String item){
        if(item == null || item.isEmpty()){
            return;
        }
        char[] chars = item.toCharArray();

        Map<Character, Integer> map = new HashMap<>(16);
        for (char aChar : chars) {
            map.put(aChar, map.getOrDefault(aChar, 0) + 1);
        }

        map.forEach((key, value) -> {
            if(value > 1){
                System.out.println(key);
            }
        });
    }


    /**
     * 按照输入的长度,生成指定长度的字符串
     * 只由 [a,z] 随机组成
     * @param length 字符串长度
     * @return 目标字符串
     */
    private String createStringByLength(int length){
        StringBuilder sb = new StringBuilder(length);

        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // a --> 97, z --> 122
            int num = random.nextInt(26) + 97;
            sb.append((char)num);
        }
        return sb.toString();
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

测试结果

syptghflfgdqhaulcbhmiskmzewedkqgvcnvmoffmpxbaahldmhfwvwzswrjgsnsimvfemhnsjgfobklybhigvjdiinrdjlifxmfdlsspdjgkvcahnffwwcszdbwlmoulrbumpuvwmfxewzmgnstqjivyguljstjpxarskarjcligabnowvqseffjqrnxlewywisincskgedhdriqynfojjnzugczmmookvwsgittjgcikxhuvboysaexansnwxuxcyzgxsrybmdvokszwezpfhszydcuqwsyjnimbztdhypyvhacahunhmayzzyoetruvbbuupwbwhanxkbxizqybkjusrubqesplatuhpuftdmnmwwglhsdfdkugjgfsznhexbwojwwspfrmehkxoeqasypcoicjexisuzezidqikzdmgjgcjuxvzbbuvmktpqismzbgespnlftsknopoecvusuzjqagihrdisoztcdmzrnzzflqcexwbgvomlczrckanuajzpbwdbqvkethbrowymvzprwqmjslggqdiyqrcfxybuookbofvrkrioxpljatqmlfughxztidyfsonkpskfzzapjrymmhokbhzoewznsqbrhwfqpxpfyjviikenkbxgbjvljgjixbvylxokwsqiywwfssvwqyqtppsbcdkmvqszxfgmpkxhybowzsmklabufsuxjjtpdfjgqpyxprkzigazpatuekhjqwzpmutnvbkebaosuxukzanowszwihicziadqacukneqeogurfznbqcflmqnhwdfqcblpetdwbrffegsidsqcegahvcajkoanqxrwkuebbpezzmwrvpldviqzdpwenmonsisaqblyfsndhjzdsvmtstugmbsquhvwepchxbostspbjrncycnrkqzdfuotzxvojgoosirtalxfaeqtrdvtbchygebarngimdzcjsmhodmyljuhgvyxzlfctnlhhnmzvsjnjskprgbvkpcwrsjamnvvfjyhjdgvuhtrzztpmaaypjoggafvtxhlwcadgomkxwbnyifappgeubppgpzpsbvhckrbrfwkonfhvwayrzqkkeqqgbqulolvmrwwbmmgmkwqesixgqpahxinvmqahvvimdvyahkjehtrbjckspckpjgkmyswpqwsapttrlbnmowqznkidolfzggfiyjcebulkvzdbovlhhmkddwtzqvgcwbeehfjlvpnrkagccercyclezubvhvrvcxljvxmaauqymnalwgbijzclowdgwkzelcjpzbbjogxwcqftexwflaagildkrbmocpmyykvjztbpecnqxzwloutdyfvpvofkmseosuiqfqdusgkhtlikbbdzywwjlqpwmuwlchlsrrrcmwvpbpklzodapszsrxifhsfukypxlmghnbqcvriwqckytlqnzvfyftxdwpocjwxhgrobehzdlighzcfypskcisscenagkjpcqtsdpboftdqugzpavizfnrekkgcgdthsyjyhjqjsmdbmfgwngwhaqzabawputcsykhurpcgumbtwhnezbsxhsgtouafjwkntmuzhsfwatoyunmrdwdatrmzximjwlbnxuughxrryrjfjslpbcwgxuvmqehfnbzdqlszszabuubpyjbctoefbtthnojmvbtbgcmjsdyntylilpmstipqbbnmyujlfjtufzvqbvugzcatloodvvoarxndaoqwvfhuopehcseoarqcldnrybjnbcfvqkuwqozbvctfsokhgxdelqlenroyjprxeccztjayjkbuawhuglnsysncehtytzznuxjjsrqyunbblicxslrqxpdkhodsdvmvevefytpnmbgsrvdikkbphkbioealtwicchwesvnthrdhsfjhdscvotxgpticjvtrdwsjrnezdtavniekfmpwfwbdhfggcilstwfcbuttpgofuxofamlbjyrbiwlojnibbqrdvltjyjjyhvucjchvyaguserqmtyhbjbtclcqqijwyfcnokwotxgawjwgqgqurtinkqzjnaijswjynyoraivpmhyxtcmgacooqfymntaeijydriixyyurppipklwoasqooamhqfaqxvuquwpmgomzvbdruutznfevfxldhmeghbiccotnjcjdycrwmlstbusimjmiaexynscxkorswcwpyeyccahelmzwyocmuwgssgftqgjrptrajvlkjorsnhoxagogfuovsjvxscloyycimwcgkamwdizdqutxfzqwgkwopzwxkoufqbbqrvqzwkqjbttzjsehlrygdlnsbnjkdiapbaqbepdgwwzuhpnytbatuvqubcygnnemlmwctskavurxrcsmrmmrwxpomeczurvknwdljjbmkjbylpriewrnhwklvmrxmvyrijihrexrgbrwaxhimikikemlxijfilmjnmnoegfrhbftkhmdqwqgixumzlfswziyqdyazjnofnobthpezokjlliuhzfwbneqihivosojqbijsfirjvigvzsemrldhnpniwlulrfhpmxqnmlqbqbjampxlenomabydttisqekrepyezaauodmiczcwzlrxzchdsgjixpdjyqtcwootacpelwpujdeckdbroshambvtkzkrytmrueofytecwskrinnairhjlsasoivhqwxiyazjqsvubaeeimpkpglzfqsnzhndwcawfeiodmidcvwbmtbipjhcuzeilyibtslgnawkonfgwxydzrqnmavopexmgvstjsyzoqczimzlkawpzbphfzhrgxbynggvwnyazudvnwkzwbnsikjrjezqhdmthixquaeeozyjordjnsklqfoohcnmdxnrytnoyuwzufcctizvrhbbnoeislbiimjagzanaedzcmjefvkcgiezfiwwmrfgbklmukxubcblvyoloarxldmiucldpifelavskusrjflipdtzrphzjfagsfzlkcnuaefgaozschingvmrqqwwzsizxcoxhzqioimwdezihsylhkafxggvsnaarxmabcicdtbpbypxaldqjlpluforjfypslbbolnqdadgjydzatadrcmolcckzxblwrzsbssncrwopqbfjoyloldoccfrxcqypybncncnmlizgdsnxzpwcrwhrwsmmelwdtaseopsvfaccbktwtojxnotiddkbesltkyohklbnuzkstpxlfddcjorjzxdnawznyljvzierrtdfycwqiraqgmqnpynqkmdgyhzbnhrovysyxqocvpvgyibculsrhecjverywtvkgngemplowrfkitmtrrfktsewacuhmowslnukapwywlkhsfempwzkpuvkeurlycoixwmjxnbxwehphdoeqcbdsprctgnwwgcpplywwrbuhvzekfujnsrgnwmmarkiknwdahvyyyabsvkvbxoqhzlcfhlcaivwupwfqsnrtgygvgjougnezpouaowkefbnwurfcicaednxdkznytqvinuyvjgevbcyfwrjrgcnikjwryazbcmrwmhucjbqrrinnckdglzlbyzuglxtgxascgnypfjuvdchglefiflejbyehjbizvdatzwqyzfvmpoeuzmpdhoxhdsgomtlcamvgcqpbycyxiyhrgspogcospjnyepktlrxswzkpkdlthkavludtyveavtiyhurbkyofnbgqrfjhctukvdjgvnjyeqtxpdlthmaiiaoysvktxgajrpzfcmgaofxmvymjdtwegivdpkrctcpksmagqmirnqgvpmsxusrsvbhzhlwfxvchprswnjorokkjqskrlpvftxplwfccrigbwusdhrhprzefrwsnfczcthzruxlygsysvrymtmqarnummddgwsbsuiarjwrnlgvqjtbqsfcofkhdwwimstznsiiptxobyiqvxotdbxntnwrvilkhhdsaveunvtchvkfdsineuiiuriosohhzqcaoainwkutozwbydtevrnxvhnungjbamaqhwxwozyberuokpacujgnrmrddfsdlhomsfukckcehhvbmqdrnlynfdaubcmisxfiqyekfqumdyswmcqbwaohkkqxajwzbodwacznajxbdzhqkyicselixtttgqbzajmcvxyivddqognfatlwprwekccuuuncrbwyksoqffuryhcwkxehuybjuijcjinknohhglplwowtvthrjizaweegjkhyqzcycmohpwmkpyxkgvtfzfcayhqyreieqjcnwmrvmxmnnjmtlrhvkebvhxgsweifcysixfatsakbcqkexcuemfqqhlzjslquivzthvknglmghphelxirbpguxeommsefpnznaedsjtgxzptazqrzcxhxggjcgnehoabpjklkpzzskwgmcdofjfuujodossizcbgaisnejghsaauwtpwztxpxdfvxiufnilouvvqruoclvsqmhxkzjyuwcdbigzxtkhkkoflfzlgyfkqwfpxiogqlnmjadtsvvutkpqkxfpeytrbegarkuldwnvkfrrnfobhdfnoqqbnkbyfqeubnyclljpwqqgouintwsdkkheibrdqtohgguoqgeapdoqlhuhckouohrhvoxqvwrbcffjdtcxpvhdicshtelkjndmjocxzruoqzazooileiygqmbdgmjrkqgtvusprjmuhztjtjjsuzxbgeavkjiiixduhtgqejzvdszslygcbdkeqseqmdnklxsjsntyhvqnhjreqjwqjfpgavgnirwhdaqchhavtyuoorxnqkwxwugsurclzqxranaqmdertqgbqnlbkygxktpgoulcidftrdixsuncgbpmmasjmnfcbhfyslwubkrfnqqscoufaduufznyeiwxepofrygtfedbpnxmzufcffvyhrkkpdqxlkawmaurtsyevippfgmpjrpmfxejlzupcfisjbluljkzsjxosubnmmtcimxtswwdcjpwadxnaszcfammudlfsuulezitqsflectrtqruvmaxwwmpcfmvbujmkytwufocczhbvyqwdntttzgheiwxmiinlwpqnecxpqdgakxeadnsiqwvynplhlovznlawmujssfycijgtgzxrvdyxldeahgpjqfkjvvesfpjchgrfoilfhlgamrvkwtrwtrihhhkmacpgbjrgcbicyydidiwmqiptladltliawysmkhditkexbnusicnrqdwhtidlcojgirbuxlckwslwwkykgkjponhmmbzjczdrpfutcgtkqtmbdhrikhuxpzsedrdqoikpwmfrstvuyhhferkxlhiojsccnibplcdyjnroqmoamcecbbywjdlanvfpzkcqvhfaielfmgaedbzmdvpotcfatjocetnhvpskpdrkhavxisgdegjovgavvpeexkgyxwbfikfaibqrwylmblpynhiohhfjfzlplbnoahvllqzyhcfbhnyrydlbllerdmxoqsshmkstslbhxbwiefyomybylrrrsmqpeqqrjbxazfbhbeiicggpfxogfyoptuzxadefzdqccjtqtpilldnziimhjuqdaqmqcythfankcsnlbzoxjgjlgrurfwibujffwqlppybhwfkbjgkxoqwbkkyaenheyzfkevjaoyfbaknohajzqixepgcoxuxfxoltyukouojvbwaxmroxhefjhbbfuwmxnnbkhmglklvckqgvegtqvacwnuwrmupqtezdrydevrfvzxrzzteukdfklidzluhpvxcnnvcegcksvqgyiqkpofvusuimshtbeeklbdqbuklrobwidfmvonschtkrnajwvhbfrtvsoocgarxdtyinkmmllhjvblrddkwhzdftbcxcmvwmqqynrkysadxqurnzzdopinbvazlkimbkltxfjufjpxdssigcvpxpgfuxqnurtbyiglxkrxjxldmvpzojrohqejtlmskftpqresiubkcjrxgvhsvtgufcpqguldrzofsfwkhrpyhweabaqfpzcsdijgndxubkerftddjajopwxwigibpphyrokqjhzbxqfwbcqdsbphqhbfvwvwjjcnkgsxbqheijddwaojcybxprdnjornkljurlbsidwhpdpovbqbcqtfhncgoeceukzrmvvhaxsibgzyfwbluxhuwxftqrhpuysmfiubhqofsfqsplkostjjblpuauoxbdsmvwyeozozoquzdowtxtpscelwyucstyqpqtfgwtcyqifyrfiimhhahdomnlbjvyqwjmetjsxaopyycmwyfbtnazozuokexyygcukgdjcuupfifzpbumtbhrswfqxazobofhffbxgtphtmautkqycoenkhgpfempkxbvsooyihojnlxvrddfkzqyldhmrineanbixkatnfzjhqnmzlwdmqylvkrcjlrlervmlwmnebkzbepzkdfuhsnurkfinuexinpoqcllnjrczkjyknittfdfkiryzyoplpyelglbqmncjakqrxglnhkfvhtaxehaxhqqygicymfjlnzrsumiwzamanurdpzqjbgzxxtzdwdipqwrsasmwtgwvfszjewciqazysbqqvrfpsjxoqwpngttpxxebrpcmomxpriuhsskvkpqbkztzsyzvpkgtdudyvrlcuhmbyfbqadagavliyrgdyyfidmesdqpjypeetenzntymqqyenwobwbrthyrghrgelqzzgrjumavpntdbrljqrnoccssnlyspcqgjxaoyolyzsjogfwhlzlnyqzngusvyyanrkgudsmkdvstmmnwvqvbhhnushzjgpdxvlidjgnrfwcybpnqgaaphbipydbwbajfvdarhlpvqpsowxanxffyeseklndxyeovtkqpjyrfkaninpddoedsxvbfwfkvfwvsfxdoaqzemkkhflzgidpbaipuacqrljrdkbvbqckpcfevskgxlwkvqmttagvfegjsjrcatdqhrfigrjfiyiyefuaohnirecrevuvdtywoeywuqrlnwdpyllufcxtvdxjmrslgynblamiofdgpwybngcxbrzgmiqkhiiipkjjhwqegqhkblgrerrfkzpmruyromcgtnfnujemcxgqgsnujqzkukzortxlleymrcprmqygxitxmoznwkvyevpucruuyntkrhyrwosuxzbtyunlmdmkpgleaqbgiubftxbwmqgfsgnqzddlyxftswevzukigvxwzmlvctlnifsbrlrkbeefrmvvoyuxhdgthcteyaraauivbsntljgsmbejdhixioyikyyocjspgbwumddxfatybsgegnqalpdbsqrmovgjafjzlalpectfncnbmnptwjatdzskaobkrkcnajjdgckjsigvlxepufbycymngxbmtpgmnqougsctbdfblcgiwwgsxdclesxrhoxipckqbsahomnikcmpnidbnwtcgifyxagcpnznwmuumwpzvwrnmbkhsrmyaobsrpyhavycxqhpkmixtcjojqvusczpqrnlhxgddzytgskieqxeaitqrvfhxnrzjzztlefnogdtqgbuvzsfsujgvagctufpcyubkzfmmhgtjclmahouburzjqchrpsglbsatxxmefzfvidmgvzjfyrxehljygnajntfaymvkxdtefuubxpvuqppzqdesaidgqzbdcdozcmbiqdteccbpwvqiexyickhxqmroefomvtaknfeiaqvxuscusjjzaoknictnjirrnlxbvrpdxohyylrwwnxclvavniyzjzszqgwzldgsmeuguqecyhgzpuksswweziurpcfizsiihkylovwfrthiornmjevvgbvzuomyutwnqocjhdrcolcbbhddvbvuagpmsvkdspkswwclfszrosvvojaqiojlsrttllmmfibeqvqwrttlukadeqnghrkxwhipenakhjaxohxazhutofgwvjtsylspniayhlsjiwvsbwdluypsafikddrfwwcilxragetfiruavsjrafikzaggqtnhggolptwvsgycrkwdjzxqhonlkpbvanfndqytxeqclirgjksagfffvflnemxlngqvwsxbtxidlvymaxrsrhleuftdpzwelnxqyvnefqwyfbfsyidcexjbwxopcsxanqmxamlhpdsqsupwdtuombevhfcvndwvvcvshsjsnvdsqrnjtvrouydcnmnwtdeqiwfdoojbxxkdvuhjrxkkltfqqtshaocnyrtewobifthgihbuhjgjxutqgbilhhffrmcfsprfbiixtaaneimzeelehfjjtdgygqylqvwmahbiuhsuovuczxvcaebxtdmkhsfftgxjmnqmuszxkddvitahgmbbooxmlukspizlcjpgjzjlybunjjcxvowwxcritlkvxmyoaixvgbyqidpjebdsodwivmiipkcafwfuvqtajywyoyyyngqbrymhfeycnkwvwlxqxmodqswchzispzpoqmnpkpbajccwluhypvcnqwgsulcecqstxhnlozxtnycfdawwiadpzpsgcjikoldfdmuyinxevjhaoifhawtpnpymwmxjkhcsgirpiyvbjeifsebhxjfmldxtkvrasazgvcdmhwvalkmtgjhybwyfqpcwcswvbqkzsjoqtnyfluykhfwxcnfivsdoaykyxrikhjjuxvkwkiyujomusoeanwfrzojjbteroxhgnsnmrdyukqszzxhmkmqymkfwvcbgyzinpzwkccsdjginveiutvomrlusylsshfquwgcfkopkxoeiaoffizyjhhkyriezchozobehxgefhqvalvmueeyfsuppdblrxrpjhdjkykajboaxryqjwfxucgajheexwkjtaoxcbzvfkunkoikhpbxcgpvxulbbaksxctgurmdhyfclfjcbfuskscrqpfobpgvsmpijmmuuefeekuwvhzbbzdfvrqcakhzxfupwsoediofshgnwcnvllxtubevqghvtvmyfhtrddufhrisblcyvujvrhnandfhypmdupsaqujlmyladnjgeuisajfihgstpxropohpswlcbhupodckbrlhvochfelavslquqrvwosgqukbinuughaqnwlhopbtigbosvdbgpvxxxvhdvfjtjayrdqgpzlrtqkmqtdfisshesicelthipqatrlxwuqsyymoojmqwshngfqqblbiewnwavokoguoybkzeraolgzsfybzoixzvwywkbxjuxeutzjzywfaitjsumiscjpznvghenrygjlzdnxuuufnyodjjzlaqlbidkryhuezyvyfcswpnkqbvhakyoyjxmlejlutanyqqmyszfqrbotgonqgatocwnpavpslephyaqffznreenuxcqizjmmgzweoabnztnhpjkojxpigbiylhxqvngwdjqucgzfrvyjhuwggdwdquxqiagbjaxmlhvavzwbvljlvvkyjvvagkcxlikonvwajjsnsknnkcplnlvjjepjhbxsoqyfdiunepmyjliohvjnrthfcuwawpqdsynmifvcceifmyfludzlbwnfepeskqffsmkugjwfayexwbredwukhsbaedaxwiwxugqzkiekzviwrplwfpuyjptjnzqpfitwqnicdapivhpsxnzarzetezflzodcuyyzbvvzzqbvwcinqufnceehwbbizloiohegcgoquiezvmtcqgriixzyzabrqulcqzcojpiveswgljwfygmcrncullthcwbmoemsjlalmfuvufgzllsoeqcjjkjvbcyyfvbvtvbnlhrofekgihymwmfucikwkktexwhajfaznklinydukcibiorbooklipzmwzrxlhfrmcfieftcpatg
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
花费时间:73

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/240427
推荐阅读
相关标签
  

闽ICP备14008679号