collection equality using hamcrest [duplicate] - java

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

Related

collection framework : operation on two collection object [duplicate]

This question already has answers here:
How to calculate the intersection of two sets? [duplicate]
(2 answers)
Closed 2 years ago.
There are two collections :-
c1 = which include all female employee
c2= all employee whose age greater than 40(age>40).
How can i find all female employee whose age greater than 40?
c1.stream().filter(c2::contains).collect(Collectors.toList());//Last step is optional. Only use it, if you want a list of it.
stream(): Create a stream out of the first collection. (Java 8+)
Then filter() by those that are in c2,too.
And then optionally make a list from it.

Best way to convert LinkedHashSet to ArrayList in core Java [duplicate]

This question already has answers here:
Most concise way to convert a Set<T> to a List<T>
(6 answers)
Closed 3 years ago.
This sounds like a really dumb question, but all Google results on the first pages either:
Use a 3rd party library
Use an explicit loop
Return a List<Object> instead of List<YourType>
Answer the reversed question of converting an ArrayList to Set
What's the best way to convert a LinkedHashSet to an ArrayList while avoiding all of the above?
A simple call to ArrayList's constructor should do the trick:
List<MyObject> myList = new ArrayList<>(myLinkedHashSet);
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("A");
set.add("B");
set.add("C");
List<String> list = new ArrayList<>(set);
System.out.println(list); // [A, B, C]

What are the <> for in an ArrayList, List or other types that use <>? [duplicate]

This question already has answers here:
What is the point of the diamond operator (<>) in Java?
(7 answers)
Closed 7 years ago.
I've Googled around but I can't find the use for the <> in ArrayList or List. I know how to use ArrayList but I like to know how things work as well. What are the <> for? I figure it's like an argument of sorts since it specifies the type but it's not an argument since it doesn't go in () so what do the <> mean?
to specify the generic type.
List<String> strings = new ArrayList<String>(); will restrict only strings to be added
When you say List strings = new ArrayList(); you are not specifying which kind of object the strings list accepts. So, strings.add(new Object()) is also possible. By specifying List<String> strings = new ArrayList<String>(); you restrict the arraylist to access only String elements.

What is a equivalent of HashSet Java in Swift? [duplicate]

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

Declaring ArrayLists: difference in Functionality? [duplicate]

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();

Categories