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);
Related
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();
This question already has answers here:
UnsupportedOperationException when trying to remove from the list returned by Array.asList
(6 answers)
Closed 3 years ago.
I have next code:
List<String> str=Arrays.asList("cat","tiger","dog","mouse");
str.add("horse");
It compiles, but at runtime I have UnsupportedOperationException. Why does it happen?
Arrays.asList(String...) creates an unmodifiable array. Wrap it with another ArrayList like so:
List<String> str = new ArrayList<>(Arrays.asList("cat","tiger","dog","mouse"));
str.add("horse");
this called backed list .
backed list it's created when you convert an array to list but keep in your mind the element in the array is linked with the element in the list so you cant add or delete any thing and you are using varargs its similar to array ,
Arrays.asList() returns a list that is fixed-size and backed by the array you pass so you can't add or remove elements because that would mean changing the array as well. (note that if you have a look at the source you'll find that Arrays.asList() will return an instance of java.util.Arrays.ArrayList which you should not confuse with java.util.ArrayList which you probably already know).
Instead you'd need to create another list, e.g via calling new ArrayList<String>( Arrays.asList(...)) which effectively makes a copy of the passed list.
A Java 8+ way might be this:
List<String> str = Stream.of( "cat","tiger","dog","mouse" ).collect( Collectors.toList() );
This question already has answers here:
Why doesn't java.util.HashSet have a get(Object o) method?
(11 answers)
Closed 4 years ago.
If there any way we can call the method of an object inside a Hashset which in itself is a value in a Hashmap without making an iterator or a .forEach(lambda) or in any way going through each object sequentially?
Consider we have a Hashmap like this. Map<Boolean,Set<Place>> selectedMap
Consider Place extends JComponent and has a boolean value representing if the user had selected the object with the mouse, the map contains a set with all selected and all unselected objects.
If we for example want to call the remove-method of all selected objects, is there a way of doing that in the manner outlined above?
If you want to call a method for all the elements in a Set, you must iterate over the elements of that Set.
That said, instead of an explicit loop, in Java 8 you can use the forEach method:
selectedMap.get(true).forEach(Place::remove);
or
selectedMap.get(true).forEach(place -> place.remove(...));
in case the remove() method requires some arguments.
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:
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