@@ -0,0 +1,2125 @@
+package com.levelup.java.string;%0A%0Aimport static org.junit.Assert.assertEquals;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Iterator;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0A%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport com.google.common.base.Joiner;%0A%0A/**%0A * This java example will demonstrate how to convert a map to string with seperator .%0A * %0A * @author Justin Musgrove%0A * @see %3Ca href='http://www.leveluplunch.com/java/examples/convert-map-value-key-entries-to-string-with-separator/'%3EConvert map to string%3C/a%3E%0A */%0Apublic class ConvertMapToString %7B%0A%0A%09private Map%3CString, Double%3E bookAndPrice = new HashMap%3C%3E();%0A%0A%09@Before%0A%09public void setUp() %7B%0A%09%09bookAndPrice.put(%22Delancey: A Man, a Woman, a Restaurant, a Marriage%22,%0A%09%09%09%0918.63);%0A%09%09bookAndPrice.put(%22Whole-Grain Mornings: New Breakfast Recipes to Span%22,%0A%09%09%09%0913.92);%0A%09%09bookAndPrice.put(%22Greg Atkinsons In Season: Culinary Adventures of a%22,%0A%09%09%09%0915.57);%0A%09%7D%0A%0A%09@Test%0A%09public void convert_map_to_string_java() %7B%0A%0A%09%09String seperator = %22, %22;%0A%09%09String keyValueSeparator = %22 cost %22;%0A%0A%09%09StringBuffer buffer = new StringBuffer();%0A%0A%09%09Iterator%3CEntry%3CString, Double%3E%3E entryIterator = bookAndPrice.entrySet()%0A%09%09%09%09.iterator();%0A%0A%09%09while (entryIterator.hasNext()) %7B%0A%0A%09%09%09Entry%3CString, Double%3E entry = entryIterator.next();%0A%0A%09%09%09buffer.append(entry.getKey());%0A%09%09%09buffer.append(keyValueSeparator);%0A%09%09%09buffer.append(entry.getValue());%0A%0A%09%09%09if (entryIterator.hasNext()) %7B%0A%09%09%09%09buffer.append(seperator);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09assertEquals(%0A%09%09%09%09%22Delancey: A Man, a Woman, a Restaurant, a Marriage cost 18.63, %22%0A%09%09%09%09%09%09+ %22Whole-Grain Mornings: New Breakfast Recipes to Span cost 13.92, %22%0A%09%09%09%09%09%09+ %22Greg Atkinsons In Season: Culinary Adventures of a cost 15.57%22,%0A%09%09%09%09buffer.toString());%0A%0A%09%7D%0A%0A%09@Test%0A%09public void convert_map_to_string_guava() %7B%0A%0A%09%09String mapJoined = Joiner.on(%22, %22).withKeyValueSeparator(%22 cost %22)%0A%09%09%09%09.join(bookAndPrice);%0A%0A%09%09assertEquals(%0A%09%09%09%09%22Delancey: A Man, a Woman, a Restaurant, a Marriage cost 18.63, %22%0A%09%09%09%09%09%09+ %22Whole-Grain Mornings: New Breakfast Recipes to Span cost 13.92, %22%0A%09%09%09%09%09%09+ %22Greg Atkinsons In Season: Culinary Adventures of a cost 15.57%22,%0A%09%09%09%09mapJoined);%0A%0A%09%7D%0A%7D%0A

convert map to string example