A TreeSet or TreeMap that allow duplicates

Zeeshan picture Zeeshan · Mar 7, 2014 · Viewed 29.5k times · Source

I need a Collection that sorts the element, but does not removes the duplicates.

I have gone for a TreeSet, since TreeSet actually adds the values to a backed TreeMap:

public boolean add(E e) {
    return m.put(e, PRESENT)==null;
}

And the TreeMap removes the duplicates using the Comparators compare logic

I have written a Comparator that returns 1 instead of 0 in case of equal elements. Hence in the case of equal elements the TreeSet with this Comparator will not overwrite the duplicate and will just sort it.

I have tested it for simple String objects, but I need a Set of Custom objects.

public static void main(String[] args)
{       
        List<String> strList = Arrays.asList( new String[]{"d","b","c","z","s","b","d","a"} );      
        Set<String> strSet = new TreeSet<String>(new StringComparator());       
        strSet.addAll(strList);     
        System.out.println(strSet); 
}

class StringComparator implements Comparator<String>
{
    @Override
    public int compare(String s1, String s2)
    {
        if(s1.compareTo(s2) == 0){
            return 1;
        }
        else{
            return s1.compareTo(s2);
        }
    }
}

Is this approach fine or is there a better way to achieve this?

EDIT

Actually I am having a ArrayList of the following class:

class Fund 
{
    String fundCode;
    BigDecimal fundValue;
    .....

    public boolean equals(Object obj) {
    // uses fundCode for equality
    }
}

I need all the fundCode with highest fundValue

Answer

Sohit Gore picture Sohit Gore · Feb 17, 2019

You can use a PriorityQueue.

PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>(); 

PriorityQueue(): Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.

This is a link to doc: https://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.html