Is it possible replace an element of java.util.Set by another element? Given below a simple example.
Set<String> set=new LinkedHashSet<String>() {{
add("A");
add("B");
add("C");
add("D");
}};
if(set.contains("C")) {
set.remove("C");
set.add("E");
}
for(String str:set) {
System.out.println(str);
}
This example first removes an element and adds another element afterwards. Doing so would add the element at the bottom of the Set.
It outputs after completion of the foreach loop as follows.
A
B
D
E
Is it somehow possible to just replace an existing element by another element retaining its original position so that it outputs like the following?
A
B
E
D