首页 技术 正文
技术 2022年11月16日
0 收藏 509 点赞 5,096 浏览 7584 个字

一、MAP

package net.xsoftlab.baike;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class TestMap {

public static void main(String[] args) {
// 初始化,10W次赋值
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < 100000; i++)

//添加和删除
   map.put(i, i);
   map.remove(0, 0);
/** 增强for循环,keySet迭代 ,4种遍历方式*/
long start = System.currentTimeMillis();
for (Integer key : map.keySet()) {
map.get(key);
}
long end = System.currentTimeMillis();
System.out.println(“增强for循环,keySet迭代 -> ” + (end – start) + ” ms”);

/** 增强for循环,entrySet迭代 */
start = System.currentTimeMillis();
for (Entry<Integer, Integer> entry : map.entrySet()) {
entry.getKey();
entry.getValue();
}
end = System.currentTimeMillis();
System.out.println(“增强for循环,entrySet迭代 -> ” + (end – start) + ” ms”);

/** 迭代器,keySet迭代 */
start = System.currentTimeMillis();
Iterator<Integer> iterator = map.keySet().iterator();
Integer key;
while (iterator.hasNext()) {
key = iterator.next();
map.get(key);
}
end = System.currentTimeMillis();
System.out.println(“迭代器,keySet迭代 -> ” + (end – start) + ” ms”);

/** 迭代器,entrySet迭代 */
start = System.currentTimeMillis();
Iterator<Map.Entry<Integer, Integer>> iterator1 = map.entrySet().iterator();
Map.Entry<Integer, Integer> entry;
while (iterator1.hasNext()) {
entry = iterator1.next();
entry.getKey();
entry.getValue();
}
end = System.currentTimeMillis();

System.out.println(“迭代器,entrySet迭代 -> ” + (end – start) + ” ms”);

//另外一个例子

Map<String,String> map=new HashMap<String,String>();
map.put(“username”, “qq”);
map.put(“PassWord”, “123”);
map.put(“UserID”, “1”);
map.put(“email”, “qq@qq.com”);
//判断是否为空
System.out.println(map.isEmpty());

//删除元素,参数为key
map.remove(“username”);

//删除元素,参数为键值对
map.remove(“email”, “qq@qq.com”);

Map<String,String> map1=new HashMap<String,String>();

//整体复制
map1.putAll(map);

//判断是否包含key
System.out.println(map1.containsKey(“userID”));

//判断是否包含value
System.out.println(map1.containsValue(“123”));

//判断map的大小
System.out.println(map1.size());

//清空map
map1.clear();
System.out.println(map1.size());

//entrySet排序
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+”—>”+entry.getValue());
}

// 通过ArrayList构造函数把map.entrySet()转换成list
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
// 通过比较器实现比较排序
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
public int compare(Map.Entry<String, String> mapping1, Map.Entry<String, String> mapping2) {
return mapping1.getKey().compareTo(mapping2.getKey());
}
});

for (Map.Entry<String, String> mapping : list) {
System.out.println(mapping.getKey() + ” :” + mapping.getValue());
}

}
}

二、List

package MyTest01;

import java.util.ArrayList;import java.util.List; public class ListTest01 {     public static void main(String[] args) {                     //list中添加,获取,删除元素            List<String> person=new ArrayList<>();            person.add("jackie");   //索引为0  //.add(e)            person.add("peter");    //索引为1            person.add("annie");    //索引为2            person.add("martin");   //索引为3            person.add("marry");    //索引为4                         person.remove(3);   //.remove(index)            person.remove("marry");     //.remove(Object o)                         String per="";            per=person.get(1);            System.out.println(per);    ////.get(index)                         for (int i = 0; i < person.size(); i++) {                System.out.println(person.get(i));  //.get(index)            }             //list总是否包含某个元素            List<String> fruits=new ArrayList<>();            fruits.add("苹果");            fruits.add("香蕉");            fruits.add("桃子");            //for循环遍历list            for (int i = 0; i < fruits.size(); i++) {                System.out.println(fruits.get(i));            }            String appleString="苹果";            //true or false            System.out.println("fruits中是否包含苹果:"+fruits.contains(appleString));                         if (fruits.contains(appleString)) {                System.out.println("我喜欢吃苹果");            }else {                System.out.println("我不开心");            }            //list中根据索引将元素数值改变(替换)            String a="白龙马", b="沙和尚", c="八戒", d="唐僧", e="悟空";            List<String> people=new ArrayList<>();            people.add(a);            people.add(b);            people.add(c);            people.set(0, d);   //.set(index, element)      //将d唐僧放到list中索引为0的位置,替换a白龙马            people.add(1, e);   //.add(index, element);     //将e悟空放到list中索引为1的位置,原来位置的b沙和尚后移一位                         //增强for循环遍历list            for(String str:people){                System.out.println(str);            }            //list中查看(判断)元素的索引            List<String> names=new ArrayList<>();            names.add("刘备");    //索引为0            names.add("关羽");    //索引为1            names.add("张飞");    //索引为2            names.add("刘备");    //索引为3            names.add("张飞");    //索引为4            System.out.println(names.indexOf("刘备"));            System.out.println(names.lastIndexOf("刘备"));            System.out.println(names.indexOf("张飞"));            System.out.println(names.lastIndexOf("张飞"));                         //根据元素索引位置进行的判断            if (names.indexOf("刘备")==0) {                System.out.println("刘备在这里");            }else if (names.lastIndexOf("刘备")==3) {                System.out.println("刘备在那里");            }else {                System.out.println("刘备到底在哪里?");            }                         //利用list中索引位置重新生成一个新的list(截取集合)            List<String> phone=new ArrayList<>();            phone.add("三星");    //索引为0            phone.add("苹果");    //索引为1            phone.add("锤子");    //索引为2            phone.add("华为");    //索引为3            phone.add("小米");    //索引为4            //原list进行遍历            for(String pho:phone){                System.out.println(pho);            }            //生成新list            phone=phone.subList(14);  //.subList(fromIndex, toIndex)  //利用索引1-4的对象重新生成一个list,但是不包含索引为4的元素            for (int i = 0; i < phone.size(); i++) { // phone.size() 该方法得到list中的元素数的和                System.out.println("新的list包含的元素是"+phone.get(i));            }            //对比两个list中的所有元素            //两个相等对象的equals方法一定为true, 但两个hashcode相等的对象不一定是相等的对象            if (person.equals(fruits)) {                System.out.println("两个list中的所有元素相同");            }else {                System.out.println("两个list中的所有元素不一样");            }            if (person.hashCode()==fruits.hashCode()) {                System.out.println("我们相同");            }else {                System.out.println("我们不一样");            }            //判断list是否为空            //空则返回true,非空则返回false            if (person.isEmpty()) {                System.out.println("空的");            }else {                System.out.println("不是空的");            }                         //返回Iterator集合对象            System.out.println("返回Iterator集合对象:"+person.iterator());                         //将集合转换为字符串            String liString="";            liString=person.toString();            System.out.println("将集合转换为字符串:"+liString);                         //将集合转换为数组,默认类型

            List<String> person=new ArrayList<>();
            person.add(“jackie”); //索引为0 //.add(e)
            person.add(“peter”); //索引为1
            person.add(“annie”); //索引为2
            String[] strings = new String[person.size()];

            person.toArray(strings);

            ////将集合转换为指定类型(友好的处理)            //1.默认类型            List<Object> listsStrings=new ArrayList<>();            for (int i = 0; i < person.size(); i++) {                listsStrings.add(person.get(i));            }            //2.指定类型            List<StringBuffer> lst=new ArrayList<>();            for(String string:person){                lst.add(StringBuffer(string));            }            }     private static StringBuffer StringBuffer(String string) {        return null;    }}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918