TreeSet or TreeMap that allow duplication

I need a Collection that sorts an element but does not remove duplicates.

I went for TreeSet .

Since TreeSet actually adds values ​​to the fallback TreeMap :

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

And TreeMap removes duplicates using Comparators compare logic

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

I 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 perfect or is there a better way to achieve this?

EDIT

Actually, I have an ArrayList of the following class:

 class Fund { String fundCode; BigDecimal fundValue; ..... public boolean equals(Object obj) { // uses fundCode for equality } } 

I need all fundCode with highest fundValue

+2
source share
5 answers

I need an entire fundCode with the highest fundValue

If this is the only reason you want to sort, I would recommend not sorting at all. Sorting occurs mainly with complexity O (n log (n)). The maximum search has only O (n) complexity and is implemented in a simple iteration through your list:

 List<Fund> maxFunds = new ArrayList<Fund>(); int max = 0; for (Fund fund : funds) { if (fund.getFundValue() > max) { maxFunds.clear(); max = fund.getFundValue(); } if (fund.getFundValue() == max) { maxFunds.add(fund); } } 

You can avoid this code using a third-level library like Guava . See: How to get the max () element from a list in Guava

+4
source

you can sort the list using Collections.sort .

considering your Fund :

 List<Fund> sortMe = new ArrayList(...); Collections.sort(sortMe, new Comparator<Fund>() { @Override public int compare(Fund left, Fund right) { return left.fundValue.compareTo(right.fundValue); } }); // sortMe is now sorted 
+3
source

In the case of a TreeSet, either Comparator or Comparable is used to compare and store objects. Equality is not called and therefore does not recognize duplicate

0
source

Instead of TreeSet, we can use List and implement the Comparable interface.

 public class Fund implements Comparable<Fund> { String fundCode; int fundValue; public Fund(String fundCode, int fundValue) { super(); this.fundCode = fundCode; this.fundValue = fundValue; } public String getFundCode() { return fundCode; } public void setFundCode(String fundCode) { this.fundCode = fundCode; } public int getFundValue() { return fundValue; } public void setFundValue(int fundValue) { this.fundValue = fundValue; } public int compareTo(Fund compareFund) { int compare = ((Fund) compareFund).getFundValue(); return compare - this.fundValue; } public static void main(String args[]){ List<Fund> funds = new ArrayList<Fund>(); Fund fund1 = new Fund("a",100); Fund fund2 = new Fund("b",20); Fund fund3 = new Fund("c",70); Fund fund4 = new Fund("a",100); funds.add(fund1); funds.add(fund2); funds.add(fund3); funds.add(fund4); Collections.sort(funds); for(Fund fund : funds){ System.out.println("Fund code: " + fund.getFundCode() + " Fund value : " + fund.getFundValue()); } } } 
0
source

Add items to the arraylist, and then sort the items using the Collections.sort utility. then do comparable ones and write your own compareTo method according to your key.

Do not delete duplicates, you can also sort also:

 List<Integer> list = new ArrayList<>(); Collections.sort(list,new Comparator<Integer>() { @Override public int compare(List left, List right) { **your logic** return ''; } } ) ; 
0
source

Source: https://habr.com/ru/post/977523/


All Articles