Network bandwidth limitation in KVM

You should add these parameters by hands. When you open the xml file of your virtual machine, find the block with interface type tag. Try to add the following

<bandwidth>
  <inbound average='xxx' peak='xxx' burst='xxxx'/>
  <outbound average='xxx' peak='xxx'/>
</bandwidth>

Where xxx is desired speed in bps.

Does java.util.HashMap.containsKey(Object key) implementation violate java.util.Map.containsKey(Object key) documentation?

java.util.Map.containsKey(Object key) documentation says:
@throws ClassCastException if the key is of an inappropriate type for this map.

The java.util.HashMap.containsKey(Object key) implementation does not say anything about it.

My problem:

If i create a Map<String,String> map = new HashMap<>(); and call the containsKey method with an Integer the value is hashed (as a String) but the method does not throw an Exception.

Btw: The hash of 4 differs from the hash of “4”.

Is this really the intended behavior?

THX in advance!

Solution:

This seems to be an optional restriction, not applied in HashMap.

As stated in API for containsKey:

[…]

Throws:
ClassCastException – if the key is of an inappropriate type for this map (optional)

Note the “optional”, and see linked documentation:

Some collection implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the collection may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as “optional” in the specification for this interface.

Why does an interface hosting main in Java 8 not have to be public?

Why does the following code compile and run successfully in Java 8+eclipse?

package package1;
interface A  
{
    static void main(String[] args) {
        System.out.println("Hi");
    }
}

If A is changed to a class, the run-time requires it to be a public class, but not so for an interface. This seems inconsistent.

EDIT: The above statment that I made when posting the question was WRONG. ( I must have been tired and misread the errors ). Java does NOT require the class hosting main to be public, only the method.
However, it is a bit inconsistent that the type hosting main does not have to be public, while the main method does.

Solution:

If A is changed to a class, the run-time requires it to be a public class.

No it doesn’t. It requires the method to be public, and methods in interfaces already are public.

but not so for an interface.

Not so.

This seems inconsistent.

It isn’t. You misread the error message.

Java 8 -Two interfaces contain same 'name' default method but different method signature, how to override?

I understand that if a class implements multiple interfaces containing default methods of same name, then we need to override that method in the child class so as to explicitly define what my method will do.
Problem is, see the below code :

interface A {
    default void print() {
        System.out.println(" In interface A ");
    }
}

interface B {
    default String print() {
        return "In interface B";
    }
}

public class C implements A, B {

    @Override
    public String print() {
        return "In class C";
    }

    public static void main(String arg[]) {
        // Other funny things
    }
}

Now interface A and B both have a default method with name ‘print’ but I want to override the print method of interface B – the one that returns a string and leave A’s print as is. But this code doesn’t compile giving this :

Overrides A.print
The return type is incompatible with A.print()

Clearly compiler is trying to override A’s print method, and I have no idea why !

Solution:

This is not possible.

8.4.8.3:

If a method declaration d1 with return type R1 overrides or hides the declaration of another method d2 with return type R2, then d1 must be return-type-substitutable for d2, or a compile-time error occurs.

8.4.5:

A method declaration d1 with return type R1 is return-type-substitutable for another method d2 with return type R2 iff any of the following is true:

  • If R1 is void then R2 is void.

  • If R1 is a primitive type then R2 is identical to R1.

  • If R1 is a reference type then one of the following is true:

    • R1, adapted to the type parameters of d2, is a subtype of R2.

    • R1 can be converted to a subtype of R2 by unchecked conversion.

    • d1 does not have the same signature as d2, and R1 = |R2|.

In other words, void, primitive- and reference-returning methods may only override and be overridden by methods of that same respective category. A void method may only override another void method, a reference-returning method may only override another reference-returning method, and so on.

One possible solution to the problem you’re having could be to use composition instead of inheritance:

class C {
    private A a = ...;
    private B b = ...;
    public A getA() { return a; }
    public B getB() { return b; }
}