Code example for LinkedHashMap

0
 * @author crazybob@google.com (Bob Lee) 
 */ 
public class ToStringBuilder { 
 
    // Linked hash map ensures ordering. 
    final Map<String, Object> map = new LinkedHashMap<>();
 
    final String name;
 
    public ToStringBuilder(String name) {
        this.name = name;
    } 
 
    public ToStringBuilder(Class type) {
        this.name = type.getSimpleName();
    } 
 
    public ToStringBuilder add(String name, Object value) {
        if (map.put(name, value) != null) {
            throw new RuntimeException("Duplicate names: " + name);