Java学习---Map的学习
1. Map
1.1. map中的方法

1.2. Map.Entry
对于集合来讲,就是把kye-value的数据保存在了Map.Entry的实例之后,再在Map集合中插入了一个Map.Entry的实例化对象
Map.Entry一般用于输出集合

1.3. Map接口的常用子类

1.4. HashTable和HashMap区别

1.5. Map的标准输出(2个)

Map的方法使用集合
package com.ftl; import java.util.*; class Person implements Comparable<Person>
{
private String name;
private int age;
public Person(String name, int age)
{
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "姓名:" + this.name + ", 年龄:" + this.age ;
}
public boolean equals(Object obj)
{
boolean flag = true;
if ( this == obj)
{
return flag ;
}
if ( !(obj instanceof Person))
{
flag = false;
}
Person p = (Person)obj;
if (this.name.equals(p.name)
&& this.age == p.age)
{
return flag;
}
else
{
flag = false;
} return flag;
} public int hashCode()
{
return this.name.hashCode() * this.age;
} @Override
public int compareTo(Person per) {
if ( this.age > per.age)
{
return 1;
}
else if (this.age < per.age)
{
return -1;
}
else
{
return this.name.compareTo(per.name);
}
} } public class HelloFtl {
public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根
System.out.println("--------------------------------------");
List<String> allList = null;
Collection<String> col = null;
allList = new ArrayList<String>();
col = new ArrayList<String>();
allList.add("Hello");
allList.add("world");
System.out.println(allList);
System.out.println("--------------------------------------");
col.add("HHH");
col.add("FTL");
col.add("WM");
allList.addAll(allList.size(), col);
System.out.println(allList);
// System.out.println("--------------------------------------");
// allList.remove(0);
// System.out.println(allList);
// System.out.println("--------------------------------------");
allList.removeAll(col);
// System.out.println(allList);
System.out.println("--------------------------------------");
for(int i = 0; i < allList.size(); i++)
{
System.out.print(allList.get(i) + "、");
}
System.out.println();
System.out.println("--------------------------------------");
for(int i = allList.size()-1; i >=0 ; i--)
{
System.out.print(allList.get(i) + "、");
}
System.out.println();
System.out.println("--------------------------------------");
Object[] obj = allList.toArray();
for(int i = 0; i <obj.length; i++)
{
String temp = (String)obj[i];
System.out.print(temp + "、");
}
System.out.println();
System.out.println("--------------------------------------");
System.out.println(allList.contains("HHH") ? "lv存在": "lv不存在");
System.out.println(allList.contains("lv的位置是:" + allList.indexOf("HHH")));
System.out.println(allList.contains("allList否为空" + allList.isEmpty() ));
System.out.println("--------------------------------------");
LinkedList<String> link = new LinkedList<String>();
link.add("A");
link.add("B");
link.add("C");
link.addFirst("Root");
link.addLast("End");
System.out.print(link);
System.out.println();
System.out.println("Element: " + link.element());
System.out.println("Pool: " + link.poll());
System.out.print(link);
System.out.println();
System.out.println("Peek: " + link.peek());
System.out.print(link);
System.out.println();
System.out.println("--------------------------------------");
for ( int i = 0; i < link.size(); i++)
{
System.out.print("Pool: "
+ "" + link.poll() + "、");
}
System.out.println();
System.out.println("--------------------------------------");
Set<Person > set = new HashSet<Person>();
// Set<Person> set = new TreeSet<Person>();
set.add(new Person("Root2", 12));
set.add(new Person("AA", 21));
set.add(new Person("BB", 22));
set.add(new Person("CC", 22));
set.add(new Person("CC", 22));
set.add(new Person("CC", 12));
System.out.println( set);
System.out.println("--------------------------------------");
System.out.println("Iterator: ");
Iterator<Person> iter = set.iterator();
while(iter.hasNext())
{
System.out.print(iter.next() + "、");
}
System.out.println();
System.out.println("--------------------------------------");
while(iter.hasNext())
{
Person str = iter.next();
if ("CC".equals(str))
{
iter.remove();
}
else
{
System.out.print(str + "、");
}
}
System.out.println("--------------------------------------");
Map<String ,String> map = null;
map = new HashMap<String, String>();
map.put("04122", "FTL1012");
map.put("04122076", "FTL");
map.put("04122078", "1012");
System.out.println(map.containsKey("04122"));
System.out.println(map.containsValue("04122"));
System.out.println("04122 --> " + map.get("04122"));
System.out.println("--------------------------------------");
System.out.println("HashMap 中所有的Key:");
Set<String> sset = map.keySet();
Iterator<String> it = sset.iterator();
while (it.hasNext())
{
String t = (String) it.next();
System.out.print( t + "\t");
}
System.out.println("--------------------------------------");
System.out.println("HashMap 中所有的Value:");
Collection<String> co = map.values();
Iterator<String> itt = co.iterator();
while (itt.hasNext())
{
String tt = (String) itt.next();
System.out.print( tt + "\t");
}
System.out.println("--------------------------------------");
map = new TreeMap<String, String>();
map.put("04122", "FTL1012");
map.put("04122076", "FTL");
map.put("04122078", "1012");
Set<String> keys = map.keySet();
Iterator<String> key = keys.iterator();
System.out.println("TreeMap 中所有的Key:");
while (key.hasNext())
{
String tt = (String)key.next();
System.out.print( key + "\t");
}
System.out.println("--------------------------------------");
Collection<String> ccc = map.values();
Iterator<String> ttt = ccc.iterator();
System.out.println("TreeMap 中所有的Value:");
while (ttt.hasNext())
{
String tt = (String) ttt.next();
System.out.print( ttt + "\t");
} System.out.println("--------------------------------------");
}
};
Map的标准输出示例:
package com.ftl;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class HelloFtl {
public static void main(String[] args){
Map<String,String>map = new TreeMap<>();
map.put("Hello", "world");
map.put("Hello1", "world1");
map.put("Hello2", "world2");
map.put("Hello3", "world3");
String str = map.get("Hello");
Set<String> set = map.keySet();
Collection<String> values = map.values();
Iterator<String> iterator = set.iterator();
Iterator<String> iter = values.iterator();
System.out.println("------------------------------------");
System.out.println("\nforeach方法:");
for (String s : values) {
System.out.print(s+"、");
}
System.out.println("------------------------------------");
System.out.println("\nIterator方法:");
while(iterator.hasNext()){
System.out.print(iterator.next()+"、");
}
System.out.println("------------------------------------");
System.out.println("\nMap的标准输出_1(Map.entrySet):");
Set<Entry<String,String>> entrySet = map.entrySet();
Iterator<Map.Entry<String, String>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<String, String> next = it.next();
System.out.print(next.getKey()+"-->: "+next.getValue()+"\n");
}
System.out.println("------------------------------------");
System.out.println("\nMap的标准输出_2(Foreach):");
for (Map.Entry<String, String> entry : entrySet) {
System.out.print(entry.getKey()+"-->: "+entry.getValue()+"\n");
}
System.out.println("------------------------------------");
System.out.println("Map.entrySet()大小:"+map.entrySet().size());
System.out.println("------------------------------------");
}}
Map的迭代输出
package test;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Test
{
public static void main(String[] args) throws Exception
{
Map<String,String> map = new HashMap();
map.put("hello", "world");
map.put("hello1", "world1");
map.put("hello2", "world2");
map.put("hello3", "world3");
map.put("hello4", "world4");
String str = map.get("hello");
boolean containsKey = map.containsKey("hello");
boolean containsValue = map.containsValue("world");
Set<String> keySet = map.keySet();
Collection<String> values = map.values(); Iterator<String> iter = values.iterator();
while(iter.hasNext()){
String s = iter.next();
System.out.println(s+"E");
} }
}
Java学习---Map的学习的更多相关文章
- Java集合Map接口与Map.Entry学习
Java集合Map接口与Map.Entry学习 Map接口不是Collection接口的继承.Map接口用于维护键/值对(key/value pairs).该接口描述了从不重复的键到值的映射. (1) ...
- sprinbcloud学习之-Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String>
日志报错,提示Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String>, 原因为 ...
- 吴裕雄--天生自然java开发常用类库学习笔记:Map接口使用的注意事项
import java.util.HashMap ; import java.util.Map ; import java.util.Set ; import java.util.Iterator ; ...
- 吴裕雄--天生自然java开发常用类库学习笔记:Map接口
import java.util.HashMap ; import java.util.Map ; public class HashMapDemo01{ public static void mai ...
- JavaSE中Map框架学习笔记
前言:最近几天都在生病,退烧之后身体虚弱.头疼.在床上躺了几天,什么事情都干不了.接下来这段时间,要好好加快进度才好. 前面用了三篇文章的篇幅学习了Collection框架的相关内容,而Map框架相对 ...
- java第5章学习总结
学号20145336 <Java程序设计>第5周学习总结 教材学习内容总结 try catch JVM会先尝试执行try区块中的内容,若发生错误且与catch后面的类型相符,则执行catc ...
- java struts2入门学习---拦截器学习
一.拦截器,拦截器栈 1.拦截器的作用 拦截器本质上和servlet的过滤器是一样的.在struts2中,拦截器能够对Action前后进行拦截,拦截器是一个可插拨的,你可以选择使用拦截器,也可以卸载拦 ...
- 2016-2017-2 20155309南皓芯java第五周学习总结
教材内容总结 这一周学习的进度和前几周比较的话是差不多的,都是学习两章. 异常处理 1.理解异常架构 2.牚握try...catch...finally处理异常的方法 3.会用throw,throws ...
- 20145118 《Java程序设计》第5周学习总结 教材学习内容总结
20145118 <Java程序设计>第5周学习总结 教材学习内容总结 1.Java中所有错误都会被打包成对象,可以通过try.catch语法对错误对象作处理,先执行try,如果出错则跳出 ...
随机推荐
- c++面试题中经常被面试官面试的小问题总结(二)(本篇偏向指针知识)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/10713204.html 1.利用指针交换两个字符串方法?(这题是我当年读大一的时候看到的,好怀 ...
- python pip 安装OpenCV
cmd pip install opencv-contrib-python -i https://pypi.mirrors.ustc.edu.cn/simple/
- url解码
最近在做流量分析的项目.需要将url解码.写了个代码,先存一下,有时间再仔细写写. #include <stdio.h> #include <stdlib.h> #includ ...
- guava快速入门(三)
Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support] ...
- nodejs记录2——一行代码实现文件下载
主要使用fs模块的pipe方法,简单粗暴: import fs from "fs"; import path from 'path'; import request from 'r ...
- 一:Bootstrap-css样式
页面大块布局: div.container 栅格系统: 一行分成 12 列 div.row div.col-md-12 div.col-xs-12 <div class="row&qu ...
- 十、获取异步线程返回值Callable
一.简介 异步线程的实现接口Runnable是无法获得返回结果的,而另一个接口Callable可以返回结果.并通过如Future等方式来获取异步结果. 二.代码示例 import java.util. ...
- 解决:启动项目报错 java.lang.UnsatisfiedLinkError: D:\Java\apache-tomcat-8.0.17\bin\tcnative-1.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
启动项目报错如下: java.lang.UnsatisfiedLinkError: D:\Java\apache-tomcat-8.0.17\bin\tcnative-1.dll: Can't loa ...
- Oracle数据库基本操作 (五) —— 使用java调用存储过程
一.环境准备 登录Oracle数据库scott账号,利用emp进行操作. 1.创建 proc_getyearsal 存储过程 -- 获取指定员工年薪 create or replace procedu ...
- jquery获取哪一个下拉框被选中
var val = $("select[name='type_irb'] option:selected").val();