How to set Java Object into JaxBElement ? * In JaxBElement we are having setValue(T t) method. this will help us to solve this problem. * java.xml.bind.JAXBElement.setValue() Eg. Lets Use College Class as Bean. MainJaxBElement class as main class to process the College Class. package com.javavirus.jaxbelement; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class College{ private int id; private String name; public College(int id, String name) { super(); this.id = id; this.name = name; } public int getId() { return id; } @XmlAttribute public void setId(int id) { this.id = id; } public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } @Override public String toString() { return "College [id=" + id + ", name=" + name + "]"...
Bi Function: * When we need to access two input argument and we need to return some values, then we need to go for BiFunction. Syntax: interface BiFunction<T,U,R> { public R apply(T t,U u); //default method andThen() } Example: class Test { public static void main(String[] args) { BiFunction<Integer,Integer,Integer> f=(a,b)->a*b; System.out.println(f.apply(10,20)); System.out.println(f.apply(100,200)); } } Example: Create Student Object by taking name, roll no as input class Student { String name; int rollno; Student(String name,int rollno) { this.name=name; this.rollno=rollno; } } class Test { public static void main(String[] args) { ArrayList<Student> l = new ArrayList<Student>(); BiFunction<String,Integer,Student> f=(name,rollno)->new Student(name,rollno); l.add(f.apply("Durga",100)); l.add(f.apply("Ravi",200)); l.add(f.apply("Shiva",300)); l.add(f.apply(...
Lambda Expression for Collections: Using Lambda expression we can sort the collections datas. ArrayList<Integer> l = new ArrayList<>(); l.add(10); l.add(20); l.add(5); l.add(7); l.add(25); Java 7: Class MyCompartor implements comparator<Interger>{ public int compare(Interger I1, Integer I2){ return (I1 > I2)?-1: (I1 < I2)?1:0); } } Collections.sort(l, new MyComparator()); Java 8: Collections.sort(l, (I1, I2 ) -> (I1 > I2)?-1: (I1 < I2)?1:0 ); TreeSet<Integer> t = new TreeSet<>(); -> Default Natural Sorting. TreeSet<Integer> t = new TreeSet<>( (I1, I2) -> (I1 > I2)?-1:(I1 < I2)?1:0 ); Below is the example to sort the custom class , Class Employee { private int id; private String name; Employee(int id, String name){ this.id = id; this.name =name; } } Java 8 : Class Test{ public static void main(String args[]){ ArrayList<Emp...
Comments
Post a Comment