#Java - Code Snippets for '#Generics' - 35 code snippet(s) found |
|
Sample 1. Get count of elements greater than 1 using Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use Stream, Predicate and Filter to get the count of elements more than 1
System.out.println(intSet.stream().filter(moreThan2Pred).count()); // Prints 3
|
|
Like Feedback lambda expression collections set hashset generics stream predicate filter |
|
|
Sample 2. Combine two Summaries and Generate a new Summary using Lambda Expression | |
|
// Populate a List using Set elements.
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
Set<Integer> intSet2 = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
intSet2.add(1);
intSet2.add(2);
intSet2.add(3);
intSet2.add(4);
// Use the stream and collectors to Summarize all Integer elements
IntSummaryStatistics summary = intSet.stream().collect(Collectors.summarizingInt(p->((Integer)p)));
summary.combine(intSet2.stream().collect(Collectors.summarizingInt(p->((Integer)p))));
System.out.println(summary); // Prints IntSummaryStatistics{count=8, sum=20, min=1, average=2.500000, max=4}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.summarizingInt summary.combine java.util.hashset java.util.stream.Collectors java.util.IntSummaryStatistics |
|
|
Sample 3. Get all elements greater than 2, sort them and then push them to a new set, using Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Set the predicate or the condition for filtering the elements.
Predicate<Integer> moreThan2Pred = (p) -> (p > 1);
// Use Filter to refine the element set, sort to Sort and Collectors.toSet to get a set out of Stream.
intSet = intSet.stream().filter(moreThan2Pred).sorted().collect(Collectors.toSet());
System.out.println(intSet); // Prints [2, 3, 4]
|
|
Like Feedback lambda expression collections set hashset generics stream predicate filter sort java.util.hashset java.util.stream.Collectors |
|
|
Sample 4. Find, if all elements of a collection matches the specified condition ( Using Lambda expressions ) | | Admin info@buggybread.com |
|
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Specify the Predicate or the Condition using Lambda expressions
Predicate<Integer> moreThan2Pred = (p) -> (p > 2);
// Use the stream method allMatch to see if all elements fulfils the Predicate.
|
|
Like Feedback lambda expression collections set hashset generics predicate |
|
|
|
Sample 5. Count number of collection elements using Lambda Expressions | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream method count to see if all elements fulfils the Predicate.
System.out.println(intSet.stream().count); // Prints 4.
|
|
Like Feedback lambda expression collections set hashset generics predicate |
|
|
Sample 6. Find the Average of all collection elements using Lambda Expressions | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and Collector to find the average of all elements.
System.out.println(intSet.stream().collect(Collectors.averagingInt(p->((Integer)p)))); Prints 2.5
|
|
Like Feedback lambda expression collections set hashset generics stream collector |
|
|
Sample 7. Group Elements by even or Odd using Lambda Expressions | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and Collector to Group by Even and Odd
System.out.println(intSet.stream().collect(Collectors.groupingBy(p->((Integer)p)%2))); Prints {0=[2, 4], 1=[1, 3]}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.groupingBy |
|
|
Sample 8. Sum all collection elements using Lambda Expressions | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and collectors to sum all elements.
System.out.println(intSet.stream().collect(Collectors.summingInt(p->(Integer)p)));
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.groupingBy |
|
|
Sample 9. Get the complete Summary of Collection Elements using Lambda Expressions | |
|
// Populate a List using Set elements.
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and collectors to Summarize all Integer elements
System.out.println(intSet.stream().collect(Collectors.summarizingInt(p->((Integer)p)))); // Prints IntSummaryStatistics{count=4, sum=10, min=1, average=2.500000, max=4}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.summarizingInt |
|
|
|
Sample 10. Find, if any element of the collection matches the specified condition ( Using Lambda Expressions ) | | Admin info@buggybread.com |
|
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Specify the Predicate or the Condition using Lambda expressions
Predicate<Integer> moreThan2Pred = (p) -> (p > 2);
// Use the stream method anyMatch to see if all elements fulfils the Predicate.
System.out.println(intSet.stream().anyMatch(moreThan2Pred)); // Prints True.
|
|
Like Feedback lambda expression collections set hashset generics predicate |
|
|
Sample 11. Find, if no element of the collection matches the specified condition ( Inverse of anyMatch ) | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Specify the Predicate or the Condition using Lambda expressions
Predicate<Integer> moreThan2Pred = (p) -> (p > 2);
// Use the stream method noneMatch to see if all elements fulfils the Predicate.
System.out.println(intSet.stream().noneMatch(moreThan2Pred)); // Prints False.
|
|
Like Feedback lambda expression collections set hashset generics predicate |
|
|
Sample 12. Populate a List using Set elements and Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and collectors to get List out of Set
List li = intSet.stream().collect(Collectors.toList());
System.out.println(li); // Prints [1, 2, 3, 4]
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.toList |
|
|
Sample 13. Create a Map using Set elements using Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and collectors to get a Map out of Set
System.out.println(intSet.stream().collect((Collectors.toMap(p->(Integer)p,q->((Integer)q)*500)))); // Prints {1=500, 2=1000, 3=1500, 4=2000}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors |
|
|
Sample 14. Code Sample / Example / Snippet of org.apache.bcel.generic.ConstantPoolGen | |
|
public void testCreateIntegerElementValue() throws Exception
{
final ClassGen cg = createClassGen("HelloWorld");
final ConstantPoolGen cp = cg.getConstantPool();
final SimpleElementValueGen evg = new SimpleElementValueGen(
ElementValueGen.PRIMITIVE_INT, cp, 555);
assertTrue("Should have the same index in the constantpool but "
+ evg.getIndex() + "!=" + cp.lookupInteger(555),
evg.getIndex() == cp.lookupInteger(555));
checkSerialize(evg, cp);
}
|
|
Like Feedback org.apache.bcel.generic.ConstantPoolGen |
|
|
|
Sample 15. Code Sample / Example / Snippet of org.apache.bcel.generic.Type | |
|
Type t = BasicType.getType((byte)idents[i].getType());
LocalVariableGen lg = method.addLocalVariable(ident, t, null, null);
int slot = lg.getIndex();
entry.setLocalVariable(lg);
InstructionHandle start = il.getEnd();
exprs[i].byte_code(il, method, cp);
start = (start == null)? il.getStart() : start.getNext();
lg.setStart(start);
il.append(new ISTORE(slot)); ASTFunDecl.pop();
l[i] = lg;
|
|
Like Feedback org.apache.bcel.generic.Type |
|
|
Sample 16. Get the Maximum number out of the Integer List, using Lambda Expression | |
|
// Declare and Initialize the Collection
List<Integer> intList = new ArrayList<Integer>();
// Add Elements
intList.add(1);
intList.add(2);
intList.add(3);
intList.add(4);
System.out.println(intList.stream().reduce(Math::max).get()); // Prints 1
|
|
Like Feedback lambda expression collections set hashset generics stream reducer Math::max |
|
|
Sample 17. Code Sample / Example / Snippet of org.apache.bcel.generic.AnnotationEntryGen | |
|
protected String dumpAnnotationEntries(final AnnotationEntryGen[] as)
{
final StringBuilder result = new StringBuilder();
result.append("[");
for (int i = 0; i < as.length; i++)
{
final AnnotationEntryGen annotation = as[i];
result.append(annotation.toShortString());
if (i + 1 < as.length) {
result.append(",");
}
}
result.append("]");
return result.toString();
}
|
|
Like Feedback org.apache.bcel.generic.AnnotationEntryGen |
|
|
Sample 18. Code Sample / Example / Snippet of org.apache.bcel.generic.ObjectType | |
|
public AnnotationEntryGen createFruitAnnotationEntry(final ConstantPoolGen cp,
final String aFruit, final boolean visibility)
{
final SimpleElementValueGen evg = new SimpleElementValueGen(
ElementValueGen.STRING, cp, aFruit);
final ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp);
final ObjectType t = new ObjectType("SimpleStringAnnotation");
final List<ElementValuePairGen> elements = new ArrayList<>();
elements.add(nvGen);
return new AnnotationEntryGen(t, elements, visibility, cp);
}
|
|
Like Feedback org.apache.bcel.generic.ObjectType |
|
|
Sample 19. Code Sample / Example / Snippet of org.apache.bcel.generic.ElementValuePairGen | |
|
public AnnotationEntryGen createFruitAnnotationEntry(final ConstantPoolGen cp,
final String aFruit, final boolean visibility)
{
final SimpleElementValueGen evg = new SimpleElementValueGen(
ElementValueGen.STRING, cp, aFruit);
final ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp);
final ObjectType t = new ObjectType("SimpleStringAnnotation");
final List<ElementValuePairGen> elements = new ArrayList<>();
elements.add(nvGen);
return new AnnotationEntryGen(t, elements, visibility, cp);
}
|
|
Like Feedback org.apache.bcel.generic.ElementValuePairGen |
|
|
|
Sample 20. Code Sample / Example / Snippet of org.apache.bcel.generic.SimpleElementValueGen | |
|
public AnnotationEntryGen createFruitAnnotationEntry(final ConstantPoolGen cp,
final String aFruit, final boolean visibility)
{
final SimpleElementValueGen evg = new SimpleElementValueGen(
ElementValueGen.STRING, cp, aFruit);
final ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp);
final ObjectType t = new ObjectType("SimpleStringAnnotation");
final List<ElementValuePairGen> elements = new ArrayList<>();
elements.add(nvGen);
return new AnnotationEntryGen(t, elements, visibility, cp);
}
|
|
Like Feedback org.apache.bcel.generic.SimpleElementValueGen |
|
|
Sample 21. Code Sample / Example / Snippet of org.apache.bcel.generic.ClassGen | |
|
public void testCreateIntegerElementValue() throws Exception
{
final ClassGen cg = createClassGen("HelloWorld");
final ConstantPoolGen cp = cg.getConstantPool();
final SimpleElementValueGen evg = new SimpleElementValueGen(
ElementValueGen.PRIMITIVE_INT, cp, 555);
assertTrue("Should have the same index in the constantpool but "
+ evg.getIndex() + "!=" + cp.lookupInteger(555),
evg.getIndex() == cp.lookupInteger(555));
checkSerialize(evg, cp);
}
|
|
Like Feedback org.apache.bcel.generic.ClassGen |
|
|
Sample 22. Code Sample / Example / Snippet of org.apache.bcel.generic.EnumElementValueGen | |
|
public void testCreateEnumElementValue() throws Exception
{
final ClassGen cg = createClassGen("HelloWorld");
final ConstantPoolGen cp = cg.getConstantPool();
final ObjectType enumType = new ObjectType("SimpleEnum"); // Supports rainbow
final EnumElementValueGen evg = new EnumElementValueGen(enumType, "Red", cp);
assertTrue(
"The new ElementValue value index should match the contents of the constantpool but "
+ evg.getValueIndex() + "!=" + cp.lookupUtf8("Red"),
evg.getValueIndex() == cp.lookupUtf8("Red"));
checkSerialize(evg, cp);
}
|
|
Like Feedback org.apache.bcel.generic.EnumElementValueGen |
|
|
Sample 23. Code Sample / Example / Snippet of org.apache.bcel.generic.ClassElementValueGen | |
|
public void testCreateClassElementValue() throws Exception
{
final ClassGen cg = createClassGen("HelloWorld");
final ConstantPoolGen cp = cg.getConstantPool();
final ObjectType classType = new ObjectType("java.lang.Integer");
final ClassElementValueGen evg = new ClassElementValueGen(classType, cp);
assertTrue("Unexpected value for contained class: '"
+ evg.getClassString() + "'", evg.getClassString().contains("Integer"));
checkSerialize(evg, cp);
}
|
|
Like Feedback org.apache.bcel.generic.ClassElementValueGen |
|
|
Sample 24. Code Sample / Example / Snippet of org.apache.bcel.generic.InstructionHandle | |
|
public void testRemoveLocalVariable() throws Exception {
final MethodGen mg = getMethod(Foo.class, "bar");
final LocalVariableGen lv = mg.getLocalVariables()[1];
assertEquals("variable name", "a", lv.getName());
final InstructionHandle start = lv.getStart();
final InstructionHandle end = lv.getEnd();
assertNotNull("scope start", start);
assertNotNull("scope end", end);
assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv));
assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv));
mg.removeLocalVariable(lv);
assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv));
assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv));
assertNull("scope start", lv.getStart());
assertNull("scope end", lv.getEnd());
}
|
|
Like Feedback org.apache.bcel.generic.InstructionHandle |
|
|
|
Sample 25. Code Sample / Example / Snippet of org.apache.bcel.generic.InstructionList | |
|
private void compare(final String name, final Method m) {
final Code c = m.getCode();
if (c == null) {
return; // e.g. abstract method
}
final byte[] src = c.getCode();
final InstructionList il = new InstructionList(src);
final byte[] out = il.getByteCode();
if (src.length == out.length) {
assertArrayEquals(name + ": " + m.toString(), src, out);
} else {
System.out.println(name + ": " + m.toString() + " " + src.length + " " + out.length);
System.out.println(bytesToHex(src));
System.out.println(bytesToHex(out));
for (final InstructionHandle ih : il) {
System.out.println(ih.toString(false));
}
fail("Array comparison failure");
}
}
|
|
Like Feedback org.apache.bcel.generic.InstructionList |
|
|
Sample 26. Code Sample / Example / Snippet of org.apache.bcel.generic.MethodGen | |
|
public void testRemoveLocalVariable() throws Exception {
final MethodGen mg = getMethod(Foo.class, "bar");
final LocalVariableGen lv = mg.getLocalVariables()[1];
assertEquals("variable name", "a", lv.getName());
final InstructionHandle start = lv.getStart();
final InstructionHandle end = lv.getEnd();
assertNotNull("scope start", start);
assertNotNull("scope end", end);
assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv));
assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv));
mg.removeLocalVariable(lv);
assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv));
assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv));
assertNull("scope start", lv.getStart());
assertNull("scope end", lv.getEnd());
}
|
|
Like Feedback org.apache.bcel.generic.MethodGen |
|
|
Sample 27. Code Sample / Example / Snippet of org.apache.bcel.generic.InvokeInstruction | |
|
public void testB262() throws ClassNotFoundException
{
final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestEnum");
final ClassGen gen = new ClassGen(clazz);
final ConstantPoolGen pool = gen.getConstantPool();
final Method m = gen.getMethodAt(0);
final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
final InstructionList il = mg.getInstructionList();
final InstructionHandle ih = il.findHandle(3);
final InvokeInstruction ii = (InvokeInstruction)(ih.getInstruction());
final String cn = ii.getClassName(pool);
assertEquals("[Lorg.apache.bcel.data.PLSETestEnum;", cn);
}
|
|
Like Feedback org.apache.bcel.generic.InvokeInstruction |
|
|
Sample 28. Code Sample / Example / Snippet of org.apache.bcel.generic.FieldGen | |
|
public static void main(String[] argv) throws Exception {
JavaClass clazz;
if ((clazz = Repository.lookupClass(argv[0])) == null) {
clazz = new ClassParser(argv[0]).parse(); // May throw IOException
}
ClassGen cg = new ClassGen(clazz);
for (Method method : clazz.getMethods()) {
MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool());
cg.replaceMethod(method, mg.getMethod());
}
for (Field field : clazz.getFields()) {
FieldGen fg = new FieldGen(field, cg.getConstantPool());
cg.replaceField(field, fg.getField());
}
cg.getJavaClass().dump(clazz.getClassName() + ".clazz");
}
|
|
Like Feedback org.apache.bcel.generic.FieldGen |
|
|
Sample 29. Code Sample / Example / Snippet of org.apache.bcel.generic.LocalVariableGen | |
|
public void testRemoveLocalVariable() throws Exception {
final MethodGen mg = getMethod(Foo.class, "bar");
final LocalVariableGen lv = mg.getLocalVariables()[1];
assertEquals("variable name", "a", lv.getName());
final InstructionHandle start = lv.getStart();
final InstructionHandle end = lv.getEnd();
assertNotNull("scope start", start);
assertNotNull("scope end", end);
assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv));
assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv));
mg.removeLocalVariable(lv);
assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv));
assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv));
assertNull("scope start", lv.getStart());
assertNull("scope end", lv.getEnd());
}
|
|
Like Feedback org.apache.bcel.generic.LocalVariableGen |
|
|
|
Sample 30. Code Sample / Example / Snippet of org.apache.bcel.generic.BranchHandle | |
|
private static BranchHandle bh_list = null; // List of reusable handles
static BranchHandle getBranchHandle( final BranchInstruction i ) {
if (bh_list == null) {
return new BranchHandle(i);
}
final BranchHandle bh = bh_list;
bh_list = (BranchHandle) bh.getNext();
bh.setInstruction(i);
return bh;
}
|
|
Like Feedback org.apache.bcel.generic.BranchHandle |
|
|
Sample 31. Code Sample / Example / Snippet of org.apache.bcel.generic.BranchInstruction | |
|
public void redirectBranches(final InstructionHandle old_target, final InstructionHandle new_target) {
for (InstructionHandle ih = start; ih != null; ih = ih.getNext()) {
final Instruction i = ih.getInstruction();
if (i instanceof BranchInstruction) {
final BranchInstruction b = (BranchInstruction) i;
final InstructionHandle target = b.getTarget();
if (target == old_target) {
b.setTarget(new_target);
}
if (b instanceof Select) { // Either LOOKUPSWITCH or TABLESWITCH
final InstructionHandle[] targets = ((Select) b).getTargets();
for (int j = 0; j < targets.length; j++) {
if (targets[j] == old_target) {
((Select) b).setTarget(j, new_target);
}
}
}
}
}
}
|
|
Like Feedback org.apache.bcel.generic.BranchInstruction |
|
|
Sample 32. Code Sample / Example / Snippet of org.apache.bcel.generic.Instruction | |
|
public Instruction copy() {
Instruction i = null;
if (InstructionConst.getInstruction(this.getOpcode()) != null) {
i = this;
} else {
try {
i = (Instruction) clone();
} catch (final CloneNotSupportedException e) {
System.err.println(e);
}
}
return i;
}
|
|
Like Feedback org.apache.bcel.generic.Instruction |
|
|
Sample 33. Code Sample / Example / Snippet of org.apache.bcel.generic.Select | |
|
protected Object clone() throws CloneNotSupportedException {
final Select copy = (Select) super.clone();
copy.match = match.clone();
copy.indices = indices.clone();
copy.targets = targets.clone();
return copy;
}
|
|
Like Feedback org.apache.bcel.generic.Select |
|
|
Sample 34. Code Sample / Example / Snippet of org.apache.bcel.generic.CPInstruction | |
|
public void replaceConstantPool(final ConstantPoolGen old_cp, final ConstantPoolGen new_cp) {
for (InstructionHandle ih = start; ih != null; ih = ih.getNext()) {
final Instruction i = ih.getInstruction();
if (i instanceof CPInstruction) {
final CPInstruction ci = (CPInstruction) i;
final Constant c = old_cp.getConstant(ci.getIndex());
ci.setIndex(new_cp.addConstant(c, old_cp));
}
}
}
|
|
Like Feedback org.apache.bcel.generic.CPInstruction |
|
|
|
Sample 35. Code Sample / Example / Snippet of org.apache.bcel.generic.ReferenceType | |
|
public ObjectType getLoadClassType( final ConstantPoolGen cpg ) {
final ReferenceType rt = getReferenceType(cpg);
if(rt instanceof ObjectType) {
return (ObjectType)rt;
}
throw new ClassGenException(rt.getSignature() + " does not represent an ObjectType");
}
|
|
Like Feedback org.apache.bcel.generic.ReferenceType |
|
|