導航:首頁 > 編程語言 > java對象合並

java對象合並

發布時間:2022-05-24 05:50:32

java,合並兩個相同對象的不同屬性值,如何做

首先區分一下類、對象和屬性的概念,A是類,a1,、a2是對象,arg1、arg2是屬性。
這個問題可以在聲明A類的時候為其設置兩個屬性arg1、arg2
初始化a1的時候arg2對應的參數傳null就可以
初始化a3的時候可以這么寫
A a3 = new A(a1.get(arg1),a2.get(arg2));
另外建議樓主找本java教材仔細閱讀第一章

Ⅱ java怎麼合並list對象集合

第一重循環 倒序循環,第二重循環這個list中找,找到就合並的第一重循環數據中,刪除這個數據。
for(int i = list.size(); i >=0; i--) {
User user = (User)list.get(i);

User temp;

for(int j = 0; j < list.size(); j ++) {

if(user.get("time").equals(temp.get("time"))) {

list.remove(j);

}

}

}

Ⅲ java 中怎麼合並同類對象的屬性

package cn.utils;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.beanutils.BeanUtils;public class ExtendObject {
/**
* 將相同類型的對象的內容向右合並
* @param beanType 返回對象的類型
* @param initObject 包含原始數據的對象
* @param updateObject包含修改後數據的對象
* @return返回兩個對象的合並,相同屬性的值如果convertedObject中包含,且不為null的話取它的值,否則取returnedObject的值
*/
@SuppressWarnings("unchecked")
public Object extendObject(Object beanType, Object initObject, Object updateObject){
Map map1 = BeanToMap(initObject);
Map map2 = BeanToMap(updateObject);
List list = getMapKeySet(map1);
for(int i=0; i<list.size(); i++){Object map2Value = map2.get(list.get(i));
if(null!=map2Value){
map1.put(list.get(i), map2Value);
}
}
return MapToBean(beanType, map1);
}
/**
* 將map轉化為bean
* @param bean 將要轉化成為的對象
* @param map 被轉化的map對象
*/
@SuppressWarnings("unchecked")
public Object MapToBean(Object bean,Map map){
Object type = null;
Date date = null ;
try {
type = bean.getClass().newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
for(PropertyDescriptor p: beanInfo.getPropertyDescriptors()){
String propertyName = p.getName();
Object mapValue = map.get(propertyName);
//去掉鍵為'class'的鍵值對
if(null!=mapValue&&!"class".equals(propertyName)){
//判斷該字元轉是否為日期類型
if(CheckType.isDateType((String)mapValue)){
String dateType = CheckType.getDateType((String)mapValue);
if(dateType.equals("yyyy-MM-dd HH:mm:ss")){
date = new SimpleDateFormat(dateType).parse((String)mapValue);
p.getWriteMethod().invoke(type, new Timestamp(date.getTime()));
}else{
p.getWriteMethod().invoke(type, date);
}
//判斷該字元串是否為整型,同時忽略值為數字,但是類型是字元串的Id們
}else if(CheckType.isInt((String) mapValue)&&(!Pattern.matches("/w*Id", propertyName))){
p.getWriteMethod().invoke(type, Integer.getInteger((String)mapValue).intValue());
//默認剩下的類型都是字元串型
}else{
p.getWriteMethod().invoke(type, mapValue);
}
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return type;
}
/**
* 將bean轉化為map
* @param object
* @return
*/
@SuppressWarnings("unchecked")
public Map BeanToMap(Object object){
Map map = null ;
try {
map = BeanUtils.describe(object);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return map;
}
/**
* 獲得對應Map的鍵值
* @param map
* @return
*/
@SuppressWarnings("unchecked")
public List getMapKeySet(Map map){
List list = new ArrayList();
Iterator iterator = map.keySet().iterator();
while(iterator.hasNext()){
list.add(iterator.next());
}
return list;
}///**
// * @param args
// */
//public static void main(String[] args) throws Exception{
//System.out.println(isInt("1"));
//Admin a = new Admin();
//a.setAdminId("1");
//a.setAdminPassword("1");
//
//Admin b = new Admin();
//b.setAdminPassword("2");
//Admin c = (Admin)extendObject(new Admin(),a,b);
//System.out.println(c.getAdminId()+"----"+c.getAdminPassword());
//}
}
------------------------------------------------------------------------------------
package cn.utils;
import java.util.regex.Pattern;
public class CheckType {
/**
* 判斷該字元串是否為日期類型
* @param str
* @return
*/
public static boolean isDateType(String str){
Boolean b = false;
String dateType1 ="/d{4}-/d{2}-/d{2}/s/d{2}:/d{2}:/d{2}./d*";
String dateType2 ="/d{4}-/d{2}-/d{2}/s/d{2}:/d{2}:/d{2}";
String dateType3 ="/d{4}-/d{2}-/d{2}";
if(Pattern.matches(dateType1, str)||
Pattern.matches(dateType2, str)||
Pattern.matches(dateType3, str)){
b = true;
}
return b;
}/**
* 返回字元串所屬日期格式
* @param str
* @return
*/
public static String getDateType(String str){
String dateType1 ="/d{4}-/d{2}-/d{2}/s/d{2}:/d{2}:/d{2}./d*";
String dateType2 ="/d{4}-/d{2}-/d{2}/s/d{2}:/d{2}:/d{2}";
String dateType3 ="/d{4}-/d{2}-/d{2}";
if(Pattern.matches(dateType1, str)||
Pattern.matches(dateType2, str)){
return"yyyy-MM-dd HH:mm:ss";
}
if(Pattern.matches(dateType3, str)){
return"yyyy-MM-dd";
}
return null;
}
/**
* 判斷該字元串是否為整型
* @param str
* @return
*/
public static boolean isInt(String str){
Boolean b = false;
if(Pattern.matches("/d+", str)){
b = true;
}
return b;
}
}

Ⅳ java中面向對象要求合並兩個同類型的數組生成一個新數組

參考邏輯
int len1 = array1.length();
int len2 = array2.length();
// 首先,定義一個新的Object數組
Object[] fullArray = new Object[len1 + len2];
// 拷貝數據
for(int i = 0; i < len1; i++) {
fullArray[i] = array1[i];
}
for(int i = len1; i < len1+len2; i++) {
fullArray[i] = array2[i - len1];
}

Ⅳ java 怎樣合並兩個list

List介面中,有一個方法addAll,可以實現合並list。

List<String> a=new ArrayList<String>();

List<String> b=new ArrayList<String>();

b.addAll(a);

合並出來的List就是b,而不是一個新的List .

如果創建新的可以:

ArrayList <String> c=(ArrayList <String> )a.clone;

c.addAll(b);

Ⅵ java 中怎樣將2個不同對象的list 和成一個list

list1.addAll(list2);

前提是list的類型要一樣,如果是兩個對象,那麼都轉成Object應該就可以了吧。

Ⅶ java 將兩個相同對象不同屬性list合並

程序如下:

IList a = new ArrayList();
IList b = new ArrayList();
(a as ArrayList).AddRange(b);

拓展資料:

Java programming language具有目前大部分編程語言所共有的一些特徵,被特意設計用於互聯網的分布式環境。Java具有類似於C++語言的"形式和感覺",但它要比C++語言更易於使用,而且在編程時徹底採用了一種"以對象為導向"的方式。使用Java編寫的應用程序,既可以在一台單獨的電腦上運行,也可以被分布在一個網路的伺服器端和客戶端運行。另外,Java還可以被用來編寫容量很小的應用程序模塊或者applet,做為網頁的一部分使用。applet可使網頁使用者和網頁之間進行互動式操作。

Java是Sun微系統公司在1995年推出的,推出之後馬上給互聯網的互動式應用帶來了新面貌。目前,最常用的兩種互聯網瀏覽器軟體中都包括一個Java虛擬機。幾乎所有的操作系統中都增添了Java編譯程序。

Ⅷ 如何合並兩個相同類型的 Java 對象

相同的部分提取出來,做成一個抽象類,然後其他的再從這個類里繼承,說這么明白了,代碼應該好寫吧。

Ⅸ JAVA 中List類中的對象合並的問題。

packagetest;

importjava.util.Arrays;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.LinkedList;
importjava.util.List;

publicclassStudent
{
Stringsno;
Stringsname;

publicStudent(Stringsno,Stringsname)
{
this.sno=sno;
this.sname=sname;
}

publicStringgetSno()
{
returnsno;
}

publicvoidsetSno(Stringsno)
{
this.sno=sno;
}

publicStringgetSname()
{
returnsname;
}

publicvoidsetSname(Stringsname)
{
this.sname=sname;
}

@Override
publicStringtoString()
{
StringBuilderbuilder=newStringBuilder();
builder.append("Student[sno=").append(sno).append(",sname=").append(sname).append("]");
returnbuilder.toString();
}

privatestaticList<Student>contains(List<Student>a,List<Student>b)
{
List<Student>list=newLinkedList<Student>();
list.addAll(a);
Stringtemp=list.toString();
for(Studentstudent:b)
{
Stringreg="sno="+student.getSno()+",";
if(temp.indexOf(reg)==-1)
{
list.add(student);
}
}
Collections.sort(list,newComparator<Student>()
{
@Override
publicintcompare(Studento1,Studento2)
{
inta=Integer.parseInt(o1.getSno());
intb=Integer.parseInt(o2.getSno());
if(a>b)
{
return1;
}
elseif(a<b)
{
return-1;
}
else
{
return0;
}
}
});
returnlist;
}

publicstaticvoidmain(String[]args)
{
Students1=newStudent("100","ko");
Students2=newStudent("102","pi");
Students3=newStudent("101","pid");
Students4=newStudent("102","pi");
List<Student>a=Arrays.asList(s1,s2);
List<Student>b=Arrays.asList(s3,s4);
List<Student>c=contains(a,b);
for(Studentstudent:c)
{
System.out.println(student);
}
}
}

Ⅹ java對象數組的合並問題

public class Counter {
public static void main(String[] args) throws Exception {
Object[] obj = { "a1,b1,c1,100", "a1,b2,c1,300", "a1,b1,c1,50",
"a1,b1,c2,100", "a1,b2,c1,100" };
Object[] result = new Object[obj.length];
int size = 0;

for (Object o : obj) {
String ori = o.toString();
String abc = ori.substring(0, ori.lastIndexOf(','));
boolean add = true;
for (int i = 0; i < size; i++) {
if (result[i].toString().indexOf(abc) == 0) {
double d1 = Double.parseDouble(ori.substring(ori
.lastIndexOf(',') + 1));
double d2 = Double.parseDouble(result[i].toString()
.substring(ori.lastIndexOf(',') + 1));
result[i] = abc + "," + (d1 + d2);
add = false;
break;
}
}
if (add) {
result[size++] = o;
}
}

for (int i = 0; i < size; i++) {
System.out.println(result[i]);
}
}
}

=================================
運行結果
a1,b1,c1,150.0
a1,b2,c1,400.0
a1,b1,c2,100

閱讀全文

與java對象合並相關的資料

熱點內容
無線路由如何設置成伺服器 瀏覽:136
QQ飛車源碼更新 瀏覽:899
虛擬機中編譯器 瀏覽:476
台達PLC編譯按鈕在哪裡 瀏覽:141
非編程計算器多少錢 瀏覽:655
房本還完貸款解壓 瀏覽:818
中國程序員有出名嗎 瀏覽:548
亳州雲伺服器 瀏覽:632
程序員最難的面試 瀏覽:894
配音秀app怎麼誦讀 瀏覽:751
sparkcore源碼 瀏覽:100
程序員中年生活 瀏覽:355
讀取加密信息失敗怎麼回事 瀏覽:510
編譯過程之後是預處理嗎 瀏覽:351
安卓是基於什麼做出來 瀏覽:600
視頻字幕提取APP怎麼使用 瀏覽:59
js通過ip地址連接伺服器嗎 瀏覽:848
java數字金額大寫金額 瀏覽:858
人人影視路由器固件編譯 瀏覽:967
照片通訊錄簡訊怎麼從安卓到蘋果 瀏覽:458