Monday, January 12, 2015

Get last index of a sub list and get max element in Java Program



How to get index of a sub list from another list

package com.java2novice.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MySubListIndex
{
public static void main(String a[])
{
        List list = new ArrayList();
        list.add("java");
        list.add("c");
        list.add("c++");
        list.add("unix");
        list.add("perl");
        list.add("php");
        list.add("javascript");
        list.add("ruby");
        list.add(".net");
        list.add("jdbc");
        list.add("servlets");
        List subList = new ArrayList();
        subList.add("php");
        subList.add("javascript");
        subList.add("ruby");
        System.out.println("Index of sublist: "+Collections.indexOfSubList(list, subList));
    }
}

How to get last index of a sub list from another list

package com.java2novice.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MySubListLastIndex
{
public static void main(String a[])
{
        List list = new ArrayList();
        list.add("java");
        list.add("c");
        list.add("c++");
        list.add("unix");
        list.add("perl");
        list.add("php");
        list.add("javascript");
        list.add("ruby");
        list.add(".net");
        list.add("jdbc");
        list.add("php");
        list.add("javascript");
        list.add("ruby");
        list.add("servlets");
        List subList = new ArrayList();
        subList.add("php");
        subList.add("javascript");
        subList.add("ruby");
        System.out.println("Last index of sublist: "
          +Collections.lastIndexOfSubList(list, subList));
    }
}

How to get max element from the given list

package com.java2novice.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MyMaxNumber
{
public static void main(String a[])
{
        List li = new ArrayList();
        li.add(23);
        li.add(44);
        li.add(12);
        li.add(45);
        li.add(2);
        li.add(16);
        System.out.println("Maximum element from the list:    "+Collections.max(li));
    }
}

No comments:

Post a Comment