1、整体框架
方便记忆:(1)set和map接口中,sorted~接口下都有Tree~实现类。Hash~类下都有LinkedHash~实现类。
(2)Collection接口下有Queue和List接口。队列接口下有队列的实现、优先级队列的实现。List下面有ArrayList和LinkedList实现。
2、详细介绍
1、treeSet特点:
public class Test {
public static void main(String[] args) {
TreeSet set = new TreeSet();
set.add(3);
set.add(2);
set.add(5);
set.add(1);
set.add(3);
System.out.println(set);
}
}
输出:[1, 2, 3, 5]
2、LinkedHashSet的特点
去掉重复元素、具备先进先出的特点
LinkedHashSet<String> linkedHashSet=new LinkedHashSet();
linkedHashSet.add("c");
linkedHashSet.add("a");
linkedHashSet.add("a");
linkedHashSet.add("c");
linkedHashSet.add("b");
linkedHashSet.add("d");
linkedHashSet.forEach(System.out::println);
输出:
c
a
b
d
3、优先级队列就是排序后的队列
public class MainTest {
public static void main(String[] args) {
Queue<Integer> p = new PriorityQueue<>(Collections.reverseOrder());
p.offer(0);
p.offer(2);
p.offer(5);
p.offer(3);
p.offer(6);
p.offer(1);
p.offer(4);
p.offer(0);
System.out.println(p.poll());
System.out.println(p.poll());
System.out.println(p.poll());
System.out.println(p.poll());
System.out.println(p.poll());
System.out.println(p.poll());
System.out.println(p.poll());
System.out.println(p.poll());
}
}
输出:
6
5
4
3
2
1
0
0
4、
Stack stack = new Stack<Integer>();//定义一个保存整型数据的堆栈
队列使用方法
add | 添加元素,返回值boolean,队列已满则抛异常,元素不能为null |
remove | 如果队列为空,则抛异常 |
element | 队列为空,则抛异常 |
offer | 添加元素,返回值boolean,队列已满则返回false,元素不能为null |
poll | 如果队列为空,则返回null |
peek | 队列为空,则返回null |
HashMap和LinkedHashMap的Key-Value最多可以存储一个NULL值,而TreeMap不可以存储NULL值。
HashMap存储的数据是无序的;LinkedHashMap存储的数据是有序的,且按照插入时的顺序或者访问时的顺序;
TreeMap存储的数据是按照自然排序或自定义方式排序,HashMap、LinkedHashMap、TreeMap三者都行线程不安全的。
hashTable和curcurentHashMap是线程安全的。
TreeMap特点:
1、不可以存储重复的数据(当Key值相同时,Value值会新值覆盖旧值)
2、Key和Value不能存储NULL值
3、存储的数据有序(默认自然顺序)
4、TreeMap是线程不安全的,如果多线程访问,会导致抛出ConcurrentModificationException的异常。
3、遍历list的方式
@Test
public void fanxing()
{
List<String> list=newArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("eee");
for(inti=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
for (String s1 : list) {
System.out.println(s1);
}
Iterator<String> iterator=list.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}
4、遍历set集合的方式
//set的结果是无序的
@Test
public voidfanxing2()
{
Set<String> set=newHashSet<String>();
set.add("eee");
set.add("www");
set.add("qqq");
//只有增强for和迭代器两种循环
for (String s : set) {
System.out.println(s);
}
Iterator<String> iterator=set.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}
5、 遍历map的方式
//map的结果是无序的
@Test
public voidfangxing3()
{
Map<String,String> map=newHashMap<String,String>();
map.put("aaa","111");
map.put("bbb","222");
map.put("ccc","333");
//遍历map的两种方式
//1.获取所由的key,key使用get方法得到value,
//2.获取key和value的关系
Set<String> sets=map.keySet();
for (String key : sets) {
String value=map.get(key);
System.out.println(key+" :"+value);
}
System.out.println("===============");
Set<Entry<String, String>> set=map.entrySet();
for(Entry<String, String>entry :set) {
System.out.println(entry.getValue()+""+entry.getKey());
}
}