0

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
11
  • 5
    You need to use a list instead. Order of elements in a set is undefined. Commented Jul 30, 2014 at 13:23
  • Set is a Unordered Collection. Commented Jul 30, 2014 at 13:24
  • You could use a TreeSet to retain some kind of order. Commented Jul 30, 2014 at 13:26
  • @gtgaxiola Exactly :) I mean that. Commented Jul 30, 2014 at 13:27
  • 1
    @sᴜʀᴇsʜᴀᴛᴛᴀ I know you know...sometimes we all get Layer8 problems except Jon Skeet... Commented Jul 30, 2014 at 13:30

3 Answers 3

1

No. LinkedHashSet.add is inherited from HashSet.add. Using it on a LinkedHashSet will add the element at the end of the underlying linked list.

Sign up to request clarification or add additional context in comments.

Comments

1

As all the answers above you cannot do that with Set. You can achieve this by adding the set to a list, then do operation and then back to set. But that is very expensive. Alternatively If you are particular about this kind of implementation you can use Apache Commons ListOrderedSet. Please note that as far as I know this doesn't support generics. So either you'll have to put extra care on type safety or else you could do explicit casting. Example

import java.util.Set;

import org.apache.commons.collections.set.ListOrderedSet;

public class Sample {

    public static void main(String[] args) {
        @SuppressWarnings("unchecked")
        Set<String> set = new ListOrderedSet();
        set.add("A");
        set.add("B");
        set.add("C");
        set.add("D");

        System.out.println(set); // Prints [A, B, C, D]
        int indexToRemove = ((ListOrderedSet) set).indexOf("C");
        if (indexToRemove != -1) {
            set.remove("C");
            ((ListOrderedSet) set).add(indexToRemove, "E");
        }
        System.out.println(set); // Prints [A, B, E, D]
    }
}

Comments

0
public static void main(String[] args) {
    Set<String> set = new LinkedHashSet<String>();
    set.add("A");
    set.add("B");
    set.add("C");
    set.add("D");
    System.out.println(set);

List<String> list = new ArrayList<String>();
list.addAll(0, set);
System.out.println(list);

if(list.contains("C")){
       int i = list.indexOf("C");
       list.add(i, "E");
       list.remove("C");
}

set.removeAll(set);
//System.out.println(list);
//System.out.println(set);
set.addAll(list);
System.out.println(set);
}

with this solution you have to use unwanted List :(

output :[A,B,E,D]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.