Java Entry类详解

Entry类概述

Java的entry是一个静态内部类,实现Map.Entry< K ,V> 这个接口,通过entry类可以构成一个单向链表。

一.java中Map及Map.Entry
(1).Map是java中的接口,Map.Entry是Map的一个内部接口。
(2).Map提供了一些常用方法,如keySet()、entrySet()等方法。
(3).keySet()方法返回值是Map中key值的集合;entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry。
(4).Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry<K,V>。它表示Map中的一个实体(一个key-value对)。接口中有getKey(),getValue方法。

二.Entry类的实现源码

//源码
private static class Entry<K,V> implements Map.Entry<K,V> {
        int hash;
        final K key;
        V value;
        //next 可构成单向链表
        Entry<K,V> next;

        protected Entry(int hash, K key, V value, Entry<K,V> next) {
            this.hash = hash;
            this.key =  key;
            this.value = value;
            this.next = next;
        }

        protected Object clone() {
            return new Entry<>(hash, key, value,(next==null ? null : (Entry<K,V>) next.clone()));
        }

        // Map.Entry Ops
        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }

        public V setValue(V value) {
            if (value == null)
                throw new NullPointerException();

            V oldValue = this.value;
            this.value = value;
            return oldValue;
        }

        public boolean equals(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<?,?> e = (Map.Entry)o;

            return key.equals(e.getKey()) && value.equals(e.getValue());
        }

        public int hashCode() {
            return hash ^ value.hashCode();
        }

        public String toString() {
            return key.toString()+"="+value.toString();
        }
    }
  • 28
    点赞
  • 113
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Java中调用post上传文件带参,可以使用Java自带的HttpURLConnection或者第三方库如Apache HttpClient。以下是使用HttpURLConnection的示例代码: ```java public static void uploadFileWithParams(String urlString, Map<String, String> params, String filePath) throws IOException { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); String boundary = "---------------------------" + System.currentTimeMillis(); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream outputStream = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true); for (Map.Entry<String, String> entry : params.entrySet()) { writer.append("--" + boundary).append("\r\n"); writer.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"").append("\r\n"); writer.append("Content-Type: text/plain; charset=UTF-8").append("\r\n"); writer.append("\r\n"); writer.append(entry.getValue()).append("\r\n"); } writer.append("--" + boundary).append("\r\n"); writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + new File(filePath).getName() + "\"").append("\r\n"); writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(filePath)).append("\r\n"); writer.append("\r\n"); writer.flush(); Files.copy(Paths.get(filePath), outputStream); outputStream.flush(); writer.append("\r\n"); writer.append("--" + boundary + "--").append("\r\n"); writer.close(); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // Handle success response } else { // Handle error response } connection.disconnect(); } ``` 这段代码中,我们首先创建一个URL对象,然后使用它创建一个HttpURLConnection对象。我们设置请求方式为POST,开启输出流,设置Content-Type为multipart/form-data,指定boundary值。然后我们依次写入参数和文件信息,其中参数使用Content-Disposition设置name属性,文件使用Content-Disposition设置name和filename属性,使用URLConnection.guessContentTypeFromName获取文件型。最后我们将文件内容拷贝到输出流中,关闭输出流,等待服务器响应。在响应中,我们可以根据responseCode判断上传是否成功。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶枫^_^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值