Java Map和List常见操作

Java map 详解 - 用法、遍历、排序、常用API等 - Java初级码农 - 博客园 (cnblogs.com)

复制代码
// 创建实例
import java.util.*;

public class ListDemo {
    public static void main(String[] args) {
        List<String> arrayList = new ArrayList<>(); // 创建ArrayList实例
        List<String> linkedList = new LinkedList<>(); // 创建LinkedList实例
    }
}

// 添加元素
arrayList.add("Apple");
linkedList.add(0, "Banana"); // 在首位插入

// 遍历元素
System.out.println(arrayList.get(0));
for (String fruit : arrayList) {
    System.out.println(fruit);
}

// 修改元素
arrayList.set(0, "Orange");

// 移除元素
arrayList.remove(0);
arrayList.remove("Orange");

// 判断是否包含元素
boolean hasApple = arrayList.contains("Apple");
int index = arrayList.indexOf("Apple");
int index = arrayList.lastIndexOf("Apple");

// List大小
int size = arrayList.size();
// 清除List
arrayList.clear();
复制代码

 

Java集合之List(超详细)_java list-CSDN博客

复制代码
// 创建实例
import java.util.*;

public class ListDemo {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
    }
}


// 插入元素
map.put("key1", "value1");



// 获取元素
map.get("key1")


// 移除元素
map.remove("key1");


// 清空map
map.clear();


// 遍历1
for (String key : map.keySet()) {
    System.out.println(key + " :" + map.get(key));
}


// 遍历2
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " :" + entry.getValue());
}
复制代码

 

posted @   你说夕阳很美  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示