Set集合[HashSet,TreeSet,LinkedHashSet],Map集合[HashMap,HashTable,TreeMap]

时间:2023-03-09 09:06:12
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

Set集合[HashSet,TreeSet,LinkedHashSet],Map集合[HashMap,HashTable,TreeMap]

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);
}
}