How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial
How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan
Artikel coding,
Artikel core java,
Artikel java collection tutorial,
Artikel programming, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.
Judul : How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial
link : How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial
//Sorting Map yesteryear keys inwards Java using Google Collections (Guava)
//Main exercise goodness is you lot tin specify whatsoever ordering similar natural or toString or arbitrary
Map<String, Integer> sortingUsingGuava = Maps.newTreeMap(Ordering.natural());
sortingUsingGuava.putAll(olympic2012);
System.out.println("Example to variety Map inwards Java using Guava : " + sortingUsingGuava);
}
/*
* Paramterized method to variety Map e.g. HashMap or Hashtable inwards Java
* throw NullPointerException if Map contains zilch key
*/
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByKeys(Map<K,V> map){
List<K> keys = new LinkedList<K>(map.keySet());
Collections.sort(keys);
//LinkedHashMap volition proceed the keys inwards the gild they are inserted
//which is currently sorted on natural ordering
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(K key: keys){
sortedMap.put(key, map.get(key));
}
return sortedMap;
}
/*
* Java method to variety Map inwards Java yesteryear value e.g. HashMap or Hashtable
* throw NullPointerException if Map contains zilch values
* It every bit good variety values fifty-fifty if they are duplicates
*/
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByValues(Map<K,V> map){
List<Map.Entry<K,V>> entries = new LinkedList<Map.Entry<K,V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
@Override
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
//LinkedHashMap volition proceed the keys inwards the gild they are inserted
//which is currently sorted on natural ordering
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(Map.Entry<K,V> entry: entries){
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
}
Output
Unsorted Map inwards Java : {USA=1, England=3, Russia=4, China=2}
Sorted Map inwards Java yesteryear key: {China=2, England=3, Russia=4, USA=1}
Sorted Map inwards Java yesteryear values: {USA=1, China=2, England=3, Russia=4}
Sorted Map inwards Java yesteryear cardinal using TreeMap : {China=2, England=3, Russia=4, USA=1}
Example to variety Map inwards Java using Guava : {China=2, England=3, Russia=4, USA=1}
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt synchronized too concurrent collection inwards Java
Judul : How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial
link : How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial
How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial
Sorting HashMap inwards Java is non every bit like shooting fish in a barrel every bit it sounds because unfortunately Java API doesn't furnish whatsoever utility method to variety HashMap based on keys too values. Sorting HashMap is non similar sorting ArrayList or sorting Arrays inwards Java. If you lot are wondering why nosotros tin non role Collections.sort() method than for your information it entirely accepts List<E>, which leaves us to write our ain utility methods to sort Map inwards Java. This is truthful for all types of Map similar Hashtable, HashMap, too LinkedHashMap. TreeMap is an already a sorted Map too thence nosotros don't demand to variety it again. Why nosotros demand to variety HashMap inwards Java, Why can't nosotros role TreeMap inwards house of HashMap is the interrogation appears inwards almost Java programmer's hear when they asked to variety HashMap inwards Java. Well, TreeMap is agency slower than HashMap because it runs sorting functioning amongst each insertion, update too removal too sometimes you lot don't actually demand an all fourth dimension sorted Map, What you lot demand is an mightiness to variety whatsoever Map implementation based upon its cardinal too value. In the terminal duo of articles, nosotros receive got seen How to loop or traverse Map inwards Java too inwards this Java tutorial nosotros volition run into a duo of ways to variety HashMap inwards Java.
Sorting Map inwards Java - By Key
As I said Map or HashMap inwards Java tin hold upwardly sorted either on keys or values. Sorting Map on keys is rather like shooting fish in a barrel than sorting on values because Map allows duplicate values exactly doesn't allow duplicates keys. You tin variety Map, hold upwardly it HashMap or Hashtable yesteryear copying keys into List than sorting List yesteryear using Collections.sort() method, hither you lot tin role either Comparator or Comparable based upon whether you lot desire to variety on a custom gild or natural order.
Once List of keys is sorted, nosotros tin exercise to a greater extent than or less other Map, specially LinkedHashMap to insert keys inwards sorted order. LinkedHashMap volition hold the gild on which keys are inserted, the final result is a sorted Map based on keys. This is shown inwards the next illustration yesteryear writing a generic parameterized method to variety Map based on keys. You tin every bit good variety Map inwards Java yesteryear using TreeMap too Google Collection API (Guava). The wages of using Guava is that you lot instruct to a greater extent than or less flexibility on specifying ordering.
Once List of keys is sorted, nosotros tin exercise to a greater extent than or less other Map, specially LinkedHashMap to insert keys inwards sorted order. LinkedHashMap volition hold the gild on which keys are inserted, the final result is a sorted Map based on keys. This is shown inwards the next illustration yesteryear writing a generic parameterized method to variety Map based on keys. You tin every bit good variety Map inwards Java yesteryear using TreeMap too Google Collection API (Guava). The wages of using Guava is that you lot instruct to a greater extent than or less flexibility on specifying ordering.
Sorting Map inwards Java - By Value
Sorting Map inwards Java e.g. HashMap or Hashtable based upon values is to a greater extent than hard than sorting Map based on keys because Map allows duplicate values too You every bit good instruct a challenge to grip zilch values.
package test;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
*
* Java programme to demonstrate how to variety Map inwards Java on cardinal too values.
* Map tin hold upwardly variety on keys or values.
*
* @author Javin Paul
*/
public class MapSortingExample {
public static void main(String args[]) {
//creating Hashtable for sorting
Map<String, Integer> olympic2012 = new HashMap<String, Integer>();
olympic2012.put("England", 3);
olympic2012.put("USA", 1);
olympic2012.put("China", 2);
olympic2012.put("Russia", 4);
//olympic2012.put("Australia", 4); //adding duplicate value
//printing hashtable without sorting
System.out.println("Unsorted Map inwards Java : " + olympic2012);
//sorting Map e.g. HashMap, Hashtable yesteryear keys inwards Java
Map<String, Integer> sorted = sortByKeys(olympic2012);
System.out.println("Sorted Map inwards Java yesteryear key: " + sorted);
//sorting Map similar Hashtable too HashMap yesteryear values inwards Java
sorted = sortByValues(olympic2012);
System.out.println("Sorted Map inwards Java yesteryear values: " + sorted);
//Sorting Map inwards Java yesteryear keys using TreeMap
Map<String, Integer> sortedMapByKeys = new TreeMap<String,Integer>();
sortedMapByKeys.putAll(olympic2012);
System.out.println("Sorted Map inwards Java yesteryear cardinal using TreeMap : " + sortedMapByKeys);
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
*
* Java programme to demonstrate how to variety Map inwards Java on cardinal too values.
* Map tin hold upwardly variety on keys or values.
*
* @author Javin Paul
*/
public class MapSortingExample {
public static void main(String args[]) {
//creating Hashtable for sorting
Map<String, Integer> olympic2012 = new HashMap<String, Integer>();
olympic2012.put("England", 3);
olympic2012.put("USA", 1);
olympic2012.put("China", 2);
olympic2012.put("Russia", 4);
//olympic2012.put("Australia", 4); //adding duplicate value
//printing hashtable without sorting
System.out.println("Unsorted Map inwards Java : " + olympic2012);
//sorting Map e.g. HashMap, Hashtable yesteryear keys inwards Java
Map<String, Integer> sorted = sortByKeys(olympic2012);
System.out.println("Sorted Map inwards Java yesteryear key: " + sorted);
//sorting Map similar Hashtable too HashMap yesteryear values inwards Java
sorted = sortByValues(olympic2012);
System.out.println("Sorted Map inwards Java yesteryear values: " + sorted);
//Sorting Map inwards Java yesteryear keys using TreeMap
Map<String, Integer> sortedMapByKeys = new TreeMap<String,Integer>();
sortedMapByKeys.putAll(olympic2012);
System.out.println("Sorted Map inwards Java yesteryear cardinal using TreeMap : " + sortedMapByKeys);
//Sorting Map yesteryear keys inwards Java using Google Collections (Guava)
//Main exercise goodness is you lot tin specify whatsoever ordering similar natural or toString or arbitrary
Map<String, Integer> sortingUsingGuava = Maps.newTreeMap(Ordering.natural());
sortingUsingGuava.putAll(olympic2012);
System.out.println("Example to variety Map inwards Java using Guava : " + sortingUsingGuava);
}
/*
* Paramterized method to variety Map e.g. HashMap or Hashtable inwards Java
* throw NullPointerException if Map contains zilch key
*/
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByKeys(Map<K,V> map){
List<K> keys = new LinkedList<K>(map.keySet());
Collections.sort(keys);
//LinkedHashMap volition proceed the keys inwards the gild they are inserted
//which is currently sorted on natural ordering
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(K key: keys){
sortedMap.put(key, map.get(key));
}
return sortedMap;
}
/*
* Java method to variety Map inwards Java yesteryear value e.g. HashMap or Hashtable
* throw NullPointerException if Map contains zilch values
* It every bit good variety values fifty-fifty if they are duplicates
*/
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByValues(Map<K,V> map){
List<Map.Entry<K,V>> entries = new LinkedList<Map.Entry<K,V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
@Override
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
//LinkedHashMap volition proceed the keys inwards the gild they are inserted
//which is currently sorted on natural ordering
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(Map.Entry<K,V> entry: entries){
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
}
Output
Unsorted Map inwards Java : {USA=1, England=3, Russia=4, China=2}
Sorted Map inwards Java yesteryear key: {China=2, England=3, Russia=4, USA=1}
Sorted Map inwards Java yesteryear values: {USA=1, China=2, England=3, Russia=4}
Sorted Map inwards Java yesteryear cardinal using TreeMap : {China=2, England=3, Russia=4, USA=1}
Example to variety Map inwards Java using Guava : {China=2, England=3, Russia=4, USA=1}
That's all on How to variety Map inwards Java like HashMap, Hashtable based upon keys too values. Just yell back that sorting Map based upon values is to a greater extent than hard than sorting Map based upon keys because values inwards Map tin hold upwardly either null or duplicates which are non the instance amongst keys.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt synchronized too concurrent collection inwards Java
Demikianlah Artikel How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial
Sekianlah artikel How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial dengan alamat link https://bestlearningjava.blogspot.com/2019/09/how-to-kind-out-hashmap-past-times.html
Belum ada Komentar untuk "How To Kind Out Hashmap Past Times Primal Together With Value Inwards Coffee - Hashtable, Map Illustration Tutorial"
Posting Komentar