概述
Set也是集合Collection接口的子接口
Set也是集合Collection接口的子接口
特点:不保证元素有顺序,数组元素不可以重复
HashSet:
底层是基于HashMap的。元素是无序的。元素不可重复,去重机制是依据hashCode()和equals()方法
LinkedHashSet:
元素有序,元素不可重复,去重机制是依据hashCode()和equals()
TreeSet:
在底层会对元素进行排序,默认是升序排顺,要求存储在TreeSet集中的元素必须实现Comparable接口,元素不可重复,去重机制是依据compareTo方法,如果compareTo()方法返回结果为0,则认为重复
Deque<String> d= new LinkedList<>();
d.offer("1"); //尾部插入 d.offerFirst("s");//队首插入 d.offerLast("2"); //队尾插入 d.poll(); //默认从队首取值,删除 d.pollFirst();//从队首取,并删除 d.pollLast();//从队尾取,删除 d.peek();//从队首取不删除 d.peekFirst(); d.peekLast(); d.getFirst(); d.getLast();
Set s = new HashSet();
Student a = new Student("re", 21, 12, "中国"); Student b = new Student("rk", 31, 56, "🚹"); Student c = new Student("ee", 41, 34, "❤"); Student d = new Student("qq", 71, 35, "🐷"); Student e = new Student("qq", 71, 35, "🐷"); s.add(a); s.add(b); s.add(c); s.add(d); s.add(e);
Set s = new LinkedHashSet();
Student a = new Student("re", 21, 12, "中国"); Student b = new Student("rk", 31, 56, "🚹"); Student c = new Student("ee", 41, 34, "❤"); Student d = new Student("qq", 71, 35, "🐷"); s.add(a); s.add(b); s.add(c); s.add(d);
Queue<String> queue = new LinkedList<>();
queue.add("1");queue.add("2");queue.add("3");queue.poll();queue.peek();queue.size();queue.forEach(s -> System.out.println(s));Collection<String> c = new ArrayList(); // Collections.replaceAll(queue,"1","2");System.out.println(queue); // queue.removeAll(queue); // System.out.println(queue);
Set<String> s = new HashSet();
s.add("one");s.add("two");s.add("three");s.add("four"); // System.out.println("one".hashCode()); // System.out.println(s); String[] s11 = s.toArray(new String[s.size()]); //toArray方法接收一个空的数组作为参数,大小等于集合的大小,然后将集合中的元素复制到这个数组中并返回该数组。for (int i = 0; i < s.size(); i++) {System.out.print(s11[i]);}System.out.println(Arrays.toString(s11));Set s1 = new LinkedHashSet();s1.add("one");s1.add("two");s1.add("three");s1.add("four");
Set s = new TreeSet();
Student a = new Student("re", 21, 12, "中国");
Student b = new Student("rk", 31, 56, "🚹");
Student c = new Student("ee", 41, 34, "❤");
Student d = new Student("qq", 71, 35, "🐷");
s.add(a);
s.add(b);
s.add(c);
s.add(d);
System.out.println(s);