Search This Blog

Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

Monday, April 24, 2023

Java Sample - Copy any object into any object including sub object fields generic

 Steps 1:

Source

Person.java(list address)

Address.java(User.java)


public class Person implements Serializable

{

private String firstName;

private String lastName;

private int age;

private List <Address> addressList;

}

public class Address implements Serializable

{

private int id;

private String address1;

private String address2;

private String city;

private String state;

private String country;

private String pincode;

private String contactNumber;

private String email;

private User user;

}




Target

PersonDTO.java (list addressDTO)

AddressDTO.java (User.java)

public class PersonDTO implements Serializable

{

private String firstName;

private int age;

private String lastName;

private List <AddressDTO> addressList;

}

public class AddressDTO implements Serializable

{

private int id;

private String address1;

private String address2;

private String city;

private String state;

private String country;

private String pincode;

private String contactNumber;

private String email;

private User user;

}


Step 2: Sample java class

package com.drvijay;

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

import org.apache.commons.lang3.RandomStringUtils;

import com.product.base.utils.ObjectUtils;

import com.product.base.utils.ProductConstants;

/**

* @author drvijay

* @date Apr-20-2023 10:30 AM

*/

@SuppressWarnings ( "unused" )

public class UtilHelper

{

private static final long serialVersionUID = 1L;

public static void copy ( Object source, Object target ) throws IllegalArgumentException, IllegalAccessException

{

Class <?> sourceClass = source.getClass ();

Class <?> targetClass = target.getClass ();

Field [] sourceFields = sourceClass.getDeclaredFields ();

Field [] targetFields = targetClass.getDeclaredFields ();

for ( Field sourceField : sourceFields )

{

for ( Field targetField : targetFields )

{

// If the source and target fields have the same name and type

if ( sourceField.getName ().equals ( targetField.getName () ) && sourceField.getType ().equals ( targetField.getType () ) )

{

// Make the source and target field accessible

sourceField.setAccessible ( true );

targetField.setAccessible ( true );

// Copy the value from the source field to the target field

targetField.set ( target, sourceField.get ( source ) );

// Stop looping through the target fields

break;

}

}

}

}

public static void main ( String [] args ) throws Exception

{

// Create a source object

Person sourcePerson = new Person ();

sourcePerson.setFirstName ( "vijay" );

sourcePerson.setLastName ( "kumar" );

sourcePerson.setAge ( 30 );

User u = new User ();

u.setUA ( "ua" );

u.setUB ( "ub" );

List<Address> aList = new ArrayList();

Address address = new Address ();

address.setAddress1 ( "add 1" );

address.setAddress2 ( "add 2" );

address.setPincode ( "54534" );

address.setUser ( u );

aList.add ( address );

address = new Address ();

address.setAddress1 ( "add 11" );

address.setAddress2 ( "add 22" );

address.setPincode ( "545341" );

aList.add ( address );

sourcePerson.setAddressList ( aList );

// Create a target object with similar properties

PersonDTO targetPersonDTO = new PersonDTO ();

// Copy the properties from the source object to the target object

UtilHelper.copy ( sourcePerson, targetPersonDTO );

// Print the values of the target object

System.out.println ( targetPersonDTO.getFirstName () );

System.out.println ( targetPersonDTO.getAge () );

System.out.println ( sourcePerson.toString () );

System.out.println ( targetPersonDTO.toString () );

System.out.println ( targetPersonDTO.getAddressList ().toString () );

}

}



Output


vijay

30

Person(firstName=vijay, lastName=kumar, age=30, addressList=[Address(id=0, address1=add 1, address2=add 2, city=null, state=null, country=null, pincode=54534, contactNumber=null, email=null, user=User(uA=ua, uB=ub)), Address(id=0, address1=add 11, address2=add 22, city=null, state=null, country=null, pincode=545341, contactNumber=null, email=null, user=null)])

PersonDTO(firstName=vijay, age=30, lastName=kumar, addressList=[Address(id=0, address1=add 1, address2=add 2, city=null, state=null, country=null, pincode=54534, contactNumber=null, email=null, user=User(uA=ua, uB=ub)), Address(id=0, address1=add 11, address2=add 22, city=null, state=null, country=null, pincode=545341, contactNumber=null, email=null, user=null)])

[Address(id=0, address1=add 1, address2=add 2, city=null, state=null, country=null, pincode=54534, contactNumber=null, email=null, user=User(uA=ua, uB=ub)), Address(id=0, address1=add 11, address2=add 22, city=null, state=null, country=null, pincode=545341, contactNumber=null, email=null, user=null)]


Wednesday, January 15, 2020

Java Stream API - Get Max value from two list.

Double max = volist.stream().map(vo -> modellist .stream()
  .collect(Collectors.averagingDouble(model ->
  jarowinklerdistance.apply(model.getCity(), vo.getCity())))
  ).max(Double::compare).get();
 
  System.out.println(max);

Wednesday, May 8, 2019

ObjectMapper - read JSON list to VO object

ObjectMapper mapper = new ObjectMapper ();

mapper.enable ( DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY );
mapper.configure ( JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true );

List queryBuilderVOList = Arrays.asList ( mapper.readValue ( mapper.writeValueAsString ( map.get ( "data" ) ).getBytes (), QueryBuilderVO [].class ) );

Tuesday, November 8, 2016

How to fetch from list of nested list- Dynamic Loop

Assume the bean is like below, CommentsBean has CommentsBeanlist.

public class CommentsBean {

int id;
int parentId;
String msg;
List commentsBeanList;

public CommentsBean() {

}

public CommentsBean(int id, int parentId, String msg) {
super();
this.id = id;
this.parentId = parentId;
this.msg = msg;
}

@Override
public String toString() {
return "CommentsBean [id=" + id + ", parentId=" + parentId + ", msg=" + msg + ", commentsBeanList="
+ commentsBeanList + "]";
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getParentId() {
return parentId;
}

public void setParentId(int parentId) {
this.parentId = parentId;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public List getCommentsBeanList() {
return commentsBeanList;
}

public void setCommentsBeanList(List commentsBeanList) {
this.commentsBeanList = commentsBeanList;
}


}


Program to extract that dynamically.

public class DynamicListLoop {

public static void main(String[] args) {
List commntsBeanList = new ArrayList<>();
List commntsBeanChildList1 = new ArrayList<>();
List commntsBeanChildList11 = new ArrayList<>();
CommentsBean commentsBeanchild20 = new CommentsBean(4,-1, "second");
CommentsBean commentsBeanchild21 = new CommentsBean(5,4, "second-child-1");
commntsBeanChildList11.add(commentsBeanchild20);
commntsBeanChildList11.add(commentsBeanchild21);
CommentsBean commentsBeanchild2 = new CommentsBean(3,2, "first-child-2");
CommentsBean commentsBeanchild = new CommentsBean(2,1, "first-child-1");
commentsBeanchild.setCommentsBeanList(commntsBeanChildList11);
commntsBeanChildList1.add(commentsBeanchild);
commntsBeanChildList1.add(commentsBeanchild2);
CommentsBean commentsBean = new CommentsBean(1,-1, "first");
commentsBean.setCommentsBeanList(commntsBeanChildList1);
commntsBeanList.add(commentsBean);
System.out.println(commntsBeanList.toString());
//write a function and pass the parent bean list.
commentsBeanParseInLoop (commntsBeanList );

}
public static void commentsBeanParseInLoop ( List commntsBeanList )
{
if ( commntsBeanList!=null && commntsBeanList.size() > 0 )
{
for ( CommentsBean cb : commntsBeanList )
{
System.out.println(cb.getId() + " : " + cb.getMsg() );
if ( cb.getCommentsBeanList() !=null && cb.getCommentsBeanList().size() > 0 )
{
//call the same function and pass the inner bean list
commentsBeanParseInLoop ( cb.getCommentsBeanList() );
}
}
}
}

}

Friday, January 22, 2010

List of Java Framework


Echo Cocoon Millstone OXF
Struts SOFIA Tapestry WebWork
RIFE Spring MVC Canyamo Maverick
JPublish JATO Folium Jucas
Verge Niggle Bishop Barracuda
Shocks TeaServlet wingS Action Framework
Expresso Bento jStatemachine jZonic
OpenEmcee Turbine Scope Warfare
JWAA Jaffa Jacquard Macaw
Smile MyFaces Chiba JBanana
Jeenius JWarp Genie Melati
Dovetail Cameleon JFormular Xoplon
Japple Helma Dinamica WebOnSwing
Nacho Cassandra Baritus Stripes
Click GWT

Thursday, January 7, 2010

Open Source Web Frameworks in Java

Tapestry
Tapestry is a powerful, open-source, all-Java framework for creating leading edge web applications in Java.

Cocoon
Apache Cocoon is a web development framework built around the concepts of separation of concerns and component-based web development.

MyFaces
MyFaces is an implementation of JavaServer(tm) Faces (JSR 127), a new and upcoming web application framework that accomplishes the MVC paradigm

Spring

Spring is a layered Java/J2EE application framework, based on code published in Expert One-on-One J2EE Design and Development.

Google Web Toolkit
Google Web Toolkit (GWT) is a Java software development framework that makes writing AJAX applications like Google Maps and Gmail easy for developers who don't speak browser quirks as a second language.

Turbine
Turbine is a servlet based framework that allows experienced Java developers to quickly build secure web applications.


WebOnSwing

WebOnSwing is a revolutionary multiple environment application framework that allows you to create web applications in the same way you develope a desktop one. You dont need to use JSP files, special tags, XML files, requests, posts, etc.

ThinWire
ThinWire is a development framework that allows you to easily build applications for the web that look and feel like the desktop applications you're familiar with.

Induction
Induction is a powerful, high performance, Java MVC web application framework. Induction supports dynamic application reloading, type-based dependency injection and dependency analysis between models, views and controllers.

jZeno

jZeno is an open source java development framework that offers the ability to develop rich web applications.

Aurora
Aurora is an open-source MVC framework that is aimed at writing configurable, fully object-oriented form controllers using the Spring Framework.

Vroom
Vroom is a framework for HTML/JSP and Java code binding. It's extremely simple to use and very powerful. It can control your HTML widgets on the fly. It can be applied independently as JSP/Servlet Model or can be integrated with Struts, JSF, YUI etc. No custom tags, no complex configurations, just simple binding configuration file and backing beans.

Struts
The core of the Struts framework is a flexible control layer based on standard technologies like Java Servlets, JavaBeans, ResourceBundles, and XML, as well as various Jakarta Commons packages. Struts encourages application architectures based on the Model 2 approach, a variation of the classic Model-View-Controller (MVC) design paradigm.


Millstone
a nice alternative to struts like framework you do not need to write a single HTML line

Wicket
Wicket is a Java web application framework that takes simplicity, separation of concerns and ease of development to a whole new level. Wicket pages can be mocked up, previewed and later revised using standard WYSIWYG HTML design tools. Dynamic content processing and form handling is all handled in Java code using a Swing-like component model backed by POJO data beans that can easily be persisted with Hibernate.


SwingWeb

Swingweb is an implementation of AWT toolkit that enables AWT/Swing application to be operating inside a web container and presented as a web application to the web browser, purely in HTML/CSS/javascript.

JSPWidget
JSPWidget is a JSP Framework that provides event-driven,view state retained,server-side GUI controls to make JSP development as intuitive as in traditional client forms or in client-based Web applications.


OpenXava
XML / Java Framework to develop J2EE business applications rapidly and easily. It is based in business components defined with XML. Feature rich and flexible since is used for years to create real business applications.


ZK
ZK is an AJAX-based, event-driven, XUL-compliant, all Java framework for enabling Web applications to have the rich user experiences. With intuitive event-driven model, you represent your application in feature-rich XUL components, and manipulate these components by listening to events triggered by users, as simple as you did for years in desktop applications. No JavaScript. No replication of business logic at the clients. No proprietary components. No compilation. ZK works with existent frameworks and technologies, such as JSF and Portals, so the pace of adapting it is all under your control.

ztemplates
ztemplates is a framework for creating web-applications based on Java 1.5. Features:

* new and unique action processing module
* clean, technology agnostic urls
* fully JSP, Velocity and AJAX compatible


Aranea Web Framework

Aranea is an Open Source (APL 2.0) Java Hierarchical Model-View-Controller Web Framework that provides a common simple approach to building the web application components, reusing custom or general GUI logic and extending the framework.

RSF

RSF is an open source web programming framework, with roughly the scope of Sun's JSF. RSF is written in Java, and is built on the Spring framework. RSF features a pure-HTML templating engine named IKAT,


Flower framework

A minimalistic framework that helps you in converting your Spring beans into HTTP-accessible controllers using one servlet and a few additional lines in your XML application context. This framework uses Extensible XML authoring feature available in Spring and allows you to expose the bean using just one additional XML attribute on your bean.

Hit Counter


View My Stats