限制Kotlin的功能

时间:2016-02-06 21:43:14

标签: java-8 java-stream kotlin

Java 8中有一个流方法package com.concretepage.util.stream; import java.util.Arrays; import java.util.List; public class LimitDemo { public static void main(String[] args) { List<String> list = Arrays.asList("AA","BB","CC","DD","EE"); list.stream().limit(3).forEach(s->System.out.println(s)); } }

AA
BB
CC 

输出:

android

Kotlin的模拟名称是什么,或者如何通过其他方式做得更好?

1 个答案:

答案 0 :(得分:24)

基于documentation

        ///////START OF NODE.H///////////
        #ifndef NODE_H
        #define NODE_H
        #include <iostream>
        template <typename T>
        class HashTable;

        template <typename T>
        class Node
        {
            friend class HashTable<T>;

            private:
                T data;

            public:
                Node(T Data): data(Data)
                {
                    std::cout << "In the node constructor" << std::endl;
                }

                Node() 
                {
                    decltype(data) {};
                }

                T getData() const
                {
                    return data;
                }
        };
        #endif
        //////////////////////END OF NODE.H////////////////////




        /////START OF HASHTABLE.H///////
        #ifndef HASHTABLE_H
        #define HASHTABLE_H
        #include "Node.h"
        #include <iostream>
        #include <array>
        #include <functional>
        #include <typeinfo>
        #include <string>
        const int TABLE_SIZE=5;
        template <typename T>
        class HashTable
        {
            private:
                std::array<std::array<Node<T>, TABLE_SIZE>, TABLE_SIZE> hashTable;
                std::array<std::array<bool, TABLE_SIZE>, TABLE_SIZE> spots;

            public:
                HashTable()
                {
                    for(int index=0;index<spots.size();++index)
                    {
                        for(int position=0;position<spots.at(index).size();++position)
                        {
                            spots.at(index).at(position)=false;
                        }
                    }
                }

                int hashFunction(Node<T> Node)
                {
                    auto key=Node.getData();

                    std::hash<decltype(Node.getData())> hash_function {};

                    int hash=hash_function(key);

                    if(hash < 0)
                    {
                        hash*=-1;
                    }

                    //std::cout << "The hash value return by the STL hash function for the key " << key << " is " << hash << std::endl;

                    if(hash > TABLE_SIZE)
                    {
                        hash%=TABLE_SIZE;
                    }

                    std::cout << "The hash value for the key " << key << " is " << hash << std::endl;

                    return hash;
                }

                void insert(Node<T> Node)
                {
                    int hashValue=hashFunction(Node);

                    auto location=hashTable.at(hashValue);

                    std::cout << "Going to insert " << Node.getData() << std::endl;

                    for(int index=0;index<location.size();++index)
                    {
                        if(spots.at(hashValue).at(index)==false)
                        {
                            std::cout << "Found a spot that is not taken!" << std::endl;

                            std::cout << "The size of the data at the spot in the array before we insert is: " << location.at(index).getData().size() << std::endl;

                            location.at(index)=Node;

                            std::cout << "The size of the data at the spot in the array after we insert is: " << location.at(index).getData().size() << std::endl;

                            std::cout << "The data that is in the spot in the array: " << location.at(index).getData() << std::endl;

                            std::cout << std::endl;

                            spots.at(hashValue).at(index)=true;

                            break;
                        }
                    }
                }

                bool contains(Node<T> Node)
                {
                    int hashValue=hashFunction(Node);

                    auto location=hashTable.at(hashValue);

                    auto result=find_if(begin(location), end(location), [Node] (const auto & element) {return element.getData()==Node.getData();});

                    if(result!=end(location))
                    {
                        return true;
                    }

                    return false;
                }

                int getSize() const
                {
                    int size {};

                    for(int index=0;index<hashTable.size();++index)
                    {
                        size+=hashTable.at(index).size();
                    }

                    return size;
                }

                void print()
                {
                    std::cout << "In the print function" << std::endl;

                    for(int index=0;index<hashTable.size();++index)
                    {
                        //std::cout << hashTable.at(index).size() << std::endl;

                        for(int position=0;position<hashTable.at(index).size();++position)
                        {                   
                            std::cout << hashTable.at(index).at(position).getData().size() << std::endl;
                        }
                    }

                    /*
                    for(int index=0;index<spots.size();++index)
                    {
                        for(int position=0;position<spots.at(index).size();++position)
                        {
                            if(spots.at(index).at(position)==true)
                            {
                                std::cout << "There should be some data here" << std::endl;
                            }
                        }
                    }
                    */
                }
        };
        #endif
        ////////////END OF HASHTABLE.H//////////



       ////////////START OF MAIN.CPP///////////
             #include "HashTable.h"
             #include <cstdlib>
             #include <random>
             #include <algorithm>
             using namespace std;                 
             int main()
             {
                HashTable<string> hash_table;

                hash_table.insert(Node<string>("Java"));

                hash_table.insert(Node<string>("C++"));

                hash_table.insert(Node<string>("C#"));

                hash_table.insert(Node<string>("Latex"));

                hash_table.insert(Node<string>("Python"));

             }
        /////////////END OF MAIN.CPP/////////////