public static void jdk9Map() {
    // Empty immutable map & if you insert UnsupportedOperationException
    Map<String, Integer> emptyMap = Map.of();
    emptyMap.put("height", 174);

    // It can take upto 10 elements to Map
    Map<String, Integer> heights =
        Map.of("sriram", 174, "prabhu", 158, "surya", 188, "praveen", 159);
    System.out.println(heights);

    // To add more than 10 objects to Map
    Map<String, String> cities =
        Map.ofEntries(
            Map.entry("sriram", "madurai"),
            entry("prabhu", "madurai"),
            entry("jack", "london"),
            entry("shankar", "coimbator"));
    System.out.println(cities);
  }
示例#2
0
  /**
   * A {@code WebSocket} close status code.
   *
   * <p>Some codes <a href="https://tools.ietf.org/html/rfc6455#section-7.4">specified</a> in the
   * WebSocket Protocol are defined as named constants here. Others can be {@linkplain #of(int)
   * retrieved on demand}.
   *
   * <p>This is a <a href="../../lang/doc-files/ValueBased.html">value-based</a> class; use of
   * identity-sensitive operations (including reference equality ({@code ==}), identity hash code,
   * or synchronization) on instances of {@code CloseCode} may have unpredictable results and should
   * be avoided.
   *
   * @since 9
   */
  final class CloseCode {

    /**
     * Indicates a normal close, meaning that the purpose for which the connection was established
     * has been fulfilled.
     *
     * <p>Numerical representation: {@code 1000}
     */
    public static final CloseCode NORMAL_CLOSURE = new CloseCode(1000, "NORMAL_CLOSURE");

    /**
     * Indicates that an endpoint is "going away", such as a server going down or a browser having
     * navigated away from a page.
     *
     * <p>Numerical representation: {@code 1001}
     */
    public static final CloseCode GOING_AWAY = new CloseCode(1001, "GOING_AWAY");

    /**
     * Indicates that an endpoint is terminating the connection due to a protocol error.
     *
     * <p>Numerical representation: {@code 1002}
     */
    public static final CloseCode PROTOCOL_ERROR = new CloseCode(1002, "PROTOCOL_ERROR");

    /**
     * Indicates that an endpoint is terminating the connection because it has received a type of
     * data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it
     * receives a binary message).
     *
     * <p>Numerical representation: {@code 1003}
     */
    public static final CloseCode CANNOT_ACCEPT = new CloseCode(1003, "CANNOT_ACCEPT");

    /**
     * Indicates that an endpoint is terminating the connection because it has received data within
     * a message that was not consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
     * data within a text message).
     *
     * <p>Numerical representation: {@code 1007}
     */
    public static final CloseCode NOT_CONSISTENT = new CloseCode(1007, "NOT_CONSISTENT");

    /**
     * Indicates that an endpoint is terminating the connection because it has received a message
     * that violates its policy. This is a generic status code that can be returned when there is no
     * other more suitable status code (e.g., {@link #CANNOT_ACCEPT} or {@link #TOO_BIG}) or if
     * there is a need to hide specific details about the policy.
     *
     * <p>Numerical representation: {@code 1008}
     */
    public static final CloseCode VIOLATED_POLICY = new CloseCode(1008, "VIOLATED_POLICY");

    /**
     * Indicates that an endpoint is terminating the connection because it has received a message
     * that is too big for it to process.
     *
     * <p>Numerical representation: {@code 1009}
     */
    public static final CloseCode TOO_BIG = new CloseCode(1009, "TOO_BIG");

    /**
     * Indicates that an endpoint is terminating the connection because it encountered an unexpected
     * condition that prevented it from fulfilling the request.
     *
     * <p>Numerical representation: {@code 1011}
     */
    public static final CloseCode UNEXPECTED_CONDITION =
        new CloseCode(1011, "UNEXPECTED_CONDITION");

    private static final Map<Integer, CloseCode> cached =
        Map.ofEntries(
            entry(NORMAL_CLOSURE),
            entry(GOING_AWAY),
            entry(PROTOCOL_ERROR),
            entry(CANNOT_ACCEPT),
            entry(NOT_CONSISTENT),
            entry(VIOLATED_POLICY),
            entry(TOO_BIG),
            entry(UNEXPECTED_CONDITION));

    /**
     * Returns a {@code CloseCode} from its numerical representation.
     *
     * <p>The given {@code code} should be in the range {@code 1000 <= code <= 4999}, and should not
     * be equal to any of the following codes: {@code 1004}, {@code 1005}, {@code 1006} and {@code
     * 1015}.
     *
     * @param code numerical representation
     * @return a close code corresponding to the provided numerical value
     * @throws IllegalArgumentException if {@code code} violates any of the requirements above
     */
    public static CloseCode of(int code) {
      if (code < 1000 || code > 4999) {
        throw new IllegalArgumentException("Out of range: " + code);
      }
      if (code == 1004 || code == 1005 || code == 1006 || code == 1015) {
        throw new IllegalArgumentException("Reserved: " + code);
      }
      CloseCode closeCode = cached.get(code);
      return closeCode != null ? closeCode : new CloseCode(code, "");
    }

    private final int code;
    private final String description;

    private CloseCode(int code, String description) {
      assert description != null;
      this.code = code;
      this.description = description;
    }

    /**
     * Returns a numerical representation of this close code.
     *
     * @return a numerical representation
     */
    public int getCode() {
      return code;
    }

    /**
     * Compares this close code to the specified object.
     *
     * @param o the object to compare this {@code CloseCode} against
     * @return {@code true} iff the argument is a close code with the same {@linkplain #getCode()
     *     numerical representation} as this one
     */
    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (!(o instanceof CloseCode)) {
        return false;
      }
      CloseCode that = (CloseCode) o;
      return code == that.code;
    }

    @Override
    public int hashCode() {
      return code;
    }

    /**
     * Returns a human-readable representation of this close code.
     *
     * @apiNote The representation is not designed to be parsed; the format may change unexpectedly.
     * @return a string representation
     */
    @Override
    public String toString() {
      return code + (description.isEmpty() ? "" : (": " + description));
    }

    private static Map.Entry<Integer, CloseCode> entry(CloseCode cc) {
      return Map.entry(cc.getCode(), cc);
    }
  }