下面列出了java.util.SortedMap#remove ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void stressMapUnChecked(SortedMap<Integer,Integer> map)
{
// get the keys, remove every odd element and re-add it
Object[] keys;
synchronized(map)
{
keys = map.keySet().toArray();
}
// remove every odd element
for (int i=1; i<keys.length; i=i+2)
{
map.remove(keys[i]);
}
// re-add every odd element
for (int i=0; i<keys.length; i++)
{
map.put((Integer)keys[i], null);
}
}
public static void writeMainSection(OutputStream out, Attributes attributes)
throws IOException {
// Main section must start with the Manifest-Version attribute.
// See https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Signed_JAR_File.
String manifestVersion = attributes.getValue(Attributes.Name.MANIFEST_VERSION);
if (manifestVersion == null) {
throw new IllegalArgumentException(
"Mandatory " + Attributes.Name.MANIFEST_VERSION + " attribute missing");
}
writeAttribute(out, Attributes.Name.MANIFEST_VERSION, manifestVersion);
if (attributes.size() > 1) {
SortedMap<String, String> namedAttributes = getAttributesSortedByName(attributes);
namedAttributes.remove(Attributes.Name.MANIFEST_VERSION.toString());
writeAttributes(out, namedAttributes);
}
writeSectionDelimiter(out);
}
private boolean playJoke(TextToSpeech tts) {
long now = System.currentTimeMillis();
// choose a random joke whose last occurrence was far enough in the past
SortedMap<Long, Utterance> availableJokes = mJokes.headMap(now - JOKE_COOLDOWN_MILLIS);
Utterance joke = null;
if (!availableJokes.isEmpty()) {
int r = RANDOM.nextInt(availableJokes.size());
int i = 0;
for (Long key : availableJokes.keySet()) {
if (i++ == r) {
joke = availableJokes.remove(key); // also removes from mJokes
break;
}
}
}
if (joke != null) {
joke.speak(tts);
// add it back with the current time
mJokes.put(now, joke);
return true;
}
return false;
}
public SortedMap<String, Integer> calculateNumberOfCitesPerYearForClass(Term inputTerm, SortedMap<String,Boolean> visibleMap) {
SortedMap<String, Integer> citesPerYear = new TreeMap<>();
boolean isTermFoundInPaperTaxonomy = false;
for(Resource r : resources) {
for (Document d : getDocumentList(r)) {
isTermFoundInPaperTaxonomy = SearchUtils.findTermInDocument(d, inputTerm) != null;
if (isTermFoundInPaperTaxonomy) {
String year = d.getYear();
int count = citesPerYear.containsKey(year) ? citesPerYear.get(year) : 0;
citesPerYear.put(year, count + d.getCites());
}
}
}
for(Map.Entry<String, Boolean> entry : visibleMap.entrySet())
if(!entry.getValue())
citesPerYear.remove(entry.getKey());
return citesPerYear;
}
/**
* GenPolynomial destructive summation.
*
* @param a
* coefficient.
* @param e
* exponent.
*/
public void doAddTo(IExpr a, ExpVectorSymbolic e) {
if (a == null || a.isZERO()) {
return;
}
SortedMap<ExpVectorSymbolic, IExpr> nv = this.val;
IExpr x = nv.get(e);
if (x != null) {
x = x.add(a);
if (!x.isZERO()) {
nv.put(e, x);
} else {
nv.remove(e);
}
} else {
nv.put(e, a);
}
return;
}
/**
* GenPolynomial summation.
*
* @param S
* GenPolynomial.
* @return this+S.
*/
// public <T extends GenPolynomial> T sum(T /*GenPolynomial*/ S) {
@Override
public ExprPolynomial sum(ExprPolynomial S) {
if (S == null) {
return this;
}
if (S.isZERO()) {
return this;
}
if (this.isZERO()) {
return S;
}
assert (ring.nvar == S.ring.nvar);
ExprPolynomial n = this.copy(); // new GenPolynomial(ring, val);
SortedMap<ExpVectorLong, IExpr> nv = n.val;
SortedMap<ExpVectorLong, IExpr> sv = S.val;
for (Map.Entry<ExpVectorLong, IExpr> me : sv.entrySet()) {
ExpVectorLong e = me.getKey();
IExpr y = me.getValue(); // sv.get(e); // assert y != null
IExpr x = nv.get(e);
if (x != null) {
x = x.add(y);
if (!x.isZERO()) {
nv.put(e, x);
} else {
nv.remove(e);
}
} else {
nv.put(e, y);
}
}
return n;
}
@Override
public SortedMap <Long, DataFlavor> getFormatsForFlavors(
DataFlavor[] flavors, FlavorTable map)
{
SortedMap <Long, DataFlavor> retval =
super.getFormatsForFlavors(flavors, map);
// The Win32 native code does not support exporting LOCALE data, nor
// should it.
retval.remove(L_CF_LOCALE);
return retval;
}
/**
* GenPolynomial scale and subtract a multiple.
*
* @param b
* scale factor.
* @param a
* coefficient.
* @param S
* GenPolynomial.
* @return this * b - a S.
*/
public SymbolicPolynomial scaleSubtractMultiple(IExpr b, IExpr a, SymbolicPolynomial S) {
if (a == null || S == null) {
return this.multiply(b);
}
if (a.isZERO() || S.isZERO()) {
return this.multiply(b);
}
if (this.isZERO() || b == null || b.isZERO()) {
return S.multiply(a.negate()); // left?
}
if (b.isOne()) {
return subtractMultiple(a, S);
}
assert (ring.nvar == S.ring.nvar);
SymbolicPolynomial n = this.multiply(b);
SortedMap<ExpVectorSymbolic, IExpr> nv = n.val;
SortedMap<ExpVectorSymbolic, IExpr> sv = S.val;
for (Map.Entry<ExpVectorSymbolic, IExpr> me : sv.entrySet()) {
ExpVectorSymbolic f = me.getKey();
// f = e.sum(f);
IExpr y = me.getValue(); // assert y != null
y = a.multiply(y); // now y can be zero
IExpr x = nv.get(f);
if (x != null) {
x = x.subtract(y);
if (!x.isZERO()) {
nv.put(f, x);
} else {
nv.remove(f);
}
} else if (!y.isZERO()) {
nv.put(f, y.negate());
}
}
return n;
}
/**
* GenPolynomial subtraction.
*
* @param S
* GenPolynomial.
* @return this-S.
*/
@Override
public ExprPolynomial subtract(ExprPolynomial S) {
if (S == null) {
return this;
}
if (S.isZERO()) {
return this;
}
if (this.isZERO()) {
return S.negate();
}
assert (ring.nvar == S.ring.nvar);
ExprPolynomial n = this.copy(); // new GenPolynomial(ring, val);
SortedMap<ExpVectorLong, IExpr> nv = n.val;
SortedMap<ExpVectorLong, IExpr> sv = S.val;
for (Map.Entry<ExpVectorLong, IExpr> me : sv.entrySet()) {
ExpVectorLong e = me.getKey();
IExpr y = me.getValue(); // sv.get(e); // assert y != null
IExpr x = nv.get(e);
if (x != null) {
x = x.subtract(y);
if (!x.isZERO()) {
nv.put(e, x);
} else {
nv.remove(e);
}
} else {
nv.put(e, y.negate());
}
}
return n;
}
/**
* GenPolynomial subtraction.
*
* @param S
* GenPolynomial.
* @return this-S.
*/
@Override
public SymbolicPolynomial subtract(SymbolicPolynomial S) {
if (S == null) {
return this;
}
if (S.isZERO()) {
return this;
}
if (this.isZERO()) {
return S.negate();
}
assert (ring.nvar == S.ring.nvar);
SymbolicPolynomial n = this.copy(); // new GenPolynomial(ring, val);
SortedMap<ExpVectorSymbolic, IExpr> nv = n.val;
SortedMap<ExpVectorSymbolic, IExpr> sv = S.val;
for (Map.Entry<ExpVectorSymbolic, IExpr> me : sv.entrySet()) {
ExpVectorSymbolic e = me.getKey();
IExpr y = me.getValue(); // sv.get(e); // assert y != null
IExpr x = nv.get(e);
if (x != null) {
x = x.subtract(y);
if (!x.isZERO()) {
nv.put(e, x);
} else {
nv.remove(e);
}
} else {
nv.put(e, y.negate());
}
}
return n;
}
/**
* GenPolynomial subtract a multiple.
*
* @param a
* coefficient.
* @param e
* exponent.
* @param S
* GenPolynomial.
* @return this - a x<sup>e</sup> S.
*/
public SymbolicPolynomial subtractMultiple(IExpr a, ExpVectorSymbolic e, SymbolicPolynomial S) {
if (a == null || a.isZERO()) {
return this;
}
if (S == null || S.isZERO()) {
return this;
}
if (this.isZERO()) {
return S.multiply(a.negate(), e);
}
assert (ring.nvar == S.ring.nvar);
SymbolicPolynomial n = this.copy();
SortedMap<ExpVectorSymbolic, IExpr> nv = n.val;
SortedMap<ExpVectorSymbolic, IExpr> sv = S.val;
for (Map.Entry<ExpVectorSymbolic, IExpr> me : sv.entrySet()) {
ExpVectorSymbolic f = me.getKey();
f = e.sum(f);
IExpr y = me.getValue(); // assert y != null
y = a.multiply(y);
IExpr x = nv.get(f);
if (x != null) {
x = x.subtract(y);
if (!x.isZERO()) {
nv.put(f, x);
} else {
nv.remove(f);
}
} else if (!y.isZERO()) {
nv.put(f, y.negate());
}
}
return n;
}
public SortedMap <Long, DataFlavor> getFormatsForFlavors(
DataFlavor[] flavors, FlavorTable map)
{
SortedMap <Long, DataFlavor> retval =
super.getFormatsForFlavors(flavors, map);
// The Win32 native code does not support exporting LOCALE data, nor
// should it.
retval.remove(L_CF_LOCALE);
return retval;
}
@Override
public SortedMap <Long, DataFlavor> getFormatsForFlavors(
DataFlavor[] flavors, FlavorTable map)
{
SortedMap <Long, DataFlavor> retval =
super.getFormatsForFlavors(flavors, map);
// The Win32 native code does not support exporting LOCALE data, nor
// should it.
retval.remove(L_CF_LOCALE);
return retval;
}
/**
* <p>Creates an instance based on user-defined data. </p>
*
* @param timeToLabels map containing the day-periods where the keys represent starting points
* and the values represent the associated labels intended for representation
* @return user-specific instance
* @throws IllegalArgumentException if given map is empty or contains empty values
* @since 3.13/4.10
*/
/*[deutsch]
* <p>Erzeugt eine Instanz, die auf benutzerdefinierten Daten beruht. </p>
*
* @param timeToLabels map containing the day-periods where the keys represent starting points
* and the values represent the associated labels intended for representation
* @return user-specific instance
* @throws IllegalArgumentException if given map is empty or contains empty values
* @since 3.13/4.10
*/
public static DayPeriod of(Map<PlainTime, String> timeToLabels) {
if (timeToLabels.isEmpty()) {
throw new IllegalArgumentException("Label map is empty.");
}
SortedMap<PlainTime, String> map = new TreeMap<PlainTime, String>(timeToLabels);
for (PlainTime key : timeToLabels.keySet()) {
if (key.getHour() == 24) {
map.put(PlainTime.midnightAtStartOfDay(), timeToLabels.get(key));
map.remove(key);
} else if (timeToLabels.get(key).isEmpty()) {
throw new IllegalArgumentException("Map has empty label: " + timeToLabels);
}
}
return new DayPeriod(null, "", map);
}
@Override
public SortedMap <Long, DataFlavor> getFormatsForFlavors(
DataFlavor[] flavors, FlavorTable map)
{
SortedMap <Long, DataFlavor> retval =
super.getFormatsForFlavors(flavors, map);
// The Win32 native code does not support exporting LOCALE data, nor
// should it.
retval.remove(L_CF_LOCALE);
return retval;
}
/**
* GenPolynomial scale and subtract a multiple.
*
* @param b
* scale factor.
* @param a
* coefficient.
* @param e
* exponent.
* @param S
* GenPolynomial.
* @return this * b - a x<sup>e</sup> S.
*/
public SymbolicPolynomial scaleSubtractMultiple(IExpr b, IExpr a, ExpVectorSymbolic e, SymbolicPolynomial S) {
if (a == null || S == null) {
return this.multiply(b);
}
if (a.isZERO() || S.isZERO()) {
return this.multiply(b);
}
if (this.isZERO() || b == null || b.isZERO()) {
return S.multiply(a.negate(), e);
}
if (b.isOne()) {
return subtractMultiple(a, e, S);
}
assert (ring.nvar == S.ring.nvar);
SymbolicPolynomial n = this.multiply(b);
SortedMap<ExpVectorSymbolic, IExpr> nv = n.val;
SortedMap<ExpVectorSymbolic, IExpr> sv = S.val;
for (Map.Entry<ExpVectorSymbolic, IExpr> me : sv.entrySet()) {
ExpVectorSymbolic f = me.getKey();
f = e.sum(f);
IExpr y = me.getValue(); // assert y != null
y = a.multiply(y); // now y can be zero
IExpr x = nv.get(f);
if (x != null) {
x = x.subtract(y);
if (!x.isZERO()) {
nv.put(f, x);
} else {
nv.remove(f);
}
} else if (!y.isZERO()) {
nv.put(f, y.negate());
}
}
return n;
}
@Override
public void processElement(BaseRow input, Context ctx, Collector<BaseRow> out) throws Exception {
initRankEnd(input);
SortedMap<BaseRow, Long> sortedMap = treeMap.value();
if (sortedMap == null) {
sortedMap = new TreeMap<>(sortKeyComparator);
}
BaseRow sortKey = sortKeySelector.getKey(input);
if (BaseRowUtil.isAccumulateMsg(input)) {
// update sortedMap
if (sortedMap.containsKey(sortKey)) {
sortedMap.put(sortKey, sortedMap.get(sortKey) + 1);
} else {
sortedMap.put(sortKey, 1L);
}
// emit
emitRecordsWithRowNumber(sortedMap, sortKey, input, out);
// update data state
List<BaseRow> inputs = dataState.get(sortKey);
if (inputs == null) {
// the sort key is never seen
inputs = new ArrayList<>();
}
inputs.add(input);
dataState.put(sortKey, inputs);
} else {
// emit updates first
retractRecordWithRowNumber(sortedMap, sortKey, input, out);
// and then update sortedMap
if (sortedMap.containsKey(sortKey)) {
long count = sortedMap.get(sortKey) - 1;
if (count == 0) {
sortedMap.remove(sortKey);
} else {
sortedMap.put(sortKey, count);
}
} else {
if (sortedMap.isEmpty()) {
if (lenient) {
LOG.warn(STATE_CLEARED_WARN_MSG);
} else {
throw new RuntimeException(STATE_CLEARED_WARN_MSG);
}
} else {
throw new RuntimeException("Can not retract a non-existent record: ${inputBaseRow.toString}. " +
"This should never happen.");
}
}
}
treeMap.update(sortedMap);
}
@Test public void buildLeftyTest() {
SortedMap<String,Integer> control = new TreeMap<>();
control.put("a", 1);
control.put("b", 2);
control.put("c", 3);
control.put("d", 4);
control.put("e", 5);
control.put("f", 6);
control.put("g", 7);
control.put("h", 8);
control.put("i", 9);
control.put("j", 10);
control.put("k", 11);
control.put("l", 12);
control.put("m", 13);
control.put("n", 14);
control.put("o", 15);
control.put("p", 16);
control.put("q", 17);
control.put("r", 18);
control.put("s", 19);
control.put("t", 20);
control.put("u", 21);
control.put("v", 22);
control.put("w", 23);
control.put("x", 24);
control.put("y", 25);
control.put("z", 26);
ImSortedMap<String,Integer> test = PersistentTreeMap.of(control.entrySet());
control.remove("g");
test = test.without("g");
assertEquals(control.hashCode(), test.hashCode());
assertTrue(control.equals(test));
assertTrue(test.equals(control));
ImSortedMap<String,Integer> ser = serializeDeserialize(test);
assertEquals(control.hashCode(), ser.hashCode());
assertTrue(control.equals(ser));
assertTrue(ser.equals(control));
equalsDistinctHashCode(control, test, ser, test.assoc("v", null));
compareIterators(control.entrySet().iterator(), test.iterator());
compareIterators(control.entrySet().iterator(), ser.iterator());
compareIterators(control.keySet().iterator(), test.keyIterator());
compareIterators(control.keySet().iterator(), ser.keyIterator());
compareIterators(control.values().iterator(), test.valIterator());
compareIterators(control.values().iterator(), ser.valIterator());
compareEntryIterSer(control.entrySet().iterator(), test.iterator());
HashMap<String,Integer> hash = new HashMap<>();
hash.putAll(control);
equalsDistinctHashCode(hash, test, ser, test.assoc("v", null));
hash = new HashMap<>();
hash.putAll(test.assoc("v", null));
equalsDistinctHashCode(control, test, ser, hash);
control.put("zz", null);
test = test.assoc("zz", null);
control.remove("a");
test = test.without("a");
compareEntryIterSer(control.entrySet().iterator(), test.iterator());
control.remove("z");
test = test.without("z");
compareEntryIterSer(control.entrySet().iterator(), test.iterator());
}
/**
* Serializes a {@link DescriptorSupport} to an {@link ObjectOutputStream}.
*/
/* If you set jmx.serial.form to "1.2.0" or "1.2.1", then we are
bug-compatible with those versions. Specifically, field names
are forced to lower-case before being written. This
contradicts the spec, which, though it does not mention
serialization explicitly, does say that the case of field names
is preserved. But in 1.2.0 and 1.2.1, this requirement was not
met. Instead, field names in the descriptor map were forced to
lower case. Those versions expect this to have happened to a
descriptor they deserialize and e.g. getFieldValue will not
find a field whose name is spelt with a different case.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
boolean compat = "1.0".equals(serialForm);
if (compat)
fields.put("currClass", currClass);
/* Purge the field "targetObject" from the DescriptorSupport before
* serializing since the referenced object is typically not
* serializable. We do this here rather than purging the "descriptor"
* variable below because that HashMap doesn't do case-insensitivity.
* See CR 6332962.
*/
SortedMap<String, Object> startMap = descriptorMap;
if (startMap.containsKey("targetObject")) {
startMap = new TreeMap<String, Object>(descriptorMap);
startMap.remove("targetObject");
}
final HashMap<String, Object> descriptor;
if (compat || "1.2.0".equals(serialForm) ||
"1.2.1".equals(serialForm)) {
descriptor = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : startMap.entrySet())
descriptor.put(entry.getKey().toLowerCase(), entry.getValue());
} else
descriptor = new HashMap<String, Object>(startMap);
fields.put("descriptor", descriptor);
out.writeFields();
}
/**
* Serializes a {@link DescriptorSupport} to an {@link ObjectOutputStream}.
*/
/* If you set jmx.serial.form to "1.2.0" or "1.2.1", then we are
bug-compatible with those versions. Specifically, field names
are forced to lower-case before being written. This
contradicts the spec, which, though it does not mention
serialization explicitly, does say that the case of field names
is preserved. But in 1.2.0 and 1.2.1, this requirement was not
met. Instead, field names in the descriptor map were forced to
lower case. Those versions expect this to have happened to a
descriptor they deserialize and e.g. getFieldValue will not
find a field whose name is spelt with a different case.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
boolean compat = "1.0".equals(serialForm);
if (compat)
fields.put("currClass", currClass);
/* Purge the field "targetObject" from the DescriptorSupport before
* serializing since the referenced object is typically not
* serializable. We do this here rather than purging the "descriptor"
* variable below because that HashMap doesn't do case-insensitivity.
* See CR 6332962.
*/
SortedMap<String, Object> startMap = descriptorMap;
if (startMap.containsKey("targetObject")) {
startMap = new TreeMap<String, Object>(descriptorMap);
startMap.remove("targetObject");
}
final HashMap<String, Object> descriptor;
if (compat || "1.2.0".equals(serialForm) ||
"1.2.1".equals(serialForm)) {
descriptor = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : startMap.entrySet())
descriptor.put(entry.getKey().toLowerCase(), entry.getValue());
} else
descriptor = new HashMap<String, Object>(startMap);
fields.put("descriptor", descriptor);
out.writeFields();
}