This question already has answers here:
Is there a better way to combine two string sets in java?
(11 answers)
Union two ImmutableEnumSets with Guava
(1 answer)
Combine multiple Collections into a single logical Collection?
(5 answers)
Closed 2 years ago.
I'm using Guava's Immutable collections. Basically I have two helper functions that return ImmutableSets both of which contain data that are instances of inner classes that implement a common interface. However, I want to merge the two Immutable sets in order into a single ImmutableSet, in the actual function.
private static ImmutableSet<Fruit.seedless> helper1(args...) {...}
private static ImmutableSet<Fruit.seeded> helper2(args...) {...}
public ImmutableSet<Fruit> MainFunction() {...}
This is an example of how you can combine 2 or more ImmutableSet objects and create another ImmutableSet. This uses the Integer type for the parameterized type because I do not have access to your Fruit class.
Set<Integer> first = ImmutableSet.of(1);
Set<Integer> second = ImmutableSet.of(2);
Set<Integer> third = ImmutableSet.<Integer>builder()
.addAll(first)
.addAll(second)
.build();
Related
This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 4 years ago.
So I've made an ArrayList of Catalogue Entries, where CatalogueEntry is made up of an int, a string and a status (which has been previously defined). I want to order the elements of the list (aka the Catalogue Entries) according to their integer. Is there any way I can do this?
Yes there is, but you must tell the program what do you mean by saying that one CatalogueEntry is bigger then an other. There are multiple ways of doing this but one example is the Comparable interface
public class CatalogueEntry implements Comaparable<CatalogueEntry> {
private int yourInt;
//methods, variables here
#Override
public int compareTo(CatalogueEntry other){
if(this.yourInt == other.yourInt) return 0;
if(this.yourInt > other.yourInt) return 1;
return -1;
}
As you can see you must implement the compareTo method and return 0 if the other entry is equal to this, -1 is its bigger and 1 if it's smaller! This way the program will know how to sort these objects and you can just use the ArrayList's sort method!
Using Comparable and Comparator you can do this. This example will give you a clear idea of how to sort your list by a property of an object. In this example Student objects are sorted by their age.
example
This question already has answers here:
How do I assert an Iterable contains elements with a certain property?
(9 answers)
Closed 6 years ago.
I have a Java Object that contains another java object that is a set. For Example:
Public States {
private Set<City> cities = new HashSet(0);
// Setters and Getters Here
}
Public City {
private String dummyValue;
// Setters and Getters Here
}
I would like to test this object to ensure it contains values that I have modified in a method. For example I was trying:
Set<City> citySet = citySetInfoFromAnotherMethod;
assert citySet.iterator().next().equals("Dallas");
However I know this will not work because a Set does not have an order such as a list does.
So in conclusion, in what way can I test this object to see if values another method assigns to it appear after I do something to it.
You can use the excellent Hamcrest matchers library to achieve what you want (it ships built in with earlier versions of JUnit, but not with later ones).
You'll want to do something like:
Set<City> citySet = citySetInfoFromAnotherMethod;
assertThat(citySet, contains("Dallas"));
Of course that won't work because your set items are a City object, not a String, but you can use more Hamcrest matchers to achieve that too!
Set<City> citySet = citySetInfoFromAnotherMethod;
assertThat(citySet, contains(hasProperty("Name", eq("Dallas"))));
Alternatively (and IMHO better) approach would be to use a property matcher from Hamcrest to extract the City name.
This question already has answers here:
Java's final vs. C++'s const
(11 answers)
Closed 6 years ago.
I know that Java uses "final" to declare a constant and that c uses "const". Just wondering what the differences are between the two.
In java, making something final means that it can't be reasigned to another reference to another instance, but if it's a reference to a mutable class, the mutable values inside the class can still be modified.
For example, a final String is a constant because Strings are immutable in Java, but a final ArrayList means that you cannot assign it to another ArrayList, but you can still add and remove elements to that ArrayList
This question already has answers here:
How to initialize HashSet values by construction?
(24 answers)
Closed 7 years ago.
Is there a convenience method to initialize a Set equivalent to Collections.singleton, which returns a mutable Set instead of an immutable one?
Guava is defintely a good solution.
Alternatively, you can do:
Set<T> mySet = new HashSet<>(Arrays.asList(t1, t2, t3));
Guava's Sets includes:
public static <E> HashSet<E> newHashSet(E... elements)
which:
Creates a mutable HashSet instance containing the given elements in unspecified order.
You can call it with a single item as:
Sets.newHashSet(item);
This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 7 years ago.
Is There a java util method, or a short way to sort an Array of some type by an attribute of that type. I currently have an array of Choice type objects where each have a getText() method that returns the visual representation of the choice. I can make a long method that creates an array of the choices texts, sort them, get their ordered index and then order the choices by that index, but I surely think there is some kind of a shortcut.
Any Suggestions?
Collections.sort(list, new Comparator<Choice>(){
public int compare (Choise c1, Choice c2) {
return c1.getText().compareTo(c2.getText());
}
});
add check for null if necessary
you can move comparator to external class and use reflection to read custom field from any object. but this will make code less understandable