#Java - Code Snippets for '#Map.Entry' - 12 code snippet(s) found |
|
Sample 1. Given a Map with Country Name as key and its capitals as values, Find the name of countries having more than 1 capital ? | |
|
Map<String,List<String>> capitals = new HashMap();
capitals.put("United States", Arrays.asList("Washington DC"));
capitals.put("Canada", Arrays.asList("Ottawa"));
capitals.put("South Africa", Arrays.asList("Pretoria", "Cape Down", "Bloemfontein"));
for(Map.Entry<String,List<String>> entry: capitals.entrySet()){
if(entry.getValue().size() > 1){
System.out.println(entry.getKey());
}
}
|
|
Like Feedback Map of String and List |
|
|
Sample 2. Method to remove duplicates from a Map ( string and List of objects ) by matching the member field of the object. | |
|
Map<String,List<ClassInfoBean>> removeDuplicates(Map<String, List<ClassInfoBean>> interfaceMap){
Map<String, List<ClassInfoBean>> interfaceMapWithoutDuplicate = new HashMap();
for(Map.Entry<String, List<ClassInfoBean>> entry: interfaceMap.entrySet()){
List<ClassInfoBean> classListWithoutDuplicate = new ArrayList();
boolean alreadyContain = false;
for(ClassInfoBean classes: entry.getValue()){
for(ClassInfoBean classes1: classListWithoutDuplicate){
if(classes1.name.equalsIgnoreCase(classes.name)){
alreadyContain = true;
break;
}
}
if(alreadyContain == false){
classListWithoutDuplicate.add(classes);
}
}
interfaceMapWithoutDuplicate.put(entry.getKey(), classListWithoutDuplicate);
}
return interfaceMapWithoutDuplicate;
}
|
|
Like Feedback map map of list of objects remove duplicates from map Map.Entry |
|
|
Sample 3. Code Sample / Example / Snippet of java.util.Properties | |
|
public Connection createConnection() throws SQLException {
final Properties info = new Properties();
for (Map.Entry<String, String> entry : map.entrySet()) {
info.setProperty(entry.getKey(), entry.getValue());
}
Connection connection =
DriverManager.getConnection("jdbc:calcite:", info);
for (ConnectionPostProcessor postProcessor : postProcessors) {
connection = postProcessor.apply(connection);
}
return connection;
}
|
|
Like Feedback java.util.Properties |
|
|
Sample 4. Write a Program for Graph Breadth First Traversal using Apache Commons MultiMap | |
|
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class Graph {
private static Multimap<Integer,Integer> adjacentDirectedNodesMap = ArrayListMultimap.create();
private static Set<Integer> alreadyVisited = new HashSet();
static{
adjacentDirectedNodesMap.put(1, 2);
adjacentDirectedNodesMap.put(1, 3);
adjacentDirectedNodesMap.put(1, 5);
adjacentDirectedNodesMap.put(2, 4);
adjacentDirectedNodesMap.put(4, 5);
}
public static void main(String[] args){
ArrayList visited = new ArrayList();
Integer startNode = 1;
displayAdjacentNodes(startNode);
}
private static void displayAdjacentNodes(Integer integer){
System.out.println(integer);
for(Map.Entry<Integer, Collection<Integer>> adjacentNodes: adjacentDirectedNodesMap.asMap().entrySet()){
for(Integer integer1:adjacentNodes.getValue()){
if(alreadyVisited.contains(integer1)){
continue;
}
alreadyVisited.add(integer1);
System.out.println(integer1);
}
}
}
}
|
|
Like Feedback graph traversal breadth first traversal |
|
|
|
Sample 5. Method to get a map of words and their count by passing in the string | |
|
Map<String,Integer> wordCountMap = new TreeMap();
String[] words = text.split(" ");
Set<Integer> countSet;
for(String word: words) {
if(wordCountMap.containsKey(word.toLowerCase())){
wordCountMap.put(word.toLowerCase(), wordCountMap.get(word.toLowerCase()).intValue() + 1);
} else {
wordCountMap.put(word.toLowerCase(), 1);
}
}
countSet = new TreeSet(Collections.reverseOrder());
countSet.addAll(wordCountMap.values());
for(Integer inte: countSet) {
for(Entry<String,Integer> entry: wordCountMap.entrySet()){
if(entry.getValue() == inte) {
System.out.println(entry);
}
}
}
|
|
Like Feedback map word count map of string and integer containsKey string split treeset Collections.reverseOrder set addAll entry |
|
|
Sample 6. Write a Program for coin changer application. The program should set the cash box with available currency and then should render the exact change. If enough cash is not available , it should present the appropriate message. | |
|
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class CoinChanger {
enum Currency {
DOLLAR(1),QUARTER(.25),DIME(.10),NICKEL(.05),PENNY(.01);
private double currencyValue;
Currency(double value){
currencyValue = value;
}
double getCurrencyValue(){
return this.currencyValue;
}
}
private static Map<Currency, Integer> cashBox;
private static Map<Currency, Integer> change;
static {
cashBox = new TreeMap<Currency, Integer>();
change = new TreeMap<Currency, Integer>();
initializeCashBox();
}
public static void main(String[] args) {
double amountToReturn = 18.79f; // Set the amount to be changed here
for(Entry<Currency, Integer> entry:cashBox.entrySet()){
Currency currency = (Currency)entry.getKey();
int coinCount = (int)(amountToReturn/(entry.getKey().getCurrencyValue()));
int availableCurrency = (int)(entry.getValue());
if(coinCount > availableCurrency){
coinCount = availableCurrency;
}
change.put(currency, coinCount);
if(coinCount > 0){
amountToReturn = amountToReturn - (coinCount * entry.getKey().getCurrencyValue());
}
}
System.out.println(change);
if(amountToReturn > .1){
System.out.println("Not enough cash");
}
}
private static void initializeCashBox(){
//set the cash box
cashBox.put(Currency.DOLLAR, 50);
cashBox.put(Currency.QUARTER, 0);
cashBox.put(Currency.DIME, 50);
cashBox.put(Currency.NICKEL, 50);
cashBox.put(Currency.PENNY, 50);
}
}
|
|
Like Feedback coin changer application coin changer app |
|
|
Sample 7. Usage of org.apache.spark.SparkConf | |
|
SparkConf sparkConf = new SparkConf();
for (Map.Entry<String, String> e : conf) {
if (e.getKey().startsWith("spark.")) {
sparkConf.set(e.getKey(), e.getValue());
}
}
this.sparkContext = new JavaSparkContext(sparkConnect, getName(), sparkConf);
|
|
Like Feedback |
|
|
Sample 8. Code Sample / Example / Snippet of org.mortbay.jetty.servlet.FilterHolder | |
|
protected void defineFilter(WebApplicationContext ctx, String name,
String classname, Map<String, String> parameters, String[] urls) {
WebApplicationHandler handler = ctx.getWebApplicationHandler();
FilterHolder holder = handler.defineFilter(name, classname);
if (parameters != null) {
for(Map.Entry<String, String> e : parameters.entrySet()) {
holder.setInitParameter(e.getKey(), e.getValue());
}
}
for (String url : urls) {
handler.addFilterPathMapping(url, name, Dispatcher.__ALL);
}
}
|
|
Like Feedback org.mortbay.jetty.servlet.FilterHolder |
|
|
Sample 9. Code Sample / Example / Snippet of org.mortbay.jetty.servlet.WebApplicationHandler | |
|
protected void defineFilter(WebApplicationContext ctx, String name,
String classname, Map<String, String> parameters, String[] urls) {
WebApplicationHandler handler = ctx.getWebApplicationHandler();
FilterHolder holder = handler.defineFilter(name, classname);
if (parameters != null) {
for(Map.Entry<String, String> e : parameters.entrySet()) {
holder.setInitParameter(e.getKey(), e.getValue());
}
}
for (String url : urls) {
handler.addFilterPathMapping(url, name, Dispatcher.__ALL);
}
}
|
|
Like Feedback org.mortbay.jetty.servlet.WebApplicationHandler |
|
|
|
Sample 10. Code Sample / Example / Snippet of com.mongodb.DBObject | |
|
private Enumerable<Object> find(DB mongoDb, String filterJson,
String projectJson, List<Map.Entry<String, Class>> fields) {
final DBCollection collection =
mongoDb.getCollection(collectionName);
final DBObject filter =
filterJson == null ? null : (DBObject) JSON.parse(filterJson);
final DBObject project =
projectJson == null ? null : (DBObject) JSON.parse(projectJson);
final Function1<DBObject, Object> getter = MongoEnumerator.getter(fields);
return new AbstractEnumerable<Object>() {
public Enumerator<Object> enumerator() {
final DBCursor cursor = collection.find(filter, project);
return new MongoEnumerator(cursor, getter);
}
};
}
|
|
Like Feedback com.mongodb.DBObject |
|
|
Sample 11. Code Sample / Example / Snippet of com.mongodb.DBCollection | |
|
private Enumerable<Object> find(DB mongoDb, String filterJson,
String projectJson, List<Map.Entry<String, Class>> fields) {
final DBCollection collection =
mongoDb.getCollection(collectionName);
final DBObject filter =
filterJson == null ? null : (DBObject) JSON.parse(filterJson);
final DBObject project =
projectJson == null ? null : (DBObject) JSON.parse(projectJson);
final Function1<DBObject, Object> getter = MongoEnumerator.getter(fields);
return new AbstractEnumerable<Object>() {
public Enumerator<Object> enumerator() {
final DBCursor cursor = collection.find(filter, project);
return new MongoEnumerator(cursor, getter);
}
};
}
|
|
Like Feedback com.mongodb.DBCollection |
|
|
Sample 12. Sort Entries of a Map by Value | |
|
public class BuggyBread {
public static void main(String args[]) {
Map<String,Integer> wordLength = new HashMap();
String str = "We are what we repeatedly do; excellence, then, is not an act but a habit";
for(String word: str.split(" ")){
wordLength.put(word, word.length());
}
List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>( wordLength.entrySet() );
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o1.getValue()).compareTo( o2.getValue() ) * -1;
}
} );
System.out.println(list);
}
}
|
|
Like Feedback |
|
|