treeset

【Java基础】集合

萝らか妹 提交于 2021-02-15 12:30:02
一、Collection接口(超级接口:Iterator) 其下有两个子接口,Set和List。 1)Set接口的两个常用子类 TreeSet:有序存放 HashSet:散列存放 2)List接口(允许重复元素) 常用子类:LinkedList、ArrayList、Vector。ArrayList是List接口最常用的一个子类。LinkedList完成的是一个链表操作。 二、Map接口 实现子类:HashMap、HashTable、TreeMap等 TreeMap与TreeSet类似,TreeMap中元素根据key自动排序。 三、集合元素为自定义类时,集合的排序问题及 输出 自定义类作为集合元素时,集合本身不知道如何排序,必须类实现Comparable接口,并覆写compareTo()方法。 public 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 int compareTo(Person o) {//按名字顺序排序 return this.name.compareTo(o.name

Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet

和自甴很熟 提交于 2020-07-28 05:55:08
问题 The bounty expires in 2 days . Answers to this question are eligible for a +50 reputation bounty. Concurrent Bhai is looking for a more detailed answer to this question. I had a interview today and the person taking my interview puzzled me with his statement asking if it possible that TreeSet equals HashSet but not HashSet equals TreeSet . I said "no" but according to him the answer is "yes". How is it even possible? 回答1: Your interviewer is right, they do not hold equivalence relation for

TreeSet Comparator failed to remove duplicates in some cases?

百般思念 提交于 2020-04-11 04:45:18
问题 I have the following comparator for my TreeSet: public class Obj { public int id; public String value; public Obj(int id, String value) { this.id = id; this.value = value; } public String toString() { return "(" + id + value + ")"; } } Obj obja = new Obj(1, "a"); Obj objb = new Obj(1, "b"); Obj objc = new Obj(2, "c"); Obj objd = new Obj(2, "a"); Set<Obj> set = new TreeSet<>((a, b) -> { System.out.println("Comparing " + a + " and " + b); int result = a.value.compareTo(b.value); if (a.id == b

TreeSet Comparator failed to remove duplicates in some cases?

余生长醉 提交于 2020-04-11 04:44:47
问题 I have the following comparator for my TreeSet: public class Obj { public int id; public String value; public Obj(int id, String value) { this.id = id; this.value = value; } public String toString() { return "(" + id + value + ")"; } } Obj obja = new Obj(1, "a"); Obj objb = new Obj(1, "b"); Obj objc = new Obj(2, "c"); Obj objd = new Obj(2, "a"); Set<Obj> set = new TreeSet<>((a, b) -> { System.out.println("Comparing " + a + " and " + b); int result = a.value.compareTo(b.value); if (a.id == b

My Treeset only adds 1 class object

允我心安 提交于 2020-01-30 11:52:06
问题 I'm trying to add the below book objects to a TreeSet. However, when I debug the code, it says that the set has a size of 1 and only contains the first object added (book1). When I comment out book1, book2 is the only one added etc. Why is it that the JVm only recognises one object? Code: public static void main(String[] args) { Set<Book> bookSet = new TreeSet<Book>(); Book book1 = new Book("Digital Fortress", "Dan Brown", "St. Martins Press", 1998); Book book2 = new Book("Angels and Demons",

Why does my TreeSet not add anything beyond the first element?

北城余情 提交于 2020-01-09 05:23:08
问题 I have several arrays in the form: private static String[] patientNames = { "John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr" }; Then I make a TreeSet like this: TreeSet<Patient> patTreeSet = new TreeSet<Patient>(); Where Patient is a different class that makes "Patient" objects. Then I loop through each element in my arrays to create several patients and add them to my patTreeSet like this: for(int i = 0; i< patientNames.length; i++){ Date dob = date.getDate("MM/dd/yyyy",

How do I sort a List of TreeSets with java8 streams

≡放荡痞女 提交于 2019-12-30 08:28:39
问题 My list contains sets like [1,3,5][2,6,4] etc, all of the same size. I tried doing this but it doesn't seem to work. List<TreeSet<T>> block; for(TreeSet<T> t : block){ block.stream().sorted((n,m)->n.compareTo(m)).collect(Collectors.toSet()); } The end result I want is [1,2,3][4,5,6] . I could try to add all the elements in an ArrayList and sort that out then make a new List of TreeSet 's. But is there is some kind of one liner? UPDATE: List<T> list=new ArrayList<T>(); for(TreeSet<T> t : block

Understanding TreeSet when compareto returns 0

不羁岁月 提交于 2019-12-30 07:18:12
问题 I have created a Student class like this: public class Student implements Comparable<Student> { private String firstName; private String lastName; public Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } // Getters & Setters follow here... @Override public int compareTo(Student student) { int hash = this.firstName.compareTo(student.firstName); return hash; } @Override public String toString() { return "Student [firstName=" + firstName + ",

Computational complexity of TreeSet operations in Java?

≯℡__Kan透↙ 提交于 2019-12-30 06:15:20
问题 I am trying to clear up some things regarding complexity in some of the operations of TreeSet. On the javadoc it says: "This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains)." So far so good. My question is what happens on addAll(), removeAll() etc. Here the javadoc for Set says: "If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets." Is it just