Set集合[HashSet,TreeSet,LinkedHashSet],Map集合[HashMap,HashTable,TreeMap]
------------ Set -------------------
有序: 根据添加元素顺序判定, 如果输出的结果和添加元素顺序是一样
无序: 根据添加元素顺序判定,如果输出的结果和添加元素的顺序是不一样的
-----------------------------------------------------------
特点: 无序,不能包含重复元素
去重:
HashSet 判定对象的hashCode值是相同,如果相同会调用equals比较2个对象的内存地址是否一致
自定义对象去重都会重写hashCode和equals方法
TreeSet 添加进去元素必须实现Comparable接口->compareTo()方法实现过程中有自己的实现一套去重算法
Set
-- HasheSet
- LinkedHashSet
-- SortedSet
-- TreeSet(排序,并且自动去重)
-- HashSet底层实质是HashMap
-- 进行了HashSet,LinkedHashSet,TreeSet性能比较
HashSet: 无序,元素唯一
LinkedHashSet: 有序,并且保证元素唯一性
TreeSet: 无序,在compareTo方法定义排序算法
----------------- Map ---------------------
Map<key,value>
存储:是以键key,值value方式存储的
key: 无序,不能重复 - > Set集合
value: 可以包含重的值 - > List集合
map = new HashMap();
map.put("字",1192);
map.put(13297086608,"G");
map.put(271314998,"G");
// 通过key获取值
map.get(key);
map.get("字") // 1192
map.get(21314998) // G
// 遍历map集合,只需要key
map.keySet() ==> Set集合
map.values() ==> Collection
/////////////////////////////
for (Object key : map.keySet()) {
Object value = map.get(key);
}
// HashMap和HashTable比较
1、HashMap的key和value都可以为null,而HashTable的key和value都不能为null
2、HashMap是非线程安全,而HashTable是线程安全的
3、HashMap性能比HashTale性能要高,使用率也最多的
set

package com.set.demo.entity;
public class Person implements Comparable {
private Integer id; // 学生编号是唯一
private String name;
public Person() {
}
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int hashCode() {
return this.getName().hashCode() + this.getId().hashCode();
}
@Override
public boolean equals(Object obj) {
Person p = null;
if (obj instanceof Person) {
p = (Person) obj;
}
// 学号和姓名都完全一样我就认为是同一个学生
return p.getId().equals(this.getId()) && p.getName().equals(this.getName());
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
// 比较的过程返回3个值 >0, =0, <0
// this 指即将添加到集合的元素
// o代表与this比较的元素
Person p = (Person) o;
System.out.println(o+"-----------"+this);
return p.getId() - this.getId();
/*return p.getName().hashCode() - this.getName().hashCode() > 0 ? 1 :
(p.getName().hashCode() - this.getName().hashCode()==0 ? 0 : -1);*/
}
}
Set方法
package com.set.demo; import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; /**
* Collection
* - Set接口
* 特点: 无序,并且不能包含重复元素
* @author Administrator
*
*/
public class SetDemo {
public static void main(String[] args) {
/*
boolean add(E o);
boolean addAll(Collection c); int size();
boolean contains(Object o);
boolean isEmpty();
void clear();
Iterator<E> iterator(); boolean remove(Object o);
boolean removeAll(Collection c); boolean retainAll(Collection c);
*/ Set set = new HashSet();
set.add("1AAA");
set.add("AAA");
set.add("CCC");
set.add("AAA");
set.add("BBB");
set.add(new Object());
set.add(new Object());
set.addAll(Arrays.asList("AAA","VVV","BBB")); // 判断是否包含CCC字符串对象
System.out.println(set.contains("CCC")); // 获取集合中元素个数
System.out.println("set集合中有:"+set.size()); // 清空集合中的元素
// set.clear(); // 判断集合中是否有元素
System.out.println(set.isEmpty()); // 使用迭代器输出这个集合中的每一个元素
Iterator it = set.iterator(); while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println("\r\n"); // 移除指定的元素
set.remove("1AAA");
// 移除指定集合中的所有元素
set.removeAll(Arrays.asList("AAA","BBB","CCC")); System.out.println(set);
}
}
Set去重
package com.set.demo; import java.util.HashSet;
import java.util.Set; import com.set.demo.entity.Person; /**
* Set集合接口是无序,不重复
*
* 添加元素的时候判断集合中是否有相同的hashcode值,
* 如果有相同触发equals比较对象中各个属性完全一致认为是同一个对象
* 如果有个属性不同,认为不是同一个对象
* @author Administrator
*
*/
public class SetDemo1 {
public static void main(String[] args) {
Set set = new HashSet();
set.add(new Person(1001, "张三"));
set.add(new Person(1001, "张三"));
set.add(new Person(1002, "李四"));
set.add(new Person(1003, "王五"));
set.add(new Person(1004, "王五1"));
set.add(new Person(1005, "王五2"));
set.add(new Person(1006, "王五3"));
set.add(new Person(1007, "王五4"));
set.add(new Person(1008, "王五5"));
set.add(new Person(1009, "王五6"));
set.add(new Person(1010, "王五7")); System.out.println(set);
}
}
TreeSet排序
package com.set.demo; import java.util.TreeSet; import com.set.demo.entity.Person; /**
* 排序实现类
* @author Administrator
*
*/
public class TreeSetDemo {
public static void main(String[] args) {
// 必须让其排序的类实现排序算法()
TreeSet treeSet =new TreeSet();
treeSet.add(new Person(1001,"账务"));
treeSet.add(new Person(1010,"账务1"));
treeSet.add(new Person(1005,"账务2"));
treeSet.add(new Person(1002,"账务3")); for (Object obj : treeSet) {
System.out.println(obj);
}
}
}
比较三种set集合子类性能
package com.set.demo.entity;
public class Cat implements Comparable {
private int size;
public Cat(int size) {
this.size = size;
}
@Override
public int compareTo(Object o) {
Cat cat = (Cat) o;
return this.size - cat.size;
}
}
package com.set.demo; import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet; import com.set.demo.entity.Cat; /**
* 比较三种set集合子类性能
* HashSet
* TreeSet
* LinkedHashSet
* @author Administrator
*
*/
public class LinkedHashSetDemo {
public static void main(String[] args) {
Random rd = new Random();
testHashSet(rd);
testTreeSet(rd);
testLinkedHashSet(rd);
} private static void testLinkedHashSet(Random rd) {
LinkedHashSet set = new LinkedHashSet();
long startTime = System.nanoTime(); for (int i = 0; i < 1000; i++) { int size = rd.nextInt(1000) + 1;
set.add(new Cat(size));
} long endTime = System.nanoTime();
System.out.println("LinkedHashSet用时:"+(endTime - startTime)); } private static void testTreeSet(Random rd) {
TreeSet set = new TreeSet();
long startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) { int size = rd.nextInt(1000) + 1;
set.add(new Cat(size));
} long endTime = System.nanoTime();
System.out.println("TreeSet用时:"+(endTime - startTime)); } private static void testHashSet(Random rd) {
HashSet set = new HashSet();
long startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) { int size = rd.nextInt(1000) + 1;
set.add(new Cat(size));
} long endTime = System.nanoTime();
System.out.println("HashSet用时:"+(endTime - startTime)); }
}
去重实例
package com.set.demo; import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet; import com.set.demo.entity.Person; /**
* 去重实例
* @author Administrator
*
*/
public class DulipulteDemo {
public static void main(String[] args) {
// List集合
List personList = new ArrayList();
// 存储学生
personList.add(new Person(1004,"晴天"));
personList.add(new Person(1004,"晴天"));
personList.add(new Person(1001,"安悟"));
personList.add(new Person(1002,"不忘你"));
personList.add(new Person(1003,"Army"));
personList.add(new Person(1004,"晴天"));
personList.add(new Person(1004,"晴天"));
personList.add(new Person(1004,"晴天"));
personList.add(new Person(1004,"晴天")); // 定义去冲方法
getNewList(personList); System.out.println(personList);
} /**
* 定义去重集合中对象的方法
* @param personList
*/
private static void getNewList(List personList) {
Set set = new TreeSet();
set.addAll(personList);
// 清空原有集合
personList.clear();
// 把set集合中元素添加到list集合中
personList.addAll(set);
}
}
Map
package com.map.demo; import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap; /**
* Map集合特点:
* 以键值对方式存储的
* key value
* key1 value1
*
* key不能重复,值可以重复
* HashMap中key和value都可以为null
* @author Administrator
*
*/
public class MapDemo {
public static void main(String[] args) {
// Map集合是以键作为获取value依据
Map map = new HashMap();
map.put("aaa", "aaa对应的值");
map.put("bbb", "bbb的值");
map.put(1, "1key对应值");
map.put(null, null);
map.put("xxxxxx", "王五");
map.put("http://xxxxx/1322443", "java进阶班教室"); System.out.println(map.get(null)); System.out.println(map); System.out.println(map.keySet()); // Set 不能包含重复
System.out.println(map.values()); // List 可以包含重复
System.out.println("------------------------");
// 可以通过key遍历Map集合
for (Object obj : map.keySet()) {
Object key = obj;
Object value = map.get(obj);
System.out.println("key="+key+"-----------value="+value);
} // HashMap和HashTable区别:
//1、 HashMap的键值都可以为null,而HashTable的键和值都不能为null的。
//2、HashMap是非线程安全的,而HashTable是线程安全的。
//3、HashTale的性能比HashMap性能要低。
// 创建线程安全的Map集合
Map map2 = Collections.synchronizedMap(new HashMap()); Hashtable hashTable = new Hashtable();
//hashTable.put("AAA", null);
System.out.println("-------------------------");
SortedMap sortedMap = new TreeMap();
sortedMap.put("AAA", "AAAA");
sortedMap.put("1BBB", "BBBB");
sortedMap.put("aCCC", "CCCC");
sortedMap.put("DDD", "DDDD");
sortedMap.put("EEE", "EEEE"); System.out.println(sortedMap);
}
}
Set集合[HashSet,TreeSet,LinkedHashSet],Map集合[HashMap,HashTable,TreeMap]的更多相关文章
- Java 集合系列14之 Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)
概要 学完了Map的全部内容,我们再回头开开Map的框架图. 本章内容包括:第1部分 Map概括第2部分 HashMap和Hashtable异同第3部分 HashMap和WeakHashMap异同 转 ...
- Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)
概要 学完了Map的全部内容,我们再回头开开Map的框架图. 本章内容包括:第1部分 Map概括第2部分 HashMap和Hashtable异同第3部分 HashMap和WeakHashMap异同 转 ...
- Map总结--HashMap/HashTable/TreeMap/WeakHashMap使用场景分析(转)
首先看下Map的框架图 1.Map概述 1.Map是键值对映射的抽象接口 2.AbstractMap实现了Map中绝大部分的函数接口,它减少了“Map实现类”的重复编码 3.SortedMap有序的“ ...
- hasSet,TreeSet,ArrayList,LinkedList,Vector,HashMap,HashTable,TreeMap利用Iterator进行输出
基础类,没有重写hashCode()和equals()方法: package niukewang; import java.util.Objects; public class setClass { ...
- Java Set 常用集合 HashSet、LinkedHashSet、TreeSet
Java 中的 Set 是非常常用的数据类型.Set 是无序的 Collection,Java Set 有三个常用的实现类,分别是:HashSet.LinkedHashSet.TreeSet 本文基于 ...
- 红黑树规则,TreeSet原理,HashSet特点,什么是哈希值,HashSet底层原理,Map集合特点,Map集合遍历方法
==学习目标== 1.能够了解红黑树 2.能够掌握HashSet集合的特点以及使用(特点以及使用,哈希表数据结构) 3.能够掌握Map集合的特点以及使用(特点,常见方法,Map集合的遍历) 4.能够掌 ...
- Collections+Iterator 接口 | Map+HashMap+HashTable+TreeMap |
Collections+Iterator 接口 1. Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询 ...
- 杨晓峰-Java核心技术-9 HashMap Hashtable TreeMap MD
目录 第9讲 | 对比Hashtable.HashMap.TreeMap有什么不同? 典型回答 考点分析 知识扩展 Map 整体结构 有序 Map HashMap 源码分析 容量.负载因子和树化 精选 ...
- Java中的集合HashSet、LinkedHashSet、TreeSet和EnumSet(二)
Set接口 前面已经简绍过Set集合,它类似于一个罐子,一旦把对象'丢进'Set集合,集合里多个对象之间没有明显的顺序.Set集合于Collection基本上完全一样,它没有提供任何额外的方法. Se ...
随机推荐
- canvas绘制三等分饼型图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ZBrush通过显示与隐藏得到子物体
在ZBrush®中得到子物体的方法有很多,本文将为大家介绍一种新的创建子物体的方法,通过显示和隐藏得到子物. ZBrush 4R8中文版下载:http://wm.makeding.com/iclk/? ...
- Kattis - Speed Limit
Speed Limit Bill and Ted are taking a road trip. But the odometer in their car is broken, so they do ...
- LeetCode Golang 3. 无重复字符的最长子串
3. 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串 ...
- js浏览器下载总结(ios、and、微信、qq)
前段时间做了个项目,里面关于手机移动端下载的问题 开始是判断微信.ios和android系统的下载 因为微信屏蔽点击事件和链接的缘故,需要通过打开新页面来进行下载 ios和android的下载分别为不 ...
- 密信(MeSince),将取代传统电子邮件
电子邮件发展至今已经有几十年的历史,但仍然是最重要的现代互联网应用之一.在全球范围内,每小时发送的非垃圾邮件数量超过30亿封,从工作场景的使用到个人生活,电子邮件都扮演着不可或缺的角色.但是由于明文电 ...
- [置顶]
openHAB 部分代码结构 UML 图
openHAB 部分代码结构 UML 图 ModelRepository: ItemRegistry: ItemUIProvider: WebAppServlet:
- org.xml.sax.SAXParseException: Failed to read schema document 的原因分析与解决方法
现象: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema documen t 'http://www.s ...
- ansible 连通测试
[root@ftp:/root] > ansible ansible01 -m ping ansible01 | UNREACHABLE! => { "changed" ...
- 2.安装Cython
许多科学的Python发行版,例如Anaconda,Enthought Canopy和Sage,捆绑Cython并且不需要设置. 与大多数Python软件不同,Cython需要在系统上存在C编译器.获 ...