This question already has answers here:
Java collections convert a string to a list of characters
(11 answers)
Closed 7 years ago.
I can propose few answers myself, but they are very far from being elegant.
Prove me that Java is not that hopeless. Thanks.
In Java 8+, you might use a forEach and something like
String str = "Hello";
Set<Character> set = new LinkedHashSet<>();
str.chars().forEach(e -> set.add((char) e));
System.out.println(set);
which outputs
[H, e, l, o]
Related
This question already has answers here:
Is there a Java equivalent to Python's Easy String Splicing?
(8 answers)
Closed 2 years ago.
I am trying to figure out how to return a selected amount of characters in my java string.
In Python, it is just like
randomVar = "hello"
print(randomVar[1:])
and the output would be "ello".
How do I do the same thing in java?
Thank you!
This can be accomplished with substring()
String a = "hello";
System.out.println(a.substring(1, a.length());
This question already has answers here:
Converting Array to List
(9 answers)
Retrieving a List from a java.util.stream.Stream in Java 8
(15 answers)
Closed 3 years ago.
still very new to Java, my apologies if this has appeared before.
Basically here is the original code
public static MarketType[] convert(final String[] values) {
return ofNullable(values).map(v -> Stream.of(values))
.orElse(Stream.empty())
.map(v -> getMarketType(v))
.toArray(MarketType[]::new);
}
Since other functions changed, I really need the return type to be List<MarketType> instead of MarketType[], but is there any way that can achieve this with the minimal amount of modification for the original code?
I have been trying to put different things in the toArray function but nothing really worked.
Any help appreciated!
This question already has answers here:
A quick and easy way to join array elements with a separator (the opposite of split) in Java [duplicate]
(15 answers)
Best way to convert an ArrayList to a string
(27 answers)
What's the simplest way to print a Java array?
(37 answers)
Java function for arrays like PHP's join()?
(24 answers)
Closed 3 years ago.
I have an ArrayList<String> that is added to periodically. What I want to do is to cast the entire ArrayList to a String without doing a loop. Can anybody tell me is it possible without using loop?
Edited:
So we found some solutions, Like
list.stream().collect(Collectors.joining());
Or
String result = String.join(",", list);
or some others as well. Now Just for getting knowledge I put a question which one is the most optimal way for compiler?
You could make a stream out of your list and collect it using joining collector :
list.stream().collect(Collectors.joining());
You can pass the separator to Collectors::joining method for example :
list.stream().collect(Collectors.joining("-"));
Or you can use String::join method :
String result = String.join(",", list);
where , is the separator.
I Believe you can do
String.join(", ", list);
Hope this helps :)
Given some ArrayList<String> s, all you need to use to get a String representation is s.toString().
The format is the following
[element1, element2, element3]
This question already has answers here:
Java 8 List<V> into Map<K, V>
(23 answers)
Closed 5 years ago.
I have a stream<A>, where
class A {
String category();
// ...
}
I would like to get a map<String, list<A>>, where the original stream is partitioned into sublists based on the value of category(). It is pretty trivial to have it implemented using a for loop, but is it possible to get a more elegant solution harnessing java streams?
EXAMPLE:
Given {[a, xyz], [a, zyx], [b, abc]}, I would like to get a map:
a -> {[a, xyz], [a, zyx]}
b -> {[b, abc]}
Use the groupingBy collector.
stream.collect(Collectors.groupingBy(A::category));
This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 8 years ago.
I have a simply doubt about syntax in Java related with Strings.
Could some one tell me what is the difference between the strings in the following code?
String[] firstS = {"word1","word2"};
String secondS[] = {"word1","word2"};
I can not realize what is the difference because doing some for loop both String have the same output.
Thank you so much.
There is no difference, but the first is often considered better style.