Will be solving some Coding challenges in Java (based on codewars-kata's).
Might also create a handy cheat-sheet of frequenly used Java methods.
-
Convert String to Char array:
char[] ch = StringArray.toCharArray();
-
Convert a characet to Uppercase:
Character.toUpperCase(ch);
-
You can also append an empty string using += same as Python, but avoid it and try using Sringbuilder:
StringBuilder bld = new StringBuilder(); for (int i = 0; i < arrayOfStrings.length; ++i) { bld.append(arrayOfStrings[i]); } return bld.toString();
-
Sort an array:
Arrays.sort(args);
-
Convert string to lower-case:
string_value.toLowerCase();
-
Dictionaries in Java are hash-maps:
HashMap<Character, Integer> hm = new HashMap<>();
-
For-looping through HashMaps to get key/values:
import java.util.HashMap; import java.util.Map; for(final Map.Entry entry: counter.entrySet()){ // or whatever you want to do if((int)entry.getValue() >= 2) duplicates += 1; }
-
Use an int value for String:
int num = 5; String.valueOf(num);
-
Get range/length of an int:
int length = String.valueOf(num).length();
-
Convert character to its desired int using ASCII: (The difference between character and desired integer is always 48, 48 in ASCII represents character:'0')
char digit = '6' digit - '0' == 6;