J. Doe
J. Doe

Reputation: 45

Finding first, second, third minimum in hash map

I am trying to mind the minimum value in hash map and use its key in another method. I also want to do this repeatedly and exclude the previously found minimum. Thank you!

    Entry<Integer, Integer> min = null;
    for (Entry<Integer, Integer> entry : graph.myMap.entrySet()) {
        if (min == null || min.getValue() > entry.getValue()) {
            min = entry;
            int minV = min.getKey();
            deleteVertex(graph, minV);
        }
    }

Upvotes: 1

Views: 611

Answers (3)

priyanka kumari
priyanka kumari

Reputation: 1

To find first, second, third minimum key in Hasmap based on key as Integer.

Map<Integer, String> map1 = new HashMap<>();
map1.put(100, "abc");
        map1.put(30, "xyz");
        map1.put(55, "ab");
        map1.put(12, "we");
        map1.put(20, "vibe");
        map1.put(29, "java");
        map1.put(10,"spring");
        Integer first = map1.keySet().stream().sorted().findFirst().get();
    System.out.println("first:" +first);
    
    Integer second = map1.keySet().stream().sorted().skip(1).findFirst().get();
    System.out.println("Second:" +second);

// Using TreeMap
        TreeMap <Integer, String> treeMap = new TreeMap<>();
            treeMap.putAll(map1);
            Integer i = treeMap.entrySet().stream().findFirst().get().getKey();
            Integer i2 = 
              treeMap.entrySet().stream().skip(1).findFirst().get().getKey();
              System.out.println(i);
              System.out.println(i2);

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 338266

tl;dr

Map < Integer, Integer > myMap = Map.of ( 1 , 111 , 4 , 444 , 2 , 222 , 3 , 333 , 5 , 555 );
myMap.keySet().stream().sorted().limit( 3 ).map( key -> myMap.get( key ) ).forEach( System.out :: println ) ;

value: 111

value: 222

value: 333

Details

Focus on the keys first, filtering and sorting them.

Set< Integer > allKeys = myMap.keySet() ;
List< Integer > sortedKeys = new ArrayList<>( allKeys ) ;
Collections.sort( sortedKeys ) ;

Loop the first three keys, fetching the matching value from your map.

for ( int index = 0 ; index < 3 ; index++ )
{
    Integer key = sortedKeys.get ( index );
    Integer value = myMap.get ( key );
    System.out.println ( "value: " + value );
}

See this code run live at IdeOne.com.

value: 111

value: 222

value: 333


Or truncate the list to the first three. You can call List::sublist. Things to know:

  • The new list is actually a view over a subset of original list. So the sub-list and original are tied together. To get a new separate list, we must feed the sub-list to a new list.
  • The numbers passes to subList are annoying zero-based counting index numbers.
  • The indexes are Half-Open, beginning is inclusive while the ending is exclusive.

Code.

List< Integer > targetKeys = new ArrayList< Integer >( sortedKeys.subList( 0 , 3 ) ) ;

Now loop the entire list of three elements.

for ( Integer targetKey : targetKeys )
{
    Integer value = myMap.get ( targetKey );
    System.out.println ( "value: " + value );
}

See this code run live at IdeOne.com.

value: 111

value: 222

value: 333


Or, get fancy with a one-liner using Java Streams.

Map < Integer, Integer > myMap = Map.of ( 1 , 111 , 4 , 444 , 2 , 222 , 3 , 333 , 5 , 555 );

myMap
.keySet ()
.stream ()
.sorted ()
.limit ( 3 )
.map ( key -> myMap.get ( key ) )
.forEach ( System.out :: println )
;

See this code run live at IdeOne.com.

value: 111

value: 222

value: 333

Upvotes: 1

Ludov Dmitrii
Ludov Dmitrii

Reputation: 572

Not quite understood second part about repeat, anyway it may help:

 public static void main(String[] args) {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1,10);
        map.put(2,100);
        map.put(3,20);
        map.put(4,990);

        Map.Entry<Integer, Integer> integerIntegerEntry = map.entrySet()
                .stream().min(Comparator.comparing(Map.Entry::getKey))
                .orElseThrow(() ->new RuntimeException("Oops!"));
        System.out.println("min val is :" + integerIntegerEntry.getValue());
        anotherMethod(integerIntegerEntry.getKey());
    }

Upvotes: 0

Related Questions