import java.util.ArrayList;
public class SampleList {
public static void main(String[] args) {
ArrayList<String> arl=new ArrayList<String>();
ArrayList<String> first=new ArrayList<String>();
ArrayList<String> second=new ArrayList<String>();
first.add("checkthis");
first.add("cat");
first.add("dog");
first.add("mat");
second.add("checkthis");
second.add("hello");
second.add("haio");
second.add("nameghj");
for (String a : first)
{
for (String b : second)
{
if (a.equals(b))
{
System.out.println("Equals " + a);
}
}
}
if(second.contains("checkthis"))
System.out.println("true");
else
System.out.println("false");
ArrayList<String> dup=new ArrayList<String>();
String name = "abcd";
String test ;
arl.add("namratha");
arl.add("sonu");
if(arl.contains("namratha"))
{
System.out.println("exe");
arl.remove("namratha");
arl.add("namratha");
}
for(String sample : arl)
{
System.out.println("list items are "+sample);
}
dup.addAll(arl);
for(String dupsam : dup)
{
System.out.println("Dup list "+dupsam);
}
if(!(name.equals(null))){
System.out.println("executed");
}
// try {
// if(test.equals(null))
// {
// System.out.println("test is null");
//
// }
// else
// {
// System.out.println("space is executed");
// }
// } catch (NullPointerException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
example 2 :
package com.sam;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
public class listcompare {
public static void main(String[] args) {
Collection listOne = new ArrayList(Arrays.asList("milan", "dingo",
"elpha", "hafil", "meat", "iga", "neeta.peeta"));
Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga",
"binga", "mike", "dingo"));
listOne.retainAll(listTwo);
System.out.println("********************"+listOne);
//my trial
Collection first = new ArrayList<>();
Collection second = new ArrayList<>();
first.add("check");
first.add("conn");
first.add("dog");
second.add("jkl");
second.add("check");
second.add("name");
if(first.retainAll(second))
System.out.println("first list "+first);
System.out.println("second list "+second);
System.out.println("first list after "+first);
}
}
example 3
package com.sam;
import java.util.*;
public class compareobj {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> al1 = new ArrayList();
al1.add("a1");
al1.add("a2");
al1.add("a3");
al1.add("a5");
al1.add("a6");
List<String> al2 = new ArrayList();
al2.add("a1");
al2.add("a2");
al2.add("a3");
al2.add("a4");
for (Object objList : al1) {
if (al2.contains(objList)) {
System.out.println("print same "+al2.toString());
al2.remove(objList);
}
else
{
System.out.println("diff are "+al2);
}
}
System.out.println("length" + al2.size());
System.out.println("Value in list" + al2);
}
}
public class SampleList {
public static void main(String[] args) {
ArrayList<String> arl=new ArrayList<String>();
ArrayList<String> first=new ArrayList<String>();
ArrayList<String> second=new ArrayList<String>();
first.add("checkthis");
first.add("cat");
first.add("dog");
first.add("mat");
second.add("checkthis");
second.add("hello");
second.add("haio");
second.add("nameghj");
for (String a : first)
{
for (String b : second)
{
if (a.equals(b))
{
System.out.println("Equals " + a);
}
}
}
if(second.contains("checkthis"))
System.out.println("true");
else
System.out.println("false");
ArrayList<String> dup=new ArrayList<String>();
String name = "abcd";
String test ;
arl.add("namratha");
arl.add("sonu");
if(arl.contains("namratha"))
{
System.out.println("exe");
arl.remove("namratha");
arl.add("namratha");
}
for(String sample : arl)
{
System.out.println("list items are "+sample);
}
dup.addAll(arl);
for(String dupsam : dup)
{
System.out.println("Dup list "+dupsam);
}
if(!(name.equals(null))){
System.out.println("executed");
}
// try {
// if(test.equals(null))
// {
// System.out.println("test is null");
//
// }
// else
// {
// System.out.println("space is executed");
// }
// } catch (NullPointerException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
example 2 :
package com.sam;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
public class listcompare {
public static void main(String[] args) {
Collection listOne = new ArrayList(Arrays.asList("milan", "dingo",
"elpha", "hafil", "meat", "iga", "neeta.peeta"));
Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga",
"binga", "mike", "dingo"));
listOne.retainAll(listTwo);
System.out.println("********************"+listOne);
//my trial
Collection first = new ArrayList<>();
Collection second = new ArrayList<>();
first.add("check");
first.add("conn");
first.add("dog");
second.add("jkl");
second.add("check");
second.add("name");
if(first.retainAll(second))
System.out.println("first list "+first);
System.out.println("second list "+second);
System.out.println("first list after "+first);
}
}
example 3
package com.sam;
import java.util.*;
public class compareobj {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> al1 = new ArrayList();
al1.add("a1");
al1.add("a2");
al1.add("a3");
al1.add("a5");
al1.add("a6");
List<String> al2 = new ArrayList();
al2.add("a1");
al2.add("a2");
al2.add("a3");
al2.add("a4");
for (Object objList : al1) {
if (al2.contains(objList)) {
System.out.println("print same "+al2.toString());
al2.remove(objList);
}
else
{
System.out.println("diff are "+al2);
}
}
System.out.println("length" + al2.size());
System.out.println("Value in list" + al2);
}
}
No comments:
Post a Comment