Formatting a LinkedHashMap

linkedhashmap scala example
linkedhashmap example
scala immutable linkedhashmap
scala listmap

I am trying to reformat the output of a LinkedHashMap to exclude the commas and curly braces.

Here is what I have for my puts:

token.put("[Server.Protocol]", url.getProtocol() + "\n");
token.put("[Server.hostName]", url.getHost() + "\n");
token.put("[Server.Port]", url.getPort() + "\n");
values.add(token.toString());

token is my LinkedHashMap and values is my LinkedList.

Here is my output:

{[Server.Protocol]=https
, [Server.Name]=myserver
, [Server.Port.HTTPS]=123
}

Here is what I want:

[Server.Protocol]=https
[Server.hostName]=myserver
[Server.Port]=123

Here is my print method:

private void webconfigToINI() throws IOException {
    File fout = new File(propFile);

    try {
        FileOutputStream fos = new FileOutputStream(fout, true /* append */);

        writer = new PrintWriter(fos);
        if (fos.getChannel().size() == 0)
            writer.print("[Code]\n");
        for (String value : values) {
            //writer.print(value.substring(1, value.length()-1));
            writer.print(value);
        }
        // writer.print("[Code]\n");
        writer.flush();
        writer.close();
        fos.close();
    } catch (Exception e) {
        throw e;
    }
}

Instead of calling toString on your map you can use this:

String string = token.entrySet()
                      .stream()
                      .map(e -> e.getKey() + "=" + e.getValue())
                      .collect(Collectors.joining());

If you can't use Java 8 then you have to iterate and joining your data retrieving them from the map using entrySet(), as the following code:

String string = "";
for (Map.Entry<String, String> value: token.entrySet()) {
    string = string + value.getKey() + "="+value.getValue();
}

LinkedHashMap printing formatted?, public static void printMapAsMatrix(Map<Integer, Integer> map, int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { Integer v = map.get(size * i + j); if (v  Create one LinkedHashMap with String keys and Integer values. Insert five different values to the LinkedHashMap. Print out the elements of the LinkedHashMap. entrySet () method returns a Set view of the mapping contained in the LinkedHashMap. We will iterate through this Set. Assign this value to the variable set.

Here:

values.add(token.toString());

You are calling the toString() on your map object. Which will invoke the toString() implemented in the AbstractMap base class. That method iterates the entries, and adds the { } braces around its entries.

Thus, simple answer: do not call that method!

Instead, you could iterate the entrySet() of your map object manually, pull keys, values, and write those into your file.

Beyond that: do you need that map in the first place? If token is only used to call toString(), to fetch those strings ... then it would be more efficient to use a StringBuilder instead. Simply push your strings into that builder, and use that in the end.

LinkedHashMap In Java with Code Example, LinkedHashMap In Java: The Class LinkedHashMap is an extension of Java HashMap - HashMap Implementation with specific feature of retaining the insertion  LinkedHashMap in Java LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their

My one-liner (with enough code before and after to make a complete example):

    LinkedHashMap<String, String> token = new LinkedHashMap<>();
    token.put("[Server.Protocol]", "https");
    token.put("[Server.hostName]", "myserver");
    token.put("[Server.Port]", "123");
    String value = token.entrySet().stream().map(Map.Entry::toString).collect(Collectors.joining("\n"));
    System.out.println(value);

Output:

[Server.Protocol]=https
[Server.hostName]=myserver
[Server.Port]=123

scala.collection.mutable.LinkedHashMap, caCerts) { subjectToCaCerts = new LinkedHashMap<>(); for (X509Certificate caCert "https://publicobject.com/2014/12/04/html-formatting-javadocs/");  LinkedHashMap (int capacity, float fillRatio, boolean Order) This constructor allows you to specify whether the elements will be stored in the linked list by insertion order, or by order of last access. If Order is true, then access order is used. If Order is false, then insertion order is used.

Hello if you are using java 8 or later you can try this :

 LinkedHashMap<String, String> token = new LinkedHashMap<>();
        token.put("[Server.Protocol]", "https" + "\n");
        token.put("[Server.hostName]", "myserver" + "\n");
        token.put("[Server.Port]", "8080" + "\n");


        String val = token.entrySet().stream().map(k -> k.getKey()+ "="+ 
    k.getValue()).collect(joining(""));
        System.out.println(val);

the value printed will look like what you are expecting cheers

java.util.LinkedHashMap.<init> java code examples, Use the put() method to add elements to LinkedHashMap collection.First, let us create a LinkedHashMap −LinkedHashMap l = new  void foo(Map m) { Map copy = new LinkedHashMap(m); This technique is particularly useful if a module takes a map on input, copies it, and later returns results whose order is determined by that of the copy.

Add elements to LinkedHashMap collection in Java, hashCode() The HashMap, LinkedHashMap, and WeakHashMap are three of The toString() method returns a well-formatted string for all entries in the Map. How can I sort a LinkedHashMap based on its values given that the LinkedHashMap contains of String and Integer. So I need to sort it based on the Values which are Integers. Thanks a lot

Beginning Java 8 Language Features: Lambda Expressions, Inner , You can sort a Map like a HashMap, LinkedHashMap, or TreeMap in Java 8 by How to use forEach() method in Java 8 (example); How to format/parse the  void foo(Map m) { Map copy = new LinkedHashMap(m); } This technique is particularly useful if a module takes a map on input, copies it, and later returns results whose order is determined by that of the copy. (Clients generally appreciate having things returned in the same order they were presented.)

Java Code Examples of java.util.LinkedHashMap, Though, if you care for order you can opt for a Map implementation which provides some sort of ordering guarantee, e.g. LinkedHashMap which  Java LinkedHashMap class. Java LinkedHashMap class is Hashtable and Linked list implementation of the Map interface, with predictable iteration order. It inherits HashMap class and implements the Map interface. Points to remember. Java LinkedHashMap contains values based on the key. Java LinkedHashMap contains unique elements.

Comments
  • Unrelated: token is a misleading name. It implies singular, but obviously you put multiple values into it. So tokens would be better.
  • do not forget to add import for collectors import java.util.stream.Collectors