1

I need to add two lists of objects of the same type to a SET. I should make sure the order of the set is of natural order and there is no reparation in the SET.

I read answer of this question but it did not help.

For example my inputs and output should be as following

List 1

A A1 List1
B B1 List1
B B1 List1

List 2

C C1 List2
D D1 List2
A A1 List2

Output

List1
A A1
B B1
List2
C C1
D D1

As you can see the second B B1 does not exist in List 1 of the output because there are two B B1 on the list 1.

Also A A1 does not exist in the list 2 of output because it already exists in List 1 of the output.

Also the order of each list is kept. (List 1 is added first so List 1 is on top of the list, after that List 2 and the order of all of their elements is preserved.)

public class MyList{
    private String name;
    private String code;
    private String group;

    //setters and getters
}

List<MyList> list1 = new ArrayList<MyList>();
List<MyList> list2 = new ArrayList<MyList>();
....
Set<MyList> output = new TreeSet<MyList>();
output.addAll(list1);
output.addAll(list2);

Exception that is thrown

java.lang.ClassCastException: com.myproject.MyList cannot be cast to java.lang.Comparable
    at java.util.TreeMap.compare(TreeMap.java:1290)
    at java.util.TreeMap.put(TreeMap.java:538)
    at java.util.TreeSet.add(TreeSet.java:255)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
    at java.util.TreeSet.addAll(TreeSet.java:312)

To solve the issue I added Comparable to MyList class, but it changes the order of the SET. How to keep the natural order of the SET?

Community
  • 1
  • 1
Jack
  • 6,430
  • 27
  • 80
  • 151
  • 2
    What do you mean by natural order? – Codebender Oct 27 '15 at 04:38
  • @Codebender I mean the order that they were added to the set. Please have a look at my sample output list in the question. – Jack Oct 27 '15 at 04:40
  • 2
    Ok so the data structure you have provided above is not a list...not sure why you keep calling it a list. Provide the full implementation of the "list" that shows how you add things to it – smac89 Oct 27 '15 at 05:08
  • @Smac89 they are both ArrayLists, question is updated. – Jack Oct 27 '15 at 05:22

5 Answers5

4

If you want a Set that maintains insertion order, the class you're looking for is LinkedHashSet.

The implementation is a hashtable that also maintains a linked list of entries, which allows you to iterate over the entries in the order they were added.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • is that the best option in terms of performance? – Jack Oct 27 '15 at 05:22
  • @Jack: `LinkedHashSet` has amortized O(1) insertion and lookup, and iteration is O(N). Asymptotically you aren't going to get better than that. However, the constant factors are going to be slightly higher than `ArrayList` or `HashSet`, and the memory usage will be a handful of bytes more per entry. In short: it's highly unlikely that it will make your program slow. As always, when dealing with performance issues, *measure* rather than guessing. You're much more likely to find performance problems in your own code (e.g. in your `equals` and `hashCode`) than in standard library code. – Daniel Pryden Oct 27 '15 at 05:28
1

"The natural order" of the TreeSet is implementing by compareTo() in your MyList.class. Because TreeSet don't know how to order your objects, but its need to do this necessarily. If you need ordering by "that they were added to the set" - use LinkedHashSet.

0

Create equals and hashcode on name and code only. You can also make a compare method that maintains the original order.

Bob
  • 780
  • 9
  • 10
0

How to keep the natural order of the SET?

List is made for this , not set, Set is not ordered, but TreeSet is only when you define Comparator to sorted according to the specified comparator. otherwise TreeSet does not know in which order it should save the objects.

Take a List it will give you the required output.

Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29
0

If you need to use TreeSet then you must definitely use Compartor or Comparable to extend your class mylist a hashset will not guarantee order and any object which need to be ordered/sort in some way other than primitive/wrapper type need explicit way to tell JVM how to sort them otherwise JVM will not know on which property it has to sort in object hence the error java.lang.ClassCastException: com.myproject.MyList cannot be cast to java.lang.Comparable

user155806
  • 17
  • 3