Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: map with duplicates - sort by value (duplicates disappear, but I need them)

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question map with duplicates - sort by value (duplicates disappear, but I need them)

    Hi all,
    I got map with full duplicates (key and value are duplicated). I need to sort the map by value, it sorts, but duplicates disappear and I need to keep them. Please see my code below. Thx for any suggestion.

    package learn;
     
    import java.util.IdentityHashMap;
    import java.util.LinkedHashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.stream.Collectors;
     
    public class sortMapWithDuplicates {
     
        private static Map<Integer, String> sortByValue(Map<Integer, String> unsortMap, final boolean order)
        {
            List<Entry<Integer, String>> list = new LinkedList<>(unsortMap.entrySet());
            System.out.println("check 1 - list = " + list);
            list.sort((o1, o2) -> order ? o1.getValue().compareTo(o2.getValue()) == 0
                    ? o1.getKey().compareTo(o2.getKey())
                    : o1.getValue().compareTo(o2.getValue()) : o2.getValue().compareTo(o1.getValue()) == 0
                    ? o2.getKey().compareTo(o1.getKey())
                    : o2.getValue().compareTo(o1.getValue()));
            System.out.println("check 2 - list sorted before return = " + list);
            return list.stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a, b) -> b, LinkedHashMap::new));
        }
     
    	  public static void main(String[] args) {
    		  Map<Integer, String> map = new IdentityHashMap<Integer, String>();
    		  map.put(new Integer(1), "one");
    		  map.put(new Integer(1), "one");
    		  map.put(new Integer(2), "two");
    		  map.put(new Integer(3), "three");
    		  map.put(new Integer(4), "four");
    		  System.out.println(sortByValue(map,true));
    	  }
     
    }

    Console output:
    check 1 - list = [1=one, 3=three, 2=two, 1=one, 4=four]
    check 2 - list sorted before return = [4=four, 1=one, 1=one, 3=three, 2=two]
    {4=four, 1=one, 3=three, 2=two}
    As you can see, my two check steps show that duplicates are present, but they disappear on return.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: map with duplicates - sort by value (duplicates disappear, but I need them)

    Also posted at: https://www.java-forums.org/new-java...need-them.html
    and https://coderanch.com/forums/posts/reply/0/698312

    Please read: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Counting duplicates in a Stack
    By Jad_Asmar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 11th, 2014, 04:07 PM
  2. Removing duplicates from an array
    By Parm in forum Collections and Generics
    Replies: 1
    Last Post: October 13th, 2013, 03:30 AM
  3. Duplicates in Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 1
    Last Post: September 15th, 2011, 06:23 PM
  4. efficient way of checking duplicates
    By starmandell in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 2nd, 2011, 06:52 PM
  5. treemap Duplicates
    By debug in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2010, 11:52 AM

Tags for this Thread