This question already has answers here:
How to create array of unique object list in Swift
(11 answers)
Closed 8 years ago.
I have specific logic in Java that uses HashSet<String>. Set collection contains only unique items.
For example:
Set<String> mySets = new HashSet<String>();
mySets.add("a");
mySets.add("a");
mySets.add("b");
mySets.add("a");
I get: ["a","b"].
What is equivalent collection in Swift?
Thanks,
The Swift to Java's HashSet is Set.
Example:
var s = Set<Character>()
s.insert("a")
s.insert("a")
s.insert("b")
s.insert("a")
print("s: \(s)")
output:
s: ["b", "a"]
Official docs
Related
This question already has answers here:
How to convert a Java 8 Stream to an Array?
(9 answers)
Closed 5 years ago.
I have a List of 'Client' objects each one with a field "email".
I need something like:
List<String> listEmails = clients.stream().map(client->client.getEmail())
.collect(Collectors.toList());
...but returning directly a String[].
Is there a proper way to map a List<Client> to a String[] listEmails using Java 8 streams?
Sure :
String[] result = clients
.stream()
.map(client->client.getEmail())
.toArray(String[]::new)
This question already has answers here:
Collection to stream to a new collection
(4 answers)
Closed 7 years ago.
So, in Clojure, I can just say something like this...
(into [] (map some-function some-collection))
And I get a new vector of my transformed data.
Is there some simple equivalent of into for Java 8 streams? For example, I don't see a constructor on ArrayList that takes a stream, nor do I see some sort of helper function in java.util.Collections, nor the stream interface.
You can do it using Collectors:
someCollection.stream()
.map(someFunction)
.collect(Collectors.toList());
You can do other cool stuff with Collectors, as explained in its javadoc:
Map<Department, Integer> totalByDept =
employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
This question already has answers here:
Hamcrest compare collections
(7 answers)
Closed 8 years ago.
I want to assert equality of two collection using hamcrest.
I know there is contains matcher but I want it to ignore ordering.
{1,2} is equals to {2,1} for my need.
What is the right syntax?
Use containsInAnyOrder() :
List colors = Arrays.asList("red","green","blue");
assertThat(colors, containsInAnyOrder("green", "red", "blue")); // is true
Collection col1, col2;
Set set1 = new HashSet(col1);
set1.removeAll(col2);
assertTrue(set1.isEmpty());
Also you can create utils class like this and use it to find difference
This question already has answers here:
What is the point of the diamond operator (<>) in Java?
(7 answers)
Closed 8 years ago.
I'm curious, When declaring ArrayLists, What is the difference in doing this:
List<String> arrayList1= new ArrayList<String>();
and this:
List<String> arrayList2= new ArrayList<>();
i.e. not declaring the <String> twice?
the only difference is that the first form is compatible with earlier java versions than java 7.
And you don't need the <> either in latter versions. e.g
List<String> arrayList2= new ArrayList();
This question already has answers here:
How do I convert a Map to List in Java?
(13 answers)
Closed 9 years ago.
I would be pleasure if you can suggest.
Could you suggest how properlly I can retriev all values from hashmap.
In such simplee example:
Map<String, Values> someMap = ....;
List<Values> valuesFromMap = (List<Values>) someMap.values();
After that I've got that, Java couldn't cast to java.util.List
Thank you in advanced.
The values method returns a Collection, not a List. Use
Collection<Values> valuesFromMap = someMap.values();
someMap.values() returns Collection. If you need to convert into List than do that as follows:
List<Values> valuesFromMap = new ArrayList<>(someMap.values());