treeset

How do I reverse the order of the data in a TreeSet instance?

China☆狼群 提交于 2019-12-11 10:28:52
问题 I have a class called Dictionary which is a collection of sorted strings. This class is an extension of the TreeSet class, which means that it uses a Comparator for the sorting. I want to have a reverse method for reversing the order of the data set, but this is unfortunately not possible since TreeSet can't change it's Comparator after initialization. As a workaround I tried to create a new Dictionary instance using a reversed version of the original Comparator, but I can't come up with any

Most Efficient Way to Check File for List of Words

不羁的心 提交于 2019-12-11 10:04:36
问题 I just had a homework assignment that wanted me to add all the Java keywords to a HashSet. Then read in a .java file, and count how many times any keyword appeared in the .java file. The route I took was: Created an String[] array that contained all the keywords. Created a HashSet, and used Collections.addAll to add the array to the HashSet. Then as I iterated through the text file I would check it by HashSet.contains(currentWordFromFile); Someone recommended using a HashTable to do this.

TreeSet violating Set contract?

眉间皱痕 提交于 2019-12-11 04:07:41
问题 I was trying to answer this question in the forum and I found that despite of overriding the equals method in the Employee class, I am still able to add duplicate elements to the TreeSet . The Javadoc of TreeSet.add(E) method says Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves

TreeSet example

主宰稳场 提交于 2019-12-11 01:41:42
问题 Why the 3rd object is not being added to the treeset here though it is a different one? import java.util.*; class Student implements Comparable<Student>{ public String fn,ln; public Student(String fn,String ln){ this.fn=fn; this.ln=ln; } //overiding equals public boolean equals(Object o) { if (!(o instanceof Student)) return false; Student s=(Student) o; if(this==s) return true; if(this.fn.equals(s.fn) && this.ln.equals(s.ln)) return true; return false; } //overiding hashcode public int

Custom String Length Comparator: what's my mistake?

一世执手 提交于 2019-12-10 22:49:52
问题 I defined a custom comparator to sort the name(String) variable of my objects by length. Here's the code from my person class: class MyNameLengthCompare implements Comparator<Person> { @Override public int compare(Person a, Person b) { if(a.getName().length() > b.getName().length()) { return -1; } else if (a.getName().length() < b.getName().length()) { return 1; } else return 0; } } Then in my main method I called Collections.sort(personList, new MyNameLengthCompare); and then I added it to

VB.NET equivalent to java.util.TreeSet

我怕爱的太早我们不能终老 提交于 2019-12-10 19:56:29
问题 Is there a VB.NET equivalent to java.util.TreeSet? 回答1: The closest you'll find is the SortedSet(T) class. 回答2: Strangely, the .NET FCL does not include tree-bases data structures/collections. You can implement your own though. See here for a C# example (easy enough to convert to VB.NET) The C5 Library is a well-regarded project that: ... provides the following data structures, described by C# classes: array list, doubly linked list, hash-indexed array list, hash-indexed linked list, hash set

GSON: Serialize object with java.util.TreeSet

安稳与你 提交于 2019-12-10 16:15:29
问题 How can I serialize a TreeSet properly? In order to give you an idea of what's not working I've set up this little demo project. The main goal is to print a JSON string of my QData object. App.java package de.company.gsonserializer; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; public class App { public static void main( String[] args ) { QData qdata = new QData(); ArrayList<LData>

Why doesn't TreeSet.contains() work?

拜拜、爱过 提交于 2019-12-10 13:49:23
问题 public class Empty { public static void main( String[] args ) { TreeSet<Class> classes = new TreeSet<Class>(); classes.add( String.class ); String test = new String(); try{ if( classes.contains(test.getClass()) ){ System.out.println( "contains" ); } }catch(ClassCastException cce){ System.out.println( "Expected: " + classes ); System.out.println( "But it was: " + test.getClass() ); } } } Why does this throw a ClassCastException ? 回答1: When instantiating TreeSet without an explicit comparator,

java: TreeSet order

拥有回忆 提交于 2019-12-09 16:45:30
问题 With this code I get this output: TreeSet<String> t=new TreeSet<String>(); t.add("test 15"); t.add("dfd 2"); t.add("ersfd 20"); t.add("asdt 10"); Iterator<String> it=t.iterator(); while(it.hasNext()){ System.out.println(it.next); } I get: asdt 10 dfd 2 ersfd 20 test 15 How can I get an order of this kind, based on the numbers, with TreeSet? dfd 2 asdt 10 test 15 ersfd 20 回答1: The TreeSet implementation is sorting by the lexicographic order of the string values you insert. If you want to sort

how to implement a comparator for StringBuffer class in Java for use in TreeSet?

为君一笑 提交于 2019-12-08 08:37:13
问题 I want to implement a Comparator for StringBuffer that compares the StringBuffer and adds to TreeSet accordingly. This is purely for learning purposes only. I know having a mutable object in a Hasable collection is a bad idea. but the purpose here is how to implement comparator for existing StringBuffer class of Java and use it to create a TreeSet. My current code is below. The code does not compile. kindly help me out here. Thanks public class ComparatorExample { public class