I want to add all lowercase alphabets to a Java HashSet. For this I use the following code snippet:
for(char c = 'a'; c <= 'z'; c++)
set.add(c);
Is there a better way to do this, like one in which I don't have to iterate over all the alphabets?
In Java 8 using IntStream, I think you should be able to get all lowercase Character values like this:
Set<Character> set = IntStream.rangeClosed(Character.MIN_VALUE, Character.MAX_VALUE)
.filter(Character::isLowerCase)
.mapToObj(i -> Character.valueOf((char) i))
.collect(Collectors.toSet());
This Set<Character> contains 1,402 Character objects.
If you don't want to box the char values as Character objects, and don't mind using a third-party library, you can do the following with Eclipse Collections:
CharSet charSet = IntInterval.zeroTo(Character.MAX_VALUE)
.asLazy()
.select(Character::isLowerCase)
.collectChar(i -> (char) i)
.toSet();
This CharSet contains 1,402 char values.
If you don't want ALL lowercase letters, then just change your range to the ones you are looking for. For example:
CharSet charSet = IntInterval.fromTo('a', 'z')
.asLazy()
.collectChar(i -> (char) i)
.toSet();
Note: I am a committer for Eclipse Collections
Related
I am trying to filter a string to only contain characters that I allow. For example let's say I allow letters [A-Z] (case insensitive) and underscores ('_').
So let's say I have the string:
"I am a happy_bear and I sk#8 because it's gr8"
I would want it to be filtered to:
"I am a happy_bear and I sk because its gr"
What I have tried so far:
//Define a method to check allowed charachters
private boolean checkAllowed(Character ch) {
return Character.isLetter(ch) || ch == '_';
}
// loop through string
for (int i = 0; i < someString.length(); i++) {
if (this.checkAllowed(someString.charAt(i)) {
// append to a list
}
}
How can I filter a string to only include characters that I allow? Is that achievable with a stream-like filtering? Can this be done easier without regex? (I am looking for a better way to do this)
Is that achievable with a stream-like filtering?
yes it is:
String s = "I am a happy_bear and I sk#8 because it's gr8";
String d = s.codePoints()
.filter(c -> Character.isLetter(c) || c == '_' || c == ' ')
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
System.out.println(d);
...
I am a happy_bear and I sk because its gr
codePoints() returns
an IntStream of Unicode code points from this sequence
the stream is then filtered and collected using StringBuilder.
a regex solution may be more concise and simple though, look at replaceAll() method.
your loop and "check method" is also fine, just use a StringBuilder to create a new string containing only the characters that "pass" the check.
If you're open to using a third-party library, the following will work with Eclipse Collections.
#Test
public void stringFilter()
{
String string = Strings.asChars("I am a happy_bear and I sk#8 because it's gr8")
.select(this::checkAllowed)
.toString();
Assert.assertEquals("I am a happy_bear and I sk because its gr", string);
}
private boolean checkAllowed(Character ch)
{
return Character.isLetter(ch) || ch == '_' || Character.isSpaceChar(ch);
}
Note: I am a committer for Eclipse Collections.
Is there anyway to convert a String to an ArrayList<Character> without using regex.
For example:
"abc".methodHere == ArrayList<Character>["a", "b", "c"]
This link converts a String to an ArrayList<String> and this link uses Array and not ArrayList
You could get the stream of characters and collect to a list:
List<Character> chars = s.chars()
.mapToObj(i -> (char) i)
.collect(Collectors.toList());
If you want an ArrayList specifically, you could collect to an ArrayList:
List<Character> chars = s.chars()
.mapToObj(i -> (char) i)
.collect(Collectors.toCollection(ArrayList::new));
Iterate through the characters in the string by index
ArrayList<Character> chars = new ArrayList<>();
for (int i = 0; i < str.length(); i++)
chars.add(str.charAt(i));
// do whatever with the chars list
Alternatively, you can do
ArrayList<Character> chars =
new ArrayList<>(Chars.asList(string.toCharArray()));
You need to import com.google.common.primitives.Chars; from Guava library.
A different answer from the ones already provided:
ArrayList<Character> list = (ArrayList<Character>)"abc".chars().mapToObj(i -> (char)i).collect(Collectors.toList());
I want to print a string that is converted from a collection of characters. But I want to eliminate the commas (,) and square brackets([]) from the printing string.
List<Character> word_to_show = new ArrayList<Character>();
for(char ch:jumbled_word.toCharArray()){
word_to_show.add(ch);
}
Collections.shuffle(word_to_show);
for (Object ch: word_to_show)
System.out.print((Character) ch );
System.out.println();
send_to_timer = word_to_show.toString();
I have come to this. It works but prints the string as, say for eg, [a, b, c]
You can use replace()
string.replace(",","").replace("[","").replace("]","")
Demo
If you have a real Collection containing characters, you can simply iterate that collection - and use a StringBuilder to append all those characters that you want to have in your final string; like:
StringBuilder validChars = new StringBuilder();
for (Character chr : yourCollection) {
if (chr != ' ' && chr != ',') {
validChars.append(chr);
}
}
First turning all characters into a string, then use replace() to create a new string with less characters seems a bit inefficient.
How I can convert a lowercase char to uppercase without using an if statement?. I.e. don't use code like this:
if(c > 'a' && c < 'z')
{
c = c-32;
}
You can use this:
char uppercase = Character.toUpperCase(c);
Use Character.toUpperCase(char):
Converts the character argument to uppercase using case mapping information from the UnicodeData file.
For example, Character.toUpperCase('a') returns 'A'.
So the full code you probably want is:
c = Character.toUpperCase(c);
If you are sure that your characters are ASCII alphabetic, then you can unset the bit that makes it lowercase, since the difference between the lowercase and uppercase latin chars is only one bit in the ASCII table.
You can simply do:
char upper = c & 0x5F;
You can use the ternary operator. For your case, try something like this:
c = (c >= 'a' && c <= 'z') ? c = c - 32 : c;
I have the following characters.
Ą¢¥ŚŠŞŤŹŽŻąľśšşťźžżÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
I need to convert to
AcYSSSTZZZalssstzzzAAAAAAACEEEEIIIIDNOOOOOOUUUUYTSaaaaaaaceeeeiiiionoooooouuuuyty
I am using Java 1.4.
Normalizer.decompose(text, true, 0).replaceAll(
"\p{InCombiningDiacriticalMarks}+", ""); only replaces characters with diacritics.
Characters like ¢¥ÆÐÞßæðøþ is not getting converted.
How can I do that, what is the efficient way to do the conversion in JDK 1.4.
Please help.
Regards,
Sridevi
Check out the ICU project, especially the icu4j part.
The Transliterator class will solve your problem.
Here is an example a Transliterator that converts any script to latin chars and removes any accents and non-ascii chars:
Transliterator accentsConverter = Transliterator.getInstance("Any-Latin; NFD; [:M:] Remove; NFC; [^\\p{ASCII}] Remove");
The Any-Latin part performs the conversion, NFD; [:M:] Remove; NFC removes the accents and [^\\p{ASCII}] Remove removes any non-ascii chars remaining.
You just call accentsConverter.transliterate(yourString) to get the results.
You can read more about how to build the transformation ID (the parameter of Transliterator.getInstance) in the ICU Transformations guide.
How can I do that, what is the efficient way to do the conversion in JDK 1.4.
The most efficient way is to use a lookup table implemented as either an array or a HashMap. But, of course, you need to populate the table.
Characters like ¢¥ÆÐÞßæðøþ is not getting converted.
Well none of those characters is really a Roman letter and can't be translated to a Roman letter ... without taking outrageous liberties with the semantics. For example:
¢ and ¥ are currency symbols,
Æ and æ are ligatures that in some languages represent two letters, and in others are a distinct letter,
ß is the german representation for a double-s.
I would do something like this;
UPDATED FOR Java 1.4 (removed generics)
public class StringConverter {
char[] source = new char[]{'Ą', '¢', '¥', 'Ś'}; // all your chars here...
char[] target = new char[]{'A', 'c', 'Y', 'S'}; // all your chars here...
//Build a map
HashMap map;
public StringConverter() {
map = new HashMap();
for (int i = 0; i < source.length; i++) {
map.put(new Character(source[i]), new Character(target[i]));
}
}
public String convert(String s) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] = map.get(chars[i]);
}
return new String(chars);
}
}