Created
August 17, 2015 06:05
-
-
Save masakid/acf18df0704791d28cf3 to your computer and use it in GitHub Desktop.
char型配列から2個以上出現する文字を抽出して降順に連結したStringを返却
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
public class CharCountTest { | |
/** | |
* char型配列から2個以上出現する文字を抽出して、 | |
* 降順に連結したStringを返却するメソッド | |
* | |
* @param cArr | |
* @return | |
*/ | |
protected static String exchangeCharArrToString(char[] cArr){ | |
Set<Character> cSet = new HashSet<>(); | |
List<Character> list = new ArrayList<>(); | |
for( char c : cArr){ | |
if(cSet.contains(c)){ | |
//すでに出現したことがあるものはListに登録 | |
list.add(c); | |
} else { | |
//初めて出現したものはSetに登録 | |
cSet.add(c); | |
} | |
} | |
Collections.sort(list); | |
Collections.reverse(list); | |
StringBuilder sb = new StringBuilder(); | |
for(Character c : list){ | |
sb.append(c); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment