JAVA開発メモ

 

[リロード]   [新規|編集|差分|添付]   [トップ|一覧|単語検索|最終更新|バックアップ|ヘルプ]

サイトメニュー

public class TestBean { public String id1; public String id2; public TestBean(String id1, String id2) { super(); this.id1 = id1; this.id2 = id2; } public String getId1() { return id1; } public void setId1(String id1) { this.id1 = id1; } public String getId2() { return id2; } public void setId2(String id2) { this.id2 = id2; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id1 == null) ? 0 : id1.hashCode()); result = prime * result + ((id2 == null) ? 0 : id2.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; TestBean other = (TestBean) obj; if (id1 == null) { if (other.id1 != null) return false; } else if (!id1.equals(other.id1)) return false; if (id2 == null) { if (other.id2 != null) return false; } else if (!id2.equals(other.id2)) return false; return true; } } public class TestUtil { public static <T> List<T> overlapList(List<T> inputList) { List<T> outputList = new ArrayList<T>(); for (int i = 0; i < inputList.size(); i++) { T object = inputList.get(i); for (int j = i + 1; j < inputList.size(); j++) { if (object != null && object.equals(inputList.get(j)) && !outputList.contains(object)) { outputList.add(object); break; } } } return outputList; } public static void main(String[] args) { List<TestBean> testList = new ArrayList<TestBean>(); testList.add(new TestBean("a", "a")); testList.add(new TestBean("a", "b")); testList.add(new TestBean("a", "a")); testList.add(new TestBean("a", "b")); testList.add(new TestBean("a", "c")); for (TestBean testBean : overlapList(testList)) { System.out.println("id1:" + testBean.getId1() + " id2:" + testBean.getId2()); } } } id1:a id2:a id1:a id2:b public class TestUtil { public static <T> List<T> overlapAllList(List<T> inputList) { List<T> outputList = new ArrayList<T>(); for (int i = 0; i < inputList.size(); i++) { T object = inputList.get(i); for (int j = 0; j < inputList.size(); j++) { if (i != j && object != null && object.equals(inputList.get(j))) { outputList.add(object); break; } } } return outputList; } public static void main(String[] args) { List<TestBean> testList = new ArrayList<TestBean>(); testList.add(new TestBean("a", "a")); testList.add(new TestBean("a", "b")); testList.add(new TestBean("a", "a")); testList.add(new TestBean("a", "b")); testList.add(new TestBean("a", "c")); for (TestBean testBean : overlapAllList(testList)) { System.out.println("id1:" + testBean.getId1() + " id2:" + testBean.getId2()); } } } id1:a id2:a id1:a id2:b id1:a id2:a id1:a id2:b public static <T> List<T> overlapArray(T[] inputArray) { List<T> outputList = new ArrayList<T>(); for (int i = 0; i < inputArray.length; i++) { T object = inputArray[i]; for (int j = i + 1; j < inputArray.length; j++) { if (object != null && object.equals(inputArray[j]) && !outputList.contains(object)) { outputList.add(object); break; } } } return outputList; } public static <T> List<T> overlapAllArray(T[] inputArray) { List<T> outputList = new ArrayList<T>(); for (int i = 0; i < inputArray.length; i++) { T object = inputArray[i]; for (int j = 0; j < inputArray.length; j++) { if (i != j && object != null && object.equals(inputArray[j])) { outputList.add(object); break; } } } return outputList; }

リロード   新規 編集 差分   トップ 一覧 検索 最終更新 バックアップ   ヘルプ   最終更新のRSS
Last-modified: Wed, 24 Sep 2008 00:12:12 UTC (4986d)

Modified by MT22(Moriwaki Takashi)

"PukiWiki" 1.3.7 Copyright © 2001,2002,2003 PukiWiki Developers Team. License is GNU/GPL.
Based on "PukiWiki" 1.3 by sng
Powered by PHP 7.4.28

HTML convert time to 0.025 sec.