Filter data when insert into List

We use list for storing data but there is no way to insertation filter. I mean when i want to insert a specific type of data like a list that always store which string has this value. here i gave one example list that  always filterine during the insertation period. lets consider we have one collection that contain all the car make names. now we need one list that will store only Honda make name.

I used here two class HondaList and ToyotaList that act as list.

———————-HondaList.java

import java.util.LinkedList;
class HondaList extends LinkedList<String> {

public HondaList() {
super();
}

public boolean add(String make) {
if (make.indexOf(“Honda”) == -1) {
return false;
} else {
super.add(make);
return true;
}
}
}
—————- ToyotaList.java

import java.util.LinkedList;
class ToyotaList extends LinkedList<String> {

public ToyotaList() {
super();
}

public boolean add(String make) {
if (make.indexOf(“Toyota”) == -1) {
return false;
} else {
super.add(make);
return true;
}
}
}

—————— Main Class

public class ListDataFilterDemo {
private HondaList hondaList;
private ToyotaList toyotaList;

public ListDataFilterDemo() {
this.hondaList = new HondaList();
this.toyotaList = new ToyotaList();
}

public void addData(){
hondaList.add(“Honda Civic 2002”);
hondaList.add(“Honda Accord 2002”);
hondaList.add(“Toyota Corolla 2002”);

toyotaList.add(“Honda Civic 2002”);
toyotaList.add(“Honda Accord 2002”);
toyotaList.add(“Toyota Corolla 2002”);

for (String make : hondaList) {
System.out.println(make);
}

for (String make : toyotaList) {
System.out.println(make);
}
}

public static void main(String[] args) {
try {
ListDataFilterDemo demo = new ListDataFilterDemo();
demo.addData();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

————- Output

Honda Civic 2002
Honda Accord 2002
Toyota Corolla 2002

HondaList adds all data except Honda data same ToyotaList does.

One thought on “Filter data when insert into List

  1. Hey there just wanted to give you a quick heads up.
    The words in your article seem to be running off the screen in Ie.
    I’m not sure if this is a format issue or something to do with browser compatibility but I thought I’d post to let you
    know. The style and design look great though! Hope you get the
    issue solved soon. Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *