由买买提看人间百态

topics

全部话题 - 话题: sortedset
1 (共1页)
m******t
发帖数: 2416
1

Say you are writing your own SortedSetImpl, according to the contract
defined in SortedSet (see the javadoc), SortedSetImpl is supposed to have "a
constructor with a single argument of type SortedSet, which creates a new
sorted set with the same elements and the same ordering as the input sorted
set"
So in order to replicate the ordering, at some point inside SortedSetImpl(
SortedSet original) you would need to call origin.comparator() to get the
same comparator and use that to sort.
It can be
I*******o
发帖数: 53
2
SortedSet有这么一个Comparator Accessor method:
Comparator comparator();
关于这个Comparator Accessor,Java tutorial里面的解释是这样的:
==================
http://java.sun.com/docs/books/tutorial/collections/interfaces/sorted-set.html
The SortedSet interface contains an accessor method called comparator
that returns the Comparator used to sort the set, or null if the set is
sorted according to the natural ordering of its elements. This method is
provided so that sorted sets can be copied into new s
g*****g
发帖数: 34805
3
Sure, one usage is to check if this SortedSet has a comparator,
null means no and SortedSet falls back to Set. You may also
want to check what kind of Comparator this is, different comparator leads
to different sequence.
if(treeSet.comparator() instanceof ...)
r*****l
发帖数: 2859
4

狭义地说,comparator()的返回值是给“别的class的”
constructor用的。不是给自己用的。所以不能是private。
maticfat解释得比较清楚了。
广义地说,comparator()的意义是告诉别人“我是怎样排序的”。
如果comparator()的返回值是null,那么这个SortedSet里面的
element必须implements Comparable(),SortedSet用Comparable()
排序。
k*o
发帖数: 46
5
来自主题: Java版 - SortedSet的问题
需要把数据从Hashtable转到SortedSet里。
哪位给个例子。在网上找了一圈,没找到。
d******p
发帖数: 24
6
来自主题: Java版 - SortedSet的问题
SortedSet dstSet = new TreeSet(srcHash.keySet());
g*****g
发帖数: 34805
7
SortedSet is an interface, TreeSet is an implementing class.
As far as I know, interface can only have public methods.

previously.
T*****e
发帖数: 361
8
来自主题: Java版 - 问一个generic的问题吧
该怎么样cast 一个generic的对象啊?
比方说:
SortedSet s0 = new TreeSet();
Object obj o = s0;
...
SortedSet s1 = (SortedSet) o;
s1.add( new Integer(1) );
但是这样cast会导致warning:
Type safety: The cast from Object to SortedSet is actually checking
against the erased type SortedSet.
如果:
SortedSet s1 = (SortedSet) o;
s1.add( new Integer(1) );
则在add的时候会有如下的warning:
Type safety: The method add(Object) belongs to the raw type Set. References to
generic type Set should be par
a****r
发帖数: 87
9
来自主题: JobHunting版 - 今天想通了一个java的问题
javadoc
/**
* Returns a synchronized (thread-safe) sorted set backed by the
specified
* sorted set. In order to guarantee serial access, it is critical that
* all access to the backing sorted set is accomplished
* through the returned sorted set (or its views).


*
* It is imperative that the user manually synchronize on the returned
* sorted set when iterating over it or any of its subSet,
* headSet, or tailSet阅读全帖

a**********0
发帖数: 422
10
来自主题: JobHunting版 - 今天想通了一个java的问题
以前有个问题 对于java.util.Collections 的某些methods比如
static SortedSet synchronizedSortedSet(SortedSet s)
Returns a synchronized (thread-safe) sorted set backed by the specified
sorted set.
static Collection unmodifiableCollection(Collection c)
Returns an unmodifiable view of the specified collection.
我当时不明白的是SortedSet 进入那个static factory method 出来怎么就变成
thread safe version了? 这肯定不是原来的SortedSet Class 而是一个新的class 它
在原始的SortedClass之上做了改动
今天看了一下effective java 解释了我的问题 其实static... 阅读全帖
a****r
发帖数: 87
11
来自主题: JobHunting版 - 今天想通了一个java的问题
看你一下JDK implementation。
Class SynchronizedSortedSet implements SortedSet{
SynchronizedSortedSet(SortedSet s) {
super(s);
ss = s;
}
}
Basically, the original SortedSet is wrapped inside SynchronizedSortedSet.
It is not totally thread safe if you still have reference to the original
SortedSet.
l******s
发帖数: 3045
12
来自主题: JobHunting版 - 问一道面试设计题
有笔误,应该是创建SortedSet时指定一个排序方法。比如下面:
SortedSet ss = new SortedSet(Comparer.Create((
a, b) => a.CallTimes == b.CallTimes ? a.PhoneName.CompareTo(b.PhoneName) : b.
CallTimes.CompareTo(a.CallTimes)));
我用的是C#,Java也有类似的创建自定义的排序的Compare的方法。
x*********w
发帖数: 533
13
来自主题: JobHunting版 - 请教一道题:skyline
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
public class EPI_15_1 {
static class Rect {
public int lft;
public int rgt;
public int height;

public Rect(int l, int r, int h) {
lft = l;
rgt = r;
height = h;
}
}

static class Point {
... 阅读全帖
p*****3
发帖数: 488
14
来自主题: Java版 - Skyline
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
public class EPI_15_1 {
static class Rect {
public int lft;
public int rgt;
public int height;

public Rect(int l, int r, int h) {
lft = l;
rgt = r;
height = h;
}
}

static class Point {
... 阅读全帖
h****3
发帖数: 89
15
来自主题: JobHunting版 - F电面
多谢楼组分享,自己写了一个java版本的
public static void printVertical(TreeNode root){
HashMap> map = new HashMap Integer>>();
printVerticalHelper(root,0,map);
SortedSet keys = new TreeSet(map.keySet());
for(int key:keys){
List temp = map.get(key);
for(int i=0;i System.out.print(temp.get(i)+" ");
}
System.out.println();
}
... 阅读全帖
l******s
发帖数: 3045
16
来自主题: JobHunting版 - 问一道面试设计题
TrieNode上加个call times field,然后用搜索结果放到SortedSet里就可以
,创建TrieNode时指定一个排序的方法,按照call times排即可。时间复杂度O(nlgn)
,n是Trie搜索的结果长度。
k****i
发帖数: 1072
17
List can be only accessed by index. There are many collection types that can
be accessed by key, for example: Dictionary, Hashset, SortedList, SortedSet
, Hashtable.
w*******g
发帖数: 9932
18
来自主题: Java版 - 问一个generic的问题吧
I think the problem is caused by backwards compatability of Java.
SortedSet can be casted back to TreeSet without problem
but if you let Object o = s0, then the runtime type of s0 is erased to
TreeSet to make sure that TreeSet is a subtype of Object.
The warning is fine and you can just ignore it. The warning is there because
Java compiler is not smart enough to tell whether you are doing the right thing
and the runtime check is not sufficient.
m******t
发帖数: 2416
19
来自主题: Java版 - pipe & filter 问题
Something like this I guess:
SortedSet set = new TreeSet();
while((line=reader.readline())!=null) {
if(line.matches(pattern) {
set.add(line);
}
}
I*******o
发帖数: 53
20
那么这就涉及2个问题了
Q1: comparator()作为public method的话,有用途么?
Q2:如果comparator()没有其他用途,实际上只是为了给constructor用一下,
却因为是interface method而不得不成为public,这个算是违反encapsulation原则了
吧?
是否有另外一个mechanicsm/pattern可以做到类似的框架,却把comparator()给封装起
来?
v*****r
发帖数: 2325
21
来自主题: Java版 - web app 每天更新数据一次
web app 读一个dateList.csv, 但是dateList.csv 由一个python script 根据
incoming data 来更新。
目前dateList 放在一个 数据Class 的 private static SortedSet setDate
中。
如果设计较好?
查了一下, 可以用timerTask class 加上singleton data class.
http://stackoverflow.com/questions/6965296/running-a-java-metho
但是singleton 好像不建议用太多。
另外一种方法是用cronjob reload webapp (servlet)
但怎么实现呢?
p*****3
发帖数: 488
22
原来java里有balanced的BST, 比C++的好用些,就是接口太多了,记不住。
写了一个求2d overlap矩形面积的题,
核心代码没多少,code都是定义各种结构各种override各种comparator去了:
public class EPI_14_20_3 {
static class Rectangle {
public int xBeg;
public int xEnd;
public int yBeg;
public int yEnd;

public Rectangle(int xb, int xe, int yb, int ye) {
xBeg = xb;
xEnd = xe;
yBeg = yb;
yEnd = ye;
}
}

static class EndPoint {
public int index... 阅读全帖
1 (共1页)